Skip to content

Commit

Permalink
fix(web): injectSpeedInsights requires a parameter value (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
feugy authored Jan 12, 2024
1 parent 2422ad3 commit 2435e08
Show file tree
Hide file tree
Showing 10 changed files with 732 additions and 610 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
require.resolve('@vercel/style-guide/eslint/jest'),
],
env: {
node: true,
jest: true,
},
parserOptions: {
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
},
"license": "Apache-2.0",
"scripts": {
"prepare": "husky install"
"prepare": "husky install",
"test": "pnpm -F @vercel/speed-insights test"
},
"prettier": "@vercel/style-guide/prettier",
"dependencies": {
"lint-staged": "^14.0.1"
},
"devDependencies": {
"@vercel/style-guide": "^5.0.1",
"@vercel/style-guide": "^5",
"husky": "^8.0.3",
"prettier": "3.0.3",
"typescript": "^5.2.2"
"typescript": "^5"
},
"packageManager": "pnpm@8.6.5"
}
6 changes: 4 additions & 2 deletions packages/web/jest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import '@testing-library/jest-dom';

beforeEach(() => {
// reset dom before each test
document.getElementsByTagName('html')[0].innerHTML = '';
if ('document' in global) {
/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- there is an HTML element */
document.querySelector('html')!.innerHTML = '';
}
});
10 changes: 5 additions & 5 deletions packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@
"@swc/jest": "^0.2.29",
"@testing-library/jest-dom": "^6.1.2",
"@testing-library/react": "^14.0.0",
"@types/jest": "^29.5.4",
"@types/node": "^20.5.9",
"@types/react": "^18.2.21",
"@types/jest": "^29.5.11",
"@types/node": "^20.11.0",
"@types/react": "^18.2.47",
"copyfiles": "^2.4.1",
"jest": "^29.6.4",
"jest-environment-jsdom": "^29.6.4",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"next": "^13.4.19",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
1 change: 0 additions & 1 deletion packages/web/scripts/postinstall.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-undef -- Allow the use `process` */
/* eslint-disable no-console -- Allow the use of `console` */
import fs from 'node:fs';
import path from 'node:path';
Expand Down
8 changes: 8 additions & 0 deletions packages/web/src/generic.server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @jest-environment node */
import { injectSpeedInsights } from './generic';

describe('injectSpeedInsights()', () => {
it('does nothing in a server environment', () => {
expect(injectSpeedInsights({})).toBe(null);
});
});
33 changes: 33 additions & 0 deletions packages/web/src/generic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { injectSpeedInsights } from './generic';

describe('injectSpeedInsights()', () => {
it('allows no parameters', () => {
expect(injectSpeedInsights()).toEqual({
setRoute: expect.any(Function) as () => void,
});
expectInjectedScript();
});

it('can set framework', () => {
const framework = 'sveltekit';
expect(injectSpeedInsights({ framework })).toEqual({
setRoute: expect.any(Function) as () => void,
});
expectInjectedScript({ framework });
});
});

function expectInjectedScript({
src = 'https://va.vercel-scripts.com/v1/speed-insights/script.debug.js',
framework = '',
} = {}): void {
const candidateScript = document.querySelector('script');
expect(candidateScript).toBeDefined();
/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- the previous assertion fails otherwise */
const script = candidateScript!;
expect(script.defer).toBe(true);
expect(script.src).toBe(src);
expect(script.dataset.sdkn).toBe(
`@vercel/speed-insights${framework ? `/${framework}` : ''}`,
);
}
2 changes: 1 addition & 1 deletion packages/web/src/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const PROXY_SCRIPT_URL = `/_vercel/speed-insights/script.js`;
function injectSpeedInsights(
props: SpeedInsightsProps & {
framework?: string;
},
} = {},
): {
setRoute: (route: string | null) => void;
} | null {
Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { computeRoute } from './utils';

describe('utils', () => {
describe('computeRoute', () => {
describe('computeRoute()', () => {
it('returns unchanged pathname if no pathParams provided', () => {
expect(computeRoute('/vercel/next-site/analytics', null)).toBe(
'/vercel/next-site/analytics',
Expand Down
Loading

5 comments on commit 2435e08

@vercel
Copy link

@vercel vercel bot commented on 2435e08 Jan 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 2435e08 Jan 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 2435e08 Jan 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 2435e08 Jan 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 2435e08 Jan 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.