Skip to content

test(vue): Use vitest #11071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/vue/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,12 @@ module.exports = {
browser: true,
},
extends: ['../../.eslintrc.js'],
overrides: [
{
files: ['vite.config.ts'],
parserOptions: {
project: ['tsconfig.test.json'],
},
},
]
};
4 changes: 2 additions & 2 deletions packages/vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@
"clean": "rimraf build coverage sentry-vue-*.tgz",
"fix": "eslint . --format stylish --fix",
"lint": "eslint . --format stylish",
"test": "jest",
"test:watch": "jest --watch",
"test": "vitest run",
"test:watch": "vitest --watch",
"yalc:publish": "ts-node ../../scripts/prepack.ts && yalc publish build --push --sig"
},
"volta": {
Expand Down
17 changes: 9 additions & 8 deletions packages/vue/test/errorHandler.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { setCurrentClient } from '@sentry/browser';
import { afterEach, describe, expect, test, vi } from 'vitest';

import { attachErrorHandler } from '../src/errorhandler';
import type { Operation, Options, ViewModel, Vue } from '../src/types';
Expand All @@ -7,7 +8,7 @@ import { generateComponentTrace } from '../src/vendor/components';
describe('attachErrorHandler', () => {
describe('attachProps', () => {
afterEach(() => {
jest.resetAllMocks();
vi.resetAllMocks();
});

describe("given I don't want to `attachProps`", () => {
Expand Down Expand Up @@ -325,21 +326,21 @@ const testHarness = ({
enableConsole,
vm,
}: TestHarnessOpts) => {
jest.useFakeTimers();
const providedErrorHandlerSpy = jest.fn();
const warnHandlerSpy = jest.fn();
const consoleErrorSpy = jest.fn();
vi.useFakeTimers();
const providedErrorHandlerSpy = vi.fn();
const warnHandlerSpy = vi.fn();
const consoleErrorSpy = vi.fn();

const client: any = {
captureException: jest.fn(async () => Promise.resolve()),
captureException: vi.fn(async () => Promise.resolve()),
};
setCurrentClient(client);

const app: Vue = {
config: {
silent: !!silent,
},
mixin: jest.fn(),
mixin: vi.fn(),
};

if (enableErrorHandler) {
Expand Down Expand Up @@ -380,7 +381,7 @@ const testHarness = ({
app.config.errorHandler(new DummyError(), vm, 'stub-lifecycle-hook');

// and waits for internal timers
jest.runAllTimers();
vi.runAllTimers();
},
expect: {
errorHandlerSpy: expect(providedErrorHandlerSpy),
Expand Down
14 changes: 8 additions & 6 deletions packages/vue/test/integration/VueIntegration.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Client } from '@sentry/types';
import { logger } from '@sentry/utils';
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { createApp } from 'vue';

import * as Sentry from '../../src';
Expand All @@ -15,10 +16,11 @@ describe('Sentry.VueIntegration', () => {
const globalRequest = globalThis.Request;

beforeAll(() => {
globalThis.fetch = jest.fn();
// @ts-expect-error This is a mock
globalThis.Response = jest.fn();
globalThis.Request = jest.fn();
globalThis.fetch = vi.fn();
// @ts-expect-error This is a mock
globalThis.Response = vi.fn();
globalThis.Request = vi.fn();
});

afterAll(() => {
Expand All @@ -31,17 +33,17 @@ describe('Sentry.VueIntegration', () => {
warnings = [];
loggerWarnings = [];

jest.spyOn(logger, 'warn').mockImplementation((message: unknown) => {
vi.spyOn(logger, 'warn').mockImplementation((message: unknown) => {
loggerWarnings.push(message);
});

jest.spyOn(console, 'warn').mockImplementation((message: unknown) => {
vi.spyOn(console, 'warn').mockImplementation((message: unknown) => {
warnings.push(message);
});
});

afterEach(() => {
jest.resetAllMocks();
vi.resetAllMocks();
});

it('allows to initialize integration later', () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/vue/test/integration/init.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { createApp } from 'vue';

import type { Options } from '../../src/types';
Expand All @@ -10,13 +11,13 @@ describe('Sentry.init', () => {

beforeEach(() => {
warnings = [];
jest.spyOn(console, 'warn').mockImplementation((message: unknown) => {
vi.spyOn(console, 'warn').mockImplementation((message: unknown) => {
warnings.push(message);
});
});

afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

it('does not warn when correctly setup (Vue 3)', () => {
Expand Down
64 changes: 33 additions & 31 deletions packages/vue/test/router.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { afterEach, describe, expect, it, test, vi } from 'vitest';

import * as SentryBrowser from '@sentry/browser';
import * as SentryCore from '@sentry/core';
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core';
Expand All @@ -6,21 +8,21 @@ import type { Span, SpanAttributes } from '@sentry/types';
import type { Route } from '../src/router';
import { instrumentVueRouter } from '../src/router';

const captureExceptionSpy = jest.spyOn(SentryBrowser, 'captureException');
jest.mock('@sentry/core', () => {
const actual = jest.requireActual('@sentry/core');
const captureExceptionSpy = vi.spyOn(SentryBrowser, 'captureException');
vi.mock('@sentry/core', async (importOriginal) => {
const actual = await importOriginal<typeof SentryCore>()
return {
...actual,
getActiveSpan: jest.fn().mockReturnValue({}),
getActiveSpan: vi.fn().mockReturnValue({}),
};
});

const mockVueRouter = {
onError: jest.fn<void, [(error: Error) => void]>(),
beforeEach: jest.fn<void, [(from: Route, to: Route, next?: () => void) => void]>(),
onError: vi.fn<any, [(error: Error) => void]>(),
beforeEach: vi.fn<any, [(from: Route, to: Route, next?: () => void) => void]>(),
};

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

const testRoutes: Record<string, Route> = {
initialPageloadRoute: { matched: [], params: {}, path: '', query: {} },
Expand Down Expand Up @@ -60,11 +62,11 @@ const testRoutes: Record<string, Route> = {

describe('instrumentVueRouter()', () => {
afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

it('should return instrumentation that instruments VueRouter.onError', () => {
const mockStartSpan = jest.fn();
const mockStartSpan = vi.fn();
instrumentVueRouter(
mockVueRouter,
{ routeLabel: 'name', instrumentPageLoad: true, instrumentNavigation: true },
Expand All @@ -90,7 +92,7 @@ describe('instrumentVueRouter()', () => {
])(
'should return instrumentation that instruments VueRouter.beforeEach(%s, %s) for navigations',
(fromKey, toKey, transactionName, transactionSource) => {
const mockStartSpan = jest.fn();
const mockStartSpan = vi.fn();
instrumentVueRouter(
mockVueRouter,
{ routeLabel: 'name', instrumentPageLoad: true, instrumentNavigation: true },
Expand Down Expand Up @@ -128,15 +130,15 @@ describe('instrumentVueRouter()', () => {
'should return instrumentation that instruments VueRouter.beforeEach(%s, %s) for pageloads',
(fromKey, toKey, transactionName, transactionSource) => {
const mockRootSpan = {
getSpanJSON: jest.fn().mockReturnValue({ op: 'pageload' }),
updateName: jest.fn(),
setAttribute: jest.fn(),
setAttributes: jest.fn(),
getSpanJSON: vi.fn().mockReturnValue({ op: 'pageload' }),
updateName: vi.fn(),
setAttribute: vi.fn(),
setAttributes: vi.fn(),
};

jest.spyOn(SentryCore, 'getRootSpan').mockImplementation(() => mockRootSpan as unknown as Span);
vi.spyOn(SentryCore, 'getRootSpan').mockImplementation(() => mockRootSpan as unknown as Span);

const mockStartSpan = jest.fn().mockImplementation(_ => {
const mockStartSpan = vi.fn().mockImplementation(_ => {
return mockRootSpan;
});
instrumentVueRouter(
Expand Down Expand Up @@ -168,7 +170,7 @@ describe('instrumentVueRouter()', () => {
);

it('allows to configure routeLabel=path', () => {
const mockStartSpan = jest.fn();
const mockStartSpan = vi.fn();
instrumentVueRouter(
mockVueRouter,
{ routeLabel: 'path', instrumentPageLoad: true, instrumentNavigation: true },
Expand All @@ -195,7 +197,7 @@ describe('instrumentVueRouter()', () => {
});

it('allows to configure routeLabel=name', () => {
const mockStartSpan = jest.fn();
const mockStartSpan = vi.fn();
instrumentVueRouter(
mockVueRouter,
{ routeLabel: 'name', instrumentPageLoad: true, instrumentNavigation: true },
Expand Down Expand Up @@ -223,9 +225,9 @@ describe('instrumentVueRouter()', () => {

it("doesn't overwrite a pageload transaction name it was set to custom before the router resolved the route", () => {
const mockRootSpan = {
updateName: jest.fn(),
setAttribute: jest.fn(),
setAttributes: jest.fn(),
updateName: vi.fn(),
setAttribute: vi.fn(),
setAttributes: vi.fn(),
name: '',
getSpanJSON: () => ({
op: 'pageload',
Expand All @@ -234,10 +236,10 @@ describe('instrumentVueRouter()', () => {
},
}),
};
const mockStartSpan = jest.fn().mockImplementation(_ => {
const mockStartSpan = vi.fn().mockImplementation(_ => {
return mockRootSpan;
});
jest.spyOn(SentryCore, 'getRootSpan').mockImplementation(() => mockRootSpan as unknown as Span);
vi.spyOn(SentryCore, 'getRootSpan').mockImplementation(() => mockRootSpan as unknown as Span);

instrumentVueRouter(
mockVueRouter,
Expand Down Expand Up @@ -283,10 +285,10 @@ describe('instrumentVueRouter()', () => {
'should return instrumentation that considers the instrumentPageLoad = %p',
(instrumentPageLoad, expectedCallsAmount) => {
const mockRootSpan = {
updateName: jest.fn(),
setData: jest.fn(),
setAttribute: jest.fn(),
setAttributes: jest.fn(),
updateName: vi.fn(),
setData: vi.fn(),
setAttribute: vi.fn(),
setAttributes: vi.fn(),
name: '',
getSpanJSON: () => ({
op: 'pageload',
Expand All @@ -295,9 +297,9 @@ describe('instrumentVueRouter()', () => {
},
}),
};
jest.spyOn(SentryCore, 'getRootSpan').mockImplementation(() => mockRootSpan as unknown as Span);
vi.spyOn(SentryCore, 'getRootSpan').mockImplementation(() => mockRootSpan as unknown as Span);

const mockStartSpan = jest.fn();
const mockStartSpan = vi.fn();
instrumentVueRouter(
mockVueRouter,
{ routeLabel: 'name', instrumentPageLoad, instrumentNavigation: true },
Expand All @@ -321,7 +323,7 @@ describe('instrumentVueRouter()', () => {
])(
'should return instrumentation that considers the instrumentNavigation = %p',
(instrumentNavigation, expectedCallsAmount) => {
const mockStartSpan = jest.fn();
const mockStartSpan = vi.fn();
instrumentVueRouter(
mockVueRouter,
{ routeLabel: 'name', instrumentPageLoad: true, instrumentNavigation },
Expand All @@ -339,7 +341,7 @@ describe('instrumentVueRouter()', () => {
);

it("doesn't throw when `next` is not available in the beforeEach callback (Vue Router 4)", () => {
const mockStartSpan = jest.fn();
const mockStartSpan = vi.fn();
instrumentVueRouter(
mockVueRouter,
{ routeLabel: 'path', instrumentPageLoad: true, instrumentNavigation: true },
Expand Down
1 change: 1 addition & 0 deletions packages/vue/test/vendor/components.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { beforeEach, describe, expect, it } from 'vitest';
import { formatComponentName } from '../../src/vendor/components';

describe('formatComponentName', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/vue/tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"extends": "./tsconfig.json",

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

"compilerOptions": {
// should include all types from `./tsconfig.json` plus types for all test frameworks used
"types": ["jest"]
"types": []

// other package-specific, test-specific options
}
Expand Down
13 changes: 13 additions & 0 deletions packages/vue/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { UserConfig } from 'vitest';

import baseConfig from '../../vite/vite.config';

export default {
...baseConfig,
test: {
// test exists, no idea why TS doesn't recognize it
// eslint-disable-next-line @typescript-eslint/no-explicit-any
...(baseConfig as UserConfig & { test: any }).test,
environment: 'jsdom',
},
};