Skip to content

Commit

Permalink
test(sveltekit): Switch to vitest (#7438)
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhiPrasad committed Mar 13, 2023
1 parent a70376e commit ba99e7c
Show file tree
Hide file tree
Showing 14 changed files with 407 additions and 43 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"@types/node": "~10.17.0",
"@types/rimraf": "^3.0.2",
"@types/sinon": "^7.0.11",
"@vitest/coverage-c8": "^0.29.2",
"acorn": "^8.7.0",
"chai": "^4.1.2",
"codecov": "^3.6.5",
Expand Down Expand Up @@ -112,7 +113,8 @@
"ts-node": "10.9.1",
"tslib": "^2.3.1",
"typedoc": "^0.18.0",
"typescript": "3.8.3"
"typescript": "3.8.3",
"vitest": "^0.29.2"
},
"resolutions": {
"**/agent-base": "5"
Expand Down
3 changes: 0 additions & 3 deletions packages/sveltekit/jest.config.js

This file was deleted.

4 changes: 2 additions & 2 deletions packages/sveltekit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
"lint:eslint": "eslint . --format stylish",
"lint:prettier": "prettier --check \"{src,test,scripts}/**/**.ts\"",
"test": "yarn test:unit",
"test:unit": "jest",
"test:watch": "jest --watch"
"test:unit": "vitest",
"test:watch": "vitest --watch"
},
"volta": {
"extends": "../../package.json"
Expand Down
19 changes: 10 additions & 9 deletions packages/sveltekit/test/client/handleError.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import { Scope } from '@sentry/svelte';
// adding a custom resolver, which will take too much time.
// eslint-disable-next-line import/no-unresolved
import type { HandleClientError, NavigationEvent } from '@sveltejs/kit';
import { vi } from 'vitest';

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

const mockCaptureException = jest.fn();
const mockCaptureException = vi.fn();
let mockScope = new Scope();

jest.mock('@sentry/svelte', () => {
const original = jest.requireActual('@sentry/core');
vi.mock('@sentry/svelte', async () => {
const original = (await vi.importActual('@sentry/core')) as any;
return {
...original,
captureException: (err: unknown, cb: (arg0: unknown) => unknown) => {
Expand All @@ -22,10 +23,10 @@ jest.mock('@sentry/svelte', () => {
};
});

const mockAddExceptionMechanism = jest.fn();
const mockAddExceptionMechanism = vi.fn();

jest.mock('@sentry/utils', () => {
const original = jest.requireActual('@sentry/utils');
vi.mock('@sentry/utils', async () => {
const original = (await vi.importActual('@sentry/utils')) as any;
return {
...original,
addExceptionMechanism: (...args: unknown[]) => mockAddExceptionMechanism(...args),
Expand Down Expand Up @@ -68,15 +69,15 @@ describe('handleError', () => {
it('calls captureException', async () => {
const wrappedHandleError = handleErrorWithSentry(handleError);
const mockError = new Error('test');
const returnVal = await wrappedHandleError({ error: mockError, event: navigationEvent });
const returnVal = (await wrappedHandleError({ error: mockError, event: navigationEvent })) as any;

expect(returnVal!.message).toEqual('Whoops!');
expect(returnVal.message).toEqual('Whoops!');
expect(mockCaptureException).toHaveBeenCalledTimes(1);
expect(mockCaptureException).toHaveBeenCalledWith(mockError, expect.any(Function));
});

it('adds an exception mechanism', async () => {
const addEventProcessorSpy = jest.spyOn(mockScope, 'addEventProcessor').mockImplementationOnce(callback => {
const addEventProcessorSpy = vi.spyOn(mockScope, 'addEventProcessor').mockImplementationOnce(callback => {
void callback({}, { event_id: 'fake-event-id' });
return mockScope;
});
Expand Down
6 changes: 4 additions & 2 deletions packages/sveltekit/test/client/sdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { getCurrentHub } from '@sentry/core';
import * as SentrySvelte from '@sentry/svelte';
import { SDK_VERSION, WINDOW } from '@sentry/svelte';
import { vi } from 'vitest';

import { init } from '../../src/client/sdk';
const svelteInit = jest.spyOn(SentrySvelte, 'init');

const svelteInit = vi.spyOn(SentrySvelte, 'init');

describe('Sentry client SDK', () => {
describe('init', () => {
afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
WINDOW.__SENTRY__.hub = undefined;
});

Expand Down
15 changes: 11 additions & 4 deletions packages/sveltekit/test/config/vitePlugins.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import * as fs from 'fs';
import type * as fs from 'fs';
import { vi } from 'vitest';

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

vi.mock('fs', async () => {
const original = await vi.importActual<typeof fs>('fs');
return {
...original,
existsSync: vi.fn().mockReturnValue(true),
};
});

describe('injectSentryInitPlugin', () => {
it('has its basic properties set', () => {
expect(injectSentryInitPlugin.name).toBe('sentry-init-injection-plugin');
expect(injectSentryInitPlugin.enforce).toBe('pre');
expect(typeof injectSentryInitPlugin.transform).toBe('function');
});

describe('tansform', () => {
jest.spyOn(fs, 'existsSync').mockReturnValue(true);

describe('transform', () => {
it('transforms the server index file', () => {
const code = 'foo();';
const id = '/node_modules/@sveltejs/kit/src/runtime/server/index.js';
Expand Down
19 changes: 10 additions & 9 deletions packages/sveltekit/test/server/handleError.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import { Scope } from '@sentry/node';
// adding a custom resolver, which will take too much time.
// eslint-disable-next-line import/no-unresolved
import type { HandleServerError, RequestEvent } from '@sveltejs/kit';
import { vi } from 'vitest';

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

const mockCaptureException = jest.fn();
const mockCaptureException = vi.fn();
let mockScope = new Scope();

jest.mock('@sentry/node', () => {
const original = jest.requireActual('@sentry/core');
vi.mock('@sentry/node', async () => {
const original = (await vi.importActual('@sentry/core')) as any;
return {
...original,
captureException: (err: unknown, cb: (arg0: unknown) => unknown) => {
Expand All @@ -22,10 +23,10 @@ jest.mock('@sentry/node', () => {
};
});

const mockAddExceptionMechanism = jest.fn();
const mockAddExceptionMechanism = vi.fn();

jest.mock('@sentry/utils', () => {
const original = jest.requireActual('@sentry/utils');
vi.mock('@sentry/utils', async () => {
const original = (await vi.importActual('@sentry/utils')) as any;
return {
...original,
addExceptionMechanism: (...args: unknown[]) => mockAddExceptionMechanism(...args),
Expand Down Expand Up @@ -60,15 +61,15 @@ describe('handleError', () => {
it('calls captureException', async () => {
const wrappedHandleError = handleErrorWithSentry(handleError);
const mockError = new Error('test');
const returnVal = await wrappedHandleError({ error: mockError, event: requestEvent });
const returnVal = (await wrappedHandleError({ error: mockError, event: requestEvent })) as any;

expect(returnVal!.message).toEqual('Whoops!');
expect(returnVal.message).toEqual('Whoops!');
expect(mockCaptureException).toHaveBeenCalledTimes(1);
expect(mockCaptureException).toHaveBeenCalledWith(mockError, expect.any(Function));
});

it('adds an exception mechanism', async () => {
const addEventProcessorSpy = jest.spyOn(mockScope, 'addEventProcessor').mockImplementationOnce(callback => {
const addEventProcessorSpy = vi.spyOn(mockScope, 'addEventProcessor').mockImplementationOnce(callback => {
void callback({}, { event_id: 'fake-event-id' });
return mockScope;
});
Expand Down
4 changes: 2 additions & 2 deletions packages/sveltekit/test/server/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { GLOBAL_OBJ } from '@sentry/utils';

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

const nodeInit = jest.spyOn(SentryNode, 'init');
const nodeInit = vi.spyOn(SentryNode, 'init');

describe('Sentry server SDK', () => {
describe('init', () => {
afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
GLOBAL_OBJ.__SENTRY__.hub = undefined;
});

Expand Down
2 changes: 1 addition & 1 deletion packages/sveltekit/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../../tsconfig.json",

"include": ["src/**/*"],
"include": ["src/**/*", "vite.config.ts"],

"compilerOptions": {
// package-specific options
Expand Down
2 changes: 1 addition & 1 deletion packages/sveltekit/tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

"compilerOptions": {
// should include all types from `./tsconfig.json` plus types for all test frameworks used
"types": ["node", "jest"]
"types": ["node", "vitest/globals"]
}
}
3 changes: 3 additions & 0 deletions packages/sveltekit/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import baseConfig from '../../vite/vite.config';

export default baseConfig;
4 changes: 2 additions & 2 deletions tsconfig.dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
{
"extends": "./tsconfig.json",

"include": ["**/scripts/**/*.ts", "jest/**/*.ts"],
"include": ["**/scripts/**/*.ts", "jest/**/*.ts", "vite/**/*.ts"],

"compilerOptions": {
"types": ["node", "jest"],
"types": ["node", "jest", "vitest/globals"]
}
}
17 changes: 17 additions & 0 deletions vite/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { defineConfig } from 'vitest/config';

export default defineConfig({
define: {
__DEBUG_BUILD__: true,
},
test: {
globals: true,
coverage: {
enabled: true,
reportsDirectory: './coverage',
},
typecheck: {
tsconfig: './tsconfig.test.json',
},
},
});
Loading

0 comments on commit ba99e7c

Please sign in to comment.