Skip to content

Commit 9c8ba4b

Browse files
committed
fix tests
1 parent 9c1d06d commit 9c8ba4b

File tree

4 files changed

+122
-10
lines changed

4 files changed

+122
-10
lines changed

dev-packages/e2e-tests/test-applications/solidstart-spa/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"clean": "pnpx rimraf node_modules pnpm-lock.yaml .vinxi .output",
66
"build": "vinxi build && sh post_build.sh",
77
"preview": "HOST=localhost PORT=3030 vinxi start",
8+
"start:import": "HOST=localhost PORT=3030 node --import ./.output/server/instrument.server.mjs .output/server/index.mjs",
89
"test:prod": "TEST_ENV=production playwright test",
910
"test:build": "pnpm install && pnpm build",
1011
"test:assert": "pnpm test:prod"

dev-packages/e2e-tests/test-applications/solidstart-spa/playwright.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { getPlaywrightConfig } from '@sentry-internal/test-utils';
22

33
const config = getPlaywrightConfig({
4-
startCommand: 'pnpm preview',
4+
startCommand: 'pnpm start:import',
55
port: 3030,
66
});
77

packages/solidstart/src/config/addInstrumentation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export async function addInstrumentationFileToBuild(nitro: Nitro): Promise<void>
7474
} catch (error) {
7575
consoleSandbox(() => {
7676
// eslint-disable-next-line no-console
77-
console.warn('[Sentry SolidStart withSentry] Build process failed.', error);
77+
console.warn('[Sentry SolidStart withSentry] Failed to add instrumentation file to build.', error);
7878
});
7979
}
8080
});

packages/solidstart/test/config/addInstrumentation.test.ts

Lines changed: 119 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,24 @@ const fsAccessMock = vi.fn();
88
const fsCopyFileMock = vi.fn();
99
const fsReadFile = vi.fn();
1010
const fsWriteFileMock = vi.fn();
11+
const fsMkdirMock = vi.fn();
12+
const fsReaddirMock = vi.fn();
13+
const fsExistsSyncMock = vi.fn();
1114

1215
vi.mock('fs', async () => {
1316
const actual = await vi.importActual('fs');
1417
return {
1518
...actual,
19+
existsSync: (...args: unknown[]) => fsExistsSyncMock(...args),
1620
promises: {
1721
// @ts-expect-error this exists
1822
...actual.promises,
1923
access: (...args: unknown[]) => fsAccessMock(...args),
2024
copyFile: (...args: unknown[]) => fsCopyFileMock(...args),
2125
readFile: (...args: unknown[]) => fsReadFile(...args),
2226
writeFile: (...args: unknown[]) => fsWriteFileMock(...args),
27+
mkdir: (...args: unknown[]) => fsMkdirMock(...args),
28+
readdir: (...args: unknown[]) => fsReaddirMock(...args),
2329
},
2430
};
2531
});
@@ -30,6 +36,9 @@ beforeEach(() => {
3036

3137
describe('addInstrumentationFileToBuild()', () => {
3238
const nitroOptions: Nitro = {
39+
hooks: {
40+
hook: vi.fn(),
41+
},
3342
options: {
3443
buildDir: '/path/to/buildDir',
3544
output: {
@@ -39,40 +48,142 @@ describe('addInstrumentationFileToBuild()', () => {
3948
},
4049
};
4150

51+
const callNitroCloseHook = async () => {
52+
const hookCallback = nitroOptions.hooks.hook.mock.calls[0][1];
53+
await hookCallback();
54+
};
55+
4256
it('adds `instrument.server.mjs` to the server output directory', async () => {
4357
fsCopyFileMock.mockResolvedValueOnce(true);
4458
await addInstrumentationFileToBuild(nitroOptions);
59+
60+
await callNitroCloseHook();
61+
4562
expect(fsCopyFileMock).toHaveBeenCalledWith(
4663
'/path/to/buildDir/build/ssr/instrument.server.js',
4764
'/path/to/serverDir/instrument.server.mjs',
4865
);
49-
expect(consoleLogSpy).toHaveBeenCalledWith(
50-
'[Sentry SolidStart withSentry] Successfully created /path/to/serverDir/instrument.server.mjs.',
51-
);
5266
});
5367

54-
it('warns when `instrument.server.js` can not be copied to the server output directory', async () => {
68+
it('warns when `instrument.server.js` cannot be copied to the server output directory', async () => {
5569
const error = new Error('Failed to copy file.');
5670
fsCopyFileMock.mockRejectedValueOnce(error);
5771
await addInstrumentationFileToBuild(nitroOptions);
72+
73+
await callNitroCloseHook();
74+
5875
expect(fsCopyFileMock).toHaveBeenCalledWith(
5976
'/path/to/buildDir/build/ssr/instrument.server.js',
6077
'/path/to/serverDir/instrument.server.mjs',
6178
);
6279
expect(consoleWarnSpy).toHaveBeenCalledWith(
63-
'[Sentry SolidStart withSentry] Failed to create /path/to/serverDir/instrument.server.mjs.',
80+
'[Sentry SolidStart withSentry] Failed to add instrumentation file to build.',
6481
error,
6582
);
6683
});
6784

68-
it.each([staticHostPresets])("doesn't add `instrument.server.mjs` for static host `%s`", async preset => {
69-
await addInstrumentationFileToBuild({
85+
it.each(staticHostPresets)("doesn't add `instrument.server.mjs` for static host `%s`", async preset => {
86+
const staticNitroOptions = {
7087
...nitroOptions,
7188
options: {
7289
...nitroOptions.options,
7390
preset,
7491
},
75-
});
92+
};
93+
94+
await addInstrumentationFileToBuild(staticNitroOptions);
95+
96+
await callNitroCloseHook();
97+
7698
expect(fsCopyFileMock).not.toHaveBeenCalled();
7799
});
100+
101+
it('creates assets directory if it does not exist', async () => {
102+
fsExistsSyncMock.mockReturnValue(false);
103+
fsMkdirMock.mockResolvedValueOnce(true);
104+
fsCopyFileMock.mockResolvedValueOnce(true);
105+
await addInstrumentationFileToBuild(nitroOptions);
106+
107+
await callNitroCloseHook();
108+
109+
expect(fsMkdirMock).toHaveBeenCalledWith('/path/to/serverDir/assets', { recursive: true });
110+
expect(consoleLogSpy).toHaveBeenCalledWith(
111+
'[Sentry SolidStart withSentry] Successfully created directory /path/to/serverDir/assets.',
112+
);
113+
});
114+
115+
it('does not create assets directory if it already exists', async () => {
116+
fsExistsSyncMock.mockReturnValue(true);
117+
await addInstrumentationFileToBuild(nitroOptions);
118+
119+
await callNitroCloseHook();
120+
121+
expect(fsMkdirMock).not.toHaveBeenCalled();
122+
});
123+
124+
it('copies release injection file if available', async () => {
125+
fsExistsSyncMock.mockReturnValue(true);
126+
fsReaddirMock.mockResolvedValueOnce(['_sentry-release-injection-file-test.js']);
127+
fsCopyFileMock.mockResolvedValueOnce(true);
128+
await addInstrumentationFileToBuild(nitroOptions);
129+
130+
await callNitroCloseHook();
131+
132+
expect(fsCopyFileMock).toHaveBeenCalledWith(
133+
'/path/to/buildDir/build/ssr/assets/_sentry-release-injection-file-test.js',
134+
'/path/to/serverDir/assets/_sentry-release-injection-file-test.js',
135+
);
136+
expect(consoleLogSpy).toHaveBeenCalledWith(
137+
'[Sentry SolidStart withSentry] Successfully created /path/to/serverDir/assets/_sentry-release-injection-file-test.js.',
138+
);
139+
});
140+
141+
it('warns when release injection file cannot be copied', async () => {
142+
const error = new Error('Failed to copy release injection file.');
143+
fsExistsSyncMock.mockReturnValue(true);
144+
fsReaddirMock.mockResolvedValueOnce(['_sentry-release-injection-file-test.js']);
145+
fsCopyFileMock.mockRejectedValueOnce(error);
146+
await addInstrumentationFileToBuild(nitroOptions);
147+
148+
await callNitroCloseHook();
149+
150+
expect(fsCopyFileMock).toHaveBeenCalledWith(
151+
'/path/to/buildDir/build/ssr/assets/_sentry-release-injection-file-test.js',
152+
'/path/to/serverDir/assets/_sentry-release-injection-file-test.js',
153+
);
154+
expect(consoleWarnSpy).toHaveBeenCalledWith(
155+
'[Sentry SolidStart withSentry] Failed to copy release injection file.',
156+
error,
157+
);
158+
});
159+
160+
it('does not copy release injection file if not found', async () => {
161+
fsExistsSyncMock.mockReturnValue(true);
162+
fsReaddirMock.mockResolvedValueOnce([]);
163+
await addInstrumentationFileToBuild(nitroOptions);
164+
165+
await callNitroCloseHook();
166+
167+
expect(fsCopyFileMock).not.toHaveBeenCalledWith(
168+
expect.stringContaining('_sentry-release-injection-file-'),
169+
expect.any(String),
170+
);
171+
});
172+
173+
it('warns when `instrument.server.js` is not found', async () => {
174+
const error = new Error('File not found');
175+
fsCopyFileMock.mockRejectedValueOnce(error);
176+
await addInstrumentationFileToBuild(nitroOptions);
177+
178+
await callNitroCloseHook();
179+
180+
expect(fsCopyFileMock).toHaveBeenCalledWith(
181+
'/path/to/buildDir/build/ssr/instrument.server.js',
182+
'/path/to/serverDir/instrument.server.mjs',
183+
);
184+
expect(consoleWarnSpy).toHaveBeenCalledWith(
185+
'[Sentry SolidStart withSentry] Failed to add instrumentation file to build.',
186+
error,
187+
);
188+
});
78189
});

0 commit comments

Comments
 (0)