Skip to content

Commit 07ac62f

Browse files
JonathonRPLms24Luca Forstner
authored
test(svelte): Use vitest instead of jest (#10350)
Use Vitest for testing the Svelte SDK --------- Co-authored-by: Lukas Stracke <[email protected]> Co-authored-by: Luca Forstner <[email protected]>
1 parent 6dc4c65 commit 07ac62f

File tree

8 files changed

+55
-35
lines changed

8 files changed

+55
-35
lines changed

packages/svelte/.eslintrc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ module.exports = {
22
env: {
33
browser: true,
44
},
5+
overrides: [
6+
{
7+
files: ['vite.config.ts'],
8+
parserOptions: {
9+
project: ['tsconfig.test.json'],
10+
},
11+
},
12+
],
513
extends: ['../../.eslintrc.js'],
614
rules: {
715
'@sentry-internal/sdk/no-unsupported-es6-methods': 'off',

packages/svelte/jest.config.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

packages/svelte/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939
"svelte": "3.x || 4.x"
4040
},
4141
"devDependencies": {
42-
"@testing-library/svelte": "^3.2.1",
4342
"svelte": "3.49.0",
44-
"svelte-jester": "^2.3.2"
43+
"@sveltejs/vite-plugin-svelte": "1.4.0",
44+
"@testing-library/svelte": "^3.2.1"
4545
},
4646
"scripts": {
4747
"build": "run-p build:transpile build:types",
@@ -59,8 +59,8 @@
5959
"clean": "rimraf build coverage sentry-svelte-*.tgz",
6060
"fix": "eslint . --format stylish --fix",
6161
"lint": "eslint . --format stylish",
62-
"test": "jest",
63-
"test:watch": "jest --watch",
62+
"test": "vitest",
63+
"test:watch": "vitest --watch",
6464
"yalc:publish": "ts-node ../../scripts/prepack.ts && yalc publish build --push --sig"
6565
},
6666
"volta": {

packages/svelte/src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,10 @@ export type ComponentTrackingInitOptions = {
6060
} & SpanOptions;
6161

6262
export type TrackComponentOptions = {
63+
/**
64+
* The name of the component to be used in the recorded spans.
65+
*
66+
* @default to <Svelte Component> if not specified
67+
*/
6368
componentName?: string;
6469
} & SpanOptions;

packages/svelte/test/performance.test.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
import type { Scope } from '@sentry/core';
22
import { act, render } from '@testing-library/svelte';
33

4+
import { vi } from 'vitest';
45
// linter doesn't like Svelte component imports
56
import DummyComponent from './components/Dummy.svelte';
67

78
let returnUndefinedTransaction = false;
89

910
const testTransaction: { spans: any[]; startChild: jest.Mock; end: jest.Mock; isRecording: () => boolean } = {
1011
spans: [],
11-
startChild: jest.fn(),
12-
end: jest.fn(),
12+
startChild: vi.fn(),
13+
end: vi.fn(),
1314
isRecording: () => true,
1415
};
15-
const testUpdateSpan = { end: jest.fn() };
16+
const testUpdateSpan = { end: vi.fn() };
1617
const testInitSpan: any = {
1718
transaction: testTransaction,
18-
end: jest.fn(),
19-
startChild: jest.fn(),
19+
end: vi.fn(),
20+
startChild: vi.fn(),
2021
isRecording: () => true,
2122
};
2223

23-
jest.mock('@sentry/core', () => {
24-
const original = jest.requireActual('@sentry/core');
24+
vi.mock('@sentry/core', async () => {
25+
const original = await vi.importActual('@sentry/core');
2526
return {
2627
...original,
2728
getCurrentScope(): Scope {
@@ -36,7 +37,7 @@ jest.mock('@sentry/core', () => {
3637

3738
describe('Sentry.trackComponent()', () => {
3839
beforeEach(() => {
39-
jest.resetAllMocks();
40+
vi.resetAllMocks();
4041
testTransaction.spans = [];
4142

4243
testTransaction.startChild.mockImplementation(spanCtx => {
@@ -49,7 +50,7 @@ describe('Sentry.trackComponent()', () => {
4950
return testUpdateSpan;
5051
});
5152

52-
testInitSpan.end = jest.fn();
53+
testInitSpan.end = vi.fn();
5354
testInitSpan.isRecording = () => true;
5455
returnUndefinedTransaction = false;
5556
});
@@ -58,13 +59,13 @@ describe('Sentry.trackComponent()', () => {
5859
render(DummyComponent, { props: { options: {} } });
5960

6061
expect(testTransaction.startChild).toHaveBeenCalledWith({
61-
description: '<Dummy>',
62+
description: '<Dummy$>',
6263
op: 'ui.svelte.init',
6364
origin: 'auto.ui.svelte',
6465
});
6566

6667
expect(testInitSpan.startChild).toHaveBeenCalledWith({
67-
description: '<Dummy>',
68+
description: '<Dummy$>',
6869
op: 'ui.svelte.update',
6970
origin: 'auto.ui.svelte',
7071
});
@@ -91,7 +92,7 @@ describe('Sentry.trackComponent()', () => {
9192
// once for init (unimportant here), once for starting the update span
9293
expect(testTransaction.startChild).toHaveBeenCalledTimes(2);
9394
expect(testTransaction.startChild).toHaveBeenLastCalledWith({
94-
description: '<Dummy>',
95+
description: '<Dummy$>',
9596
op: 'ui.svelte.update',
9697
origin: 'auto.ui.svelte',
9798
});
@@ -102,7 +103,7 @@ describe('Sentry.trackComponent()', () => {
102103
render(DummyComponent, { props: { options: { trackUpdates: false } } });
103104

104105
expect(testTransaction.startChild).toHaveBeenCalledWith({
105-
description: '<Dummy>',
106+
description: '<Dummy$>',
106107
op: 'ui.svelte.init',
107108
origin: 'auto.ui.svelte',
108109
});
@@ -117,7 +118,7 @@ describe('Sentry.trackComponent()', () => {
117118
render(DummyComponent, { props: { options: { trackInit: false } } });
118119

119120
expect(testTransaction.startChild).toHaveBeenCalledWith({
120-
description: '<Dummy>',
121+
description: '<Dummy$>',
121122
op: 'ui.svelte.update',
122123
origin: 'auto.ui.svelte',
123124
});
@@ -187,7 +188,7 @@ describe('Sentry.trackComponent()', () => {
187188
// but not the second update
188189
expect(testTransaction.startChild).toHaveBeenCalledTimes(1);
189190
expect(testTransaction.startChild).toHaveBeenLastCalledWith({
190-
description: '<Dummy>',
191+
description: '<Dummy$>',
191192
op: 'ui.svelte.init',
192193
origin: 'auto.ui.svelte',
193194
});

packages/svelte/test/sdk.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import { SDK_VERSION } from '@sentry/browser';
22
import * as SentryBrowser from '@sentry/browser';
33
import type { EventProcessor } from '@sentry/types';
44

5+
import { vi } from 'vitest';
56
import { detectAndReportSvelteKit, init as svelteInit, isSvelteKitApp } from '../src/sdk';
67

78
let passedEventProcessor: EventProcessor | undefined;
89

9-
const browserInit = jest.spyOn(SentryBrowser, 'init');
10-
const addEventProcessor = jest
10+
const browserInit = vi.spyOn(SentryBrowser, 'init');
11+
const addEventProcessor = vi
1112
.spyOn(SentryBrowser, 'addEventProcessor')
1213
.mockImplementation((eventProcessor: EventProcessor) => {
1314
passedEventProcessor = eventProcessor;
@@ -16,7 +17,7 @@ const addEventProcessor = jest
1617

1718
describe('Initialize Svelte SDk', () => {
1819
beforeEach(() => {
19-
jest.clearAllMocks();
20+
vi.clearAllMocks();
2021
});
2122

2223
it('has the correct metadata', () => {
@@ -74,7 +75,7 @@ describe('Initialize Svelte SDk', () => {
7475
describe('detectAndReportSvelteKit()', () => {
7576
const originalHtmlBody = document.body.innerHTML;
7677
beforeEach(() => {
77-
jest.clearAllMocks();
78+
vi.clearAllMocks();
7879
document.body.innerHTML = originalHtmlBody;
7980
passedEventProcessor = undefined;
8081
});

packages/svelte/tsconfig.test.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"extends": "./tsconfig.json",
33

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

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

1010
// other package-specific, test-specific options
1111
}

packages/svelte/vite.config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { svelte } from '@sveltejs/vite-plugin-svelte';
2+
import type { UserConfig } from 'vitest';
3+
import baseConfig from '../../vite/vite.config';
4+
5+
export default {
6+
...baseConfig,
7+
plugins: [svelte({ hot: !process.env.VITEST })],
8+
test: {
9+
// test exists, no idea why TS doesn't recognize it
10+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
11+
...(baseConfig as UserConfig & { test: any }).test,
12+
environment: 'jsdom',
13+
alias: [{ find: /^svelte$/, replacement: 'svelte/internal' }],
14+
},
15+
};

0 commit comments

Comments
 (0)