Skip to content

Commit ba99e7c

Browse files
authored
test(sveltekit): Switch to vitest (#7438)
1 parent a70376e commit ba99e7c

14 files changed

+407
-43
lines changed

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
"@types/node": "~10.17.0",
8282
"@types/rimraf": "^3.0.2",
8383
"@types/sinon": "^7.0.11",
84+
"@vitest/coverage-c8": "^0.29.2",
8485
"acorn": "^8.7.0",
8586
"chai": "^4.1.2",
8687
"codecov": "^3.6.5",
@@ -112,7 +113,8 @@
112113
"ts-node": "10.9.1",
113114
"tslib": "^2.3.1",
114115
"typedoc": "^0.18.0",
115-
"typescript": "3.8.3"
116+
"typescript": "3.8.3",
117+
"vitest": "^0.29.2"
116118
},
117119
"resolutions": {
118120
"**/agent-base": "5"

packages/sveltekit/jest.config.js

-3
This file was deleted.

packages/sveltekit/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@
6363
"lint:eslint": "eslint . --format stylish",
6464
"lint:prettier": "prettier --check \"{src,test,scripts}/**/**.ts\"",
6565
"test": "yarn test:unit",
66-
"test:unit": "jest",
67-
"test:watch": "jest --watch"
66+
"test:unit": "vitest",
67+
"test:watch": "vitest --watch"
6868
},
6969
"volta": {
7070
"extends": "../../package.json"

packages/sveltekit/test/client/handleError.test.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import { Scope } from '@sentry/svelte';
44
// adding a custom resolver, which will take too much time.
55
// eslint-disable-next-line import/no-unresolved
66
import type { HandleClientError, NavigationEvent } from '@sveltejs/kit';
7+
import { vi } from 'vitest';
78

89
import { handleErrorWithSentry } from '../../src/client/handleError';
910

10-
const mockCaptureException = jest.fn();
11+
const mockCaptureException = vi.fn();
1112
let mockScope = new Scope();
1213

13-
jest.mock('@sentry/svelte', () => {
14-
const original = jest.requireActual('@sentry/core');
14+
vi.mock('@sentry/svelte', async () => {
15+
const original = (await vi.importActual('@sentry/core')) as any;
1516
return {
1617
...original,
1718
captureException: (err: unknown, cb: (arg0: unknown) => unknown) => {
@@ -22,10 +23,10 @@ jest.mock('@sentry/svelte', () => {
2223
};
2324
});
2425

25-
const mockAddExceptionMechanism = jest.fn();
26+
const mockAddExceptionMechanism = vi.fn();
2627

27-
jest.mock('@sentry/utils', () => {
28-
const original = jest.requireActual('@sentry/utils');
28+
vi.mock('@sentry/utils', async () => {
29+
const original = (await vi.importActual('@sentry/utils')) as any;
2930
return {
3031
...original,
3132
addExceptionMechanism: (...args: unknown[]) => mockAddExceptionMechanism(...args),
@@ -68,15 +69,15 @@ describe('handleError', () => {
6869
it('calls captureException', async () => {
6970
const wrappedHandleError = handleErrorWithSentry(handleError);
7071
const mockError = new Error('test');
71-
const returnVal = await wrappedHandleError({ error: mockError, event: navigationEvent });
72+
const returnVal = (await wrappedHandleError({ error: mockError, event: navigationEvent })) as any;
7273

73-
expect(returnVal!.message).toEqual('Whoops!');
74+
expect(returnVal.message).toEqual('Whoops!');
7475
expect(mockCaptureException).toHaveBeenCalledTimes(1);
7576
expect(mockCaptureException).toHaveBeenCalledWith(mockError, expect.any(Function));
7677
});
7778

7879
it('adds an exception mechanism', async () => {
79-
const addEventProcessorSpy = jest.spyOn(mockScope, 'addEventProcessor').mockImplementationOnce(callback => {
80+
const addEventProcessorSpy = vi.spyOn(mockScope, 'addEventProcessor').mockImplementationOnce(callback => {
8081
void callback({}, { event_id: 'fake-event-id' });
8182
return mockScope;
8283
});

packages/sveltekit/test/client/sdk.test.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { getCurrentHub } from '@sentry/core';
22
import * as SentrySvelte from '@sentry/svelte';
33
import { SDK_VERSION, WINDOW } from '@sentry/svelte';
4+
import { vi } from 'vitest';
45

56
import { init } from '../../src/client/sdk';
6-
const svelteInit = jest.spyOn(SentrySvelte, 'init');
7+
8+
const svelteInit = vi.spyOn(SentrySvelte, 'init');
79

810
describe('Sentry client SDK', () => {
911
describe('init', () => {
1012
afterEach(() => {
11-
jest.clearAllMocks();
13+
vi.clearAllMocks();
1214
WINDOW.__SENTRY__.hub = undefined;
1315
});
1416

packages/sveltekit/test/config/vitePlugins.test.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
import * as fs from 'fs';
1+
import type * as fs from 'fs';
2+
import { vi } from 'vitest';
23

34
import { injectSentryInitPlugin } from '../../src/config/vitePlugins';
45

6+
vi.mock('fs', async () => {
7+
const original = await vi.importActual<typeof fs>('fs');
8+
return {
9+
...original,
10+
existsSync: vi.fn().mockReturnValue(true),
11+
};
12+
});
13+
514
describe('injectSentryInitPlugin', () => {
615
it('has its basic properties set', () => {
716
expect(injectSentryInitPlugin.name).toBe('sentry-init-injection-plugin');
817
expect(injectSentryInitPlugin.enforce).toBe('pre');
918
expect(typeof injectSentryInitPlugin.transform).toBe('function');
1019
});
1120

12-
describe('tansform', () => {
13-
jest.spyOn(fs, 'existsSync').mockReturnValue(true);
14-
21+
describe('transform', () => {
1522
it('transforms the server index file', () => {
1623
const code = 'foo();';
1724
const id = '/node_modules/@sveltejs/kit/src/runtime/server/index.js';

packages/sveltekit/test/server/handleError.test.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import { Scope } from '@sentry/node';
44
// adding a custom resolver, which will take too much time.
55
// eslint-disable-next-line import/no-unresolved
66
import type { HandleServerError, RequestEvent } from '@sveltejs/kit';
7+
import { vi } from 'vitest';
78

89
import { handleErrorWithSentry } from '../../src/server/handleError';
910

10-
const mockCaptureException = jest.fn();
11+
const mockCaptureException = vi.fn();
1112
let mockScope = new Scope();
1213

13-
jest.mock('@sentry/node', () => {
14-
const original = jest.requireActual('@sentry/core');
14+
vi.mock('@sentry/node', async () => {
15+
const original = (await vi.importActual('@sentry/core')) as any;
1516
return {
1617
...original,
1718
captureException: (err: unknown, cb: (arg0: unknown) => unknown) => {
@@ -22,10 +23,10 @@ jest.mock('@sentry/node', () => {
2223
};
2324
});
2425

25-
const mockAddExceptionMechanism = jest.fn();
26+
const mockAddExceptionMechanism = vi.fn();
2627

27-
jest.mock('@sentry/utils', () => {
28-
const original = jest.requireActual('@sentry/utils');
28+
vi.mock('@sentry/utils', async () => {
29+
const original = (await vi.importActual('@sentry/utils')) as any;
2930
return {
3031
...original,
3132
addExceptionMechanism: (...args: unknown[]) => mockAddExceptionMechanism(...args),
@@ -60,15 +61,15 @@ describe('handleError', () => {
6061
it('calls captureException', async () => {
6162
const wrappedHandleError = handleErrorWithSentry(handleError);
6263
const mockError = new Error('test');
63-
const returnVal = await wrappedHandleError({ error: mockError, event: requestEvent });
64+
const returnVal = (await wrappedHandleError({ error: mockError, event: requestEvent })) as any;
6465

65-
expect(returnVal!.message).toEqual('Whoops!');
66+
expect(returnVal.message).toEqual('Whoops!');
6667
expect(mockCaptureException).toHaveBeenCalledTimes(1);
6768
expect(mockCaptureException).toHaveBeenCalledWith(mockError, expect.any(Function));
6869
});
6970

7071
it('adds an exception mechanism', async () => {
71-
const addEventProcessorSpy = jest.spyOn(mockScope, 'addEventProcessor').mockImplementationOnce(callback => {
72+
const addEventProcessorSpy = vi.spyOn(mockScope, 'addEventProcessor').mockImplementationOnce(callback => {
7273
void callback({}, { event_id: 'fake-event-id' });
7374
return mockScope;
7475
});

packages/sveltekit/test/server/sdk.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import { GLOBAL_OBJ } from '@sentry/utils';
55

66
import { init } from '../../src/server/sdk';
77

8-
const nodeInit = jest.spyOn(SentryNode, 'init');
8+
const nodeInit = vi.spyOn(SentryNode, 'init');
99

1010
describe('Sentry server SDK', () => {
1111
describe('init', () => {
1212
afterEach(() => {
13-
jest.clearAllMocks();
13+
vi.clearAllMocks();
1414
GLOBAL_OBJ.__SENTRY__.hub = undefined;
1515
});
1616

packages/sveltekit/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"extends": "../../tsconfig.json",
33

4-
"include": ["src/**/*"],
4+
"include": ["src/**/*", "vite.config.ts"],
55

66
"compilerOptions": {
77
// package-specific options

packages/sveltekit/tsconfig.test.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66
"compilerOptions": {
77
// should include all types from `./tsconfig.json` plus types for all test frameworks used
8-
"types": ["node", "jest"]
8+
"types": ["node", "vitest/globals"]
99
}
1010
}

packages/sveltekit/vite.config.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import baseConfig from '../../vite/vite.config';
2+
3+
export default baseConfig;

tsconfig.dev.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
{
33
"extends": "./tsconfig.json",
44

5-
"include": ["**/scripts/**/*.ts", "jest/**/*.ts"],
5+
"include": ["**/scripts/**/*.ts", "jest/**/*.ts", "vite/**/*.ts"],
66

77
"compilerOptions": {
8-
"types": ["node", "jest"],
8+
"types": ["node", "jest", "vitest/globals"]
99
}
1010
}

vite/vite.config.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { defineConfig } from 'vitest/config';
2+
3+
export default defineConfig({
4+
define: {
5+
__DEBUG_BUILD__: true,
6+
},
7+
test: {
8+
globals: true,
9+
coverage: {
10+
enabled: true,
11+
reportsDirectory: './coverage',
12+
},
13+
typecheck: {
14+
tsconfig: './tsconfig.test.json',
15+
},
16+
},
17+
});

0 commit comments

Comments
 (0)