Skip to content

Commit 189d5eb

Browse files
authored
test(node): Migrate to vitest (#15478)
1 parent 14667ee commit 189d5eb

30 files changed

+468
-380
lines changed

packages/node/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@
119119
"clean": "rimraf build coverage sentry-node-*.tgz",
120120
"fix": "eslint . --format stylish --fix",
121121
"lint": "eslint . --format stylish",
122-
"test": "yarn test:jest",
123-
"test:jest": "jest",
124-
"test:watch": "jest --watch",
122+
"test": "yarn test:unit",
123+
"test:unit": "vitest run",
124+
"test:watch": "vitest --watch",
125125
"yalc:publish": "yalc publish --push --sig"
126126
},
127127
"volta": {

packages/node/test/cron.test.ts

Lines changed: 90 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import * as SentryCore from '@sentry/core';
22

3+
import { type MockInstance, afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
34
import { cron } from '../src';
45
import type { CronJob, CronJobParams } from '../src/cron/cron';
56
import type { NodeCron, NodeCronOptions } from '../src/cron/node-cron';
67

78
describe('cron check-ins', () => {
8-
let withMonitorSpy: jest.SpyInstance;
9+
let withMonitorSpy: MockInstance;
910

1011
beforeEach(() => {
11-
withMonitorSpy = jest.spyOn(SentryCore, 'withMonitor');
12+
withMonitorSpy = vi.spyOn(SentryCore, 'withMonitor');
1213
});
1314

1415
afterEach(() => {
15-
jest.restoreAllMocks();
16+
vi.restoreAllMocks();
1617
});
1718

1819
describe('cron', () => {
@@ -48,43 +49,45 @@ describe('cron check-ins', () => {
4849
}
4950
}
5051

51-
test('new CronJob()', done => {
52-
expect.assertions(4);
53-
54-
const CronJobWithCheckIn = cron.instrumentCron(CronJobMock, 'my-cron-job');
55-
56-
new CronJobWithCheckIn(
57-
'* * * Jan,Sep Sun',
58-
() => {
59-
expect(withMonitorSpy).toHaveBeenCalledTimes(1);
60-
expect(withMonitorSpy).toHaveBeenLastCalledWith('my-cron-job', expect.anything(), {
61-
schedule: { type: 'crontab', value: '* * * 1,9 0' },
62-
timezone: 'America/Los_Angeles',
63-
});
64-
done();
65-
},
66-
undefined,
67-
true,
68-
'America/Los_Angeles',
69-
);
70-
});
52+
test('new CronJob()', () =>
53+
new Promise<void>(done => {
54+
expect.assertions(4);
55+
56+
const CronJobWithCheckIn = cron.instrumentCron(CronJobMock, 'my-cron-job');
57+
58+
new CronJobWithCheckIn(
59+
'* * * Jan,Sep Sun',
60+
() => {
61+
expect(withMonitorSpy).toHaveBeenCalledTimes(1);
62+
expect(withMonitorSpy).toHaveBeenLastCalledWith('my-cron-job', expect.anything(), {
63+
schedule: { type: 'crontab', value: '* * * 1,9 0' },
64+
timezone: 'America/Los_Angeles',
65+
});
66+
done();
67+
},
68+
undefined,
69+
true,
70+
'America/Los_Angeles',
71+
);
72+
}));
7173

72-
test('CronJob.from()', done => {
73-
expect.assertions(4);
74+
test('CronJob.from()', () =>
75+
new Promise<void>(done => {
76+
expect.assertions(4);
7477

75-
const CronJobWithCheckIn = cron.instrumentCron(CronJobMock, 'my-cron-job');
78+
const CronJobWithCheckIn = cron.instrumentCron(CronJobMock, 'my-cron-job');
7679

77-
CronJobWithCheckIn.from({
78-
cronTime: '* * * Jan,Sep Sun',
79-
onTick: () => {
80-
expect(withMonitorSpy).toHaveBeenCalledTimes(1);
81-
expect(withMonitorSpy).toHaveBeenLastCalledWith('my-cron-job', expect.anything(), {
82-
schedule: { type: 'crontab', value: '* * * 1,9 0' },
83-
});
84-
done();
85-
},
86-
});
87-
});
80+
CronJobWithCheckIn.from({
81+
cronTime: '* * * Jan,Sep Sun',
82+
onTick: () => {
83+
expect(withMonitorSpy).toHaveBeenCalledTimes(1);
84+
expect(withMonitorSpy).toHaveBeenLastCalledWith('my-cron-job', expect.anything(), {
85+
schedule: { type: 'crontab', value: '* * * 1,9 0' },
86+
});
87+
done();
88+
},
89+
});
90+
}));
8891

8992
test('throws with multiple jobs same name', () => {
9093
const CronJobWithCheckIn = cron.instrumentCron(CronJobMock, 'my-cron-job');
@@ -108,32 +111,33 @@ describe('cron check-ins', () => {
108111
});
109112

110113
describe('node-cron', () => {
111-
test('calls withMonitor', done => {
112-
expect.assertions(5);
113-
114-
const nodeCron: NodeCron = {
115-
schedule: (expression: string, callback: () => void, options?: NodeCronOptions): unknown => {
116-
expect(expression).toBe('* * * Jan,Sep Sun');
117-
expect(callback).toBeInstanceOf(Function);
118-
expect(options?.name).toBe('my-cron-job');
119-
return callback();
120-
},
121-
};
122-
123-
const cronWithCheckIn = cron.instrumentNodeCron(nodeCron);
124-
125-
cronWithCheckIn.schedule(
126-
'* * * Jan,Sep Sun',
127-
() => {
128-
expect(withMonitorSpy).toHaveBeenCalledTimes(1);
129-
expect(withMonitorSpy).toHaveBeenLastCalledWith('my-cron-job', expect.anything(), {
130-
schedule: { type: 'crontab', value: '* * * 1,9 0' },
131-
});
132-
done();
133-
},
134-
{ name: 'my-cron-job' },
135-
);
136-
});
114+
test('calls withMonitor', () =>
115+
new Promise<void>(done => {
116+
expect.assertions(5);
117+
118+
const nodeCron: NodeCron = {
119+
schedule: (expression: string, callback: () => void, options?: NodeCronOptions): unknown => {
120+
expect(expression).toBe('* * * Jan,Sep Sun');
121+
expect(callback).toBeInstanceOf(Function);
122+
expect(options?.name).toBe('my-cron-job');
123+
return callback();
124+
},
125+
};
126+
127+
const cronWithCheckIn = cron.instrumentNodeCron(nodeCron);
128+
129+
cronWithCheckIn.schedule(
130+
'* * * Jan,Sep Sun',
131+
() => {
132+
expect(withMonitorSpy).toHaveBeenCalledTimes(1);
133+
expect(withMonitorSpy).toHaveBeenLastCalledWith('my-cron-job', expect.anything(), {
134+
schedule: { type: 'crontab', value: '* * * 1,9 0' },
135+
});
136+
done();
137+
},
138+
{ name: 'my-cron-job' },
139+
);
140+
}));
137141

138142
test('throws without supplied name', () => {
139143
const nodeCron: NodeCron = {
@@ -154,32 +158,33 @@ describe('cron check-ins', () => {
154158
});
155159

156160
describe('node-schedule', () => {
157-
test('calls withMonitor', done => {
158-
expect.assertions(5);
159-
160-
class NodeScheduleMock {
161-
scheduleJob(
162-
nameOrExpression: string | Date | object,
163-
expressionOrCallback: string | Date | object | (() => void),
164-
callback: () => void,
165-
): unknown {
166-
expect(nameOrExpression).toBe('my-cron-job');
167-
expect(expressionOrCallback).toBe('* * * Jan,Sep Sun');
168-
expect(callback).toBeInstanceOf(Function);
169-
return callback();
161+
test('calls withMonitor', () =>
162+
new Promise<void>(done => {
163+
expect.assertions(5);
164+
165+
class NodeScheduleMock {
166+
scheduleJob(
167+
nameOrExpression: string | Date | object,
168+
expressionOrCallback: string | Date | object | (() => void),
169+
callback: () => void,
170+
): unknown {
171+
expect(nameOrExpression).toBe('my-cron-job');
172+
expect(expressionOrCallback).toBe('* * * Jan,Sep Sun');
173+
expect(callback).toBeInstanceOf(Function);
174+
return callback();
175+
}
170176
}
171-
}
172177

173-
const scheduleWithCheckIn = cron.instrumentNodeSchedule(new NodeScheduleMock());
178+
const scheduleWithCheckIn = cron.instrumentNodeSchedule(new NodeScheduleMock());
174179

175-
scheduleWithCheckIn.scheduleJob('my-cron-job', '* * * Jan,Sep Sun', () => {
176-
expect(withMonitorSpy).toHaveBeenCalledTimes(1);
177-
expect(withMonitorSpy).toHaveBeenLastCalledWith('my-cron-job', expect.anything(), {
178-
schedule: { type: 'crontab', value: '* * * 1,9 0' },
180+
scheduleWithCheckIn.scheduleJob('my-cron-job', '* * * Jan,Sep Sun', () => {
181+
expect(withMonitorSpy).toHaveBeenCalledTimes(1);
182+
expect(withMonitorSpy).toHaveBeenLastCalledWith('my-cron-job', expect.anything(), {
183+
schedule: { type: 'crontab', value: '* * * 1,9 0' },
184+
});
185+
done();
179186
});
180-
done();
181-
});
182-
});
187+
}));
183188

184189
test('throws without crontab string', () => {
185190
class NodeScheduleMock {

packages/node/test/helpers/conditional.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { parseSemver } from '@sentry/core';
2+
import { it, test } from 'vitest';
23

34
const NODE_VERSION = parseSemver(process.versions.node).major;
45

@@ -8,7 +9,7 @@ const NODE_VERSION = parseSemver(process.versions.node).major;
89
* @param {{ min?: number; max?: number }} allowedVersion
910
* @return {*} {jest.Describe}
1011
*/
11-
export const conditionalTest = (allowedVersion: { min?: number; max?: number }): jest.It => {
12+
export const conditionalTest = (allowedVersion: { min?: number; max?: number }) => {
1213
if (!NODE_VERSION) {
1314
return it.skip;
1415
}

packages/node/test/integration/breadcrumbs.test.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@ import { startSpan } from '@sentry/opentelemetry';
33
import { getClient } from '../../src/';
44
import type { NodeClient } from '../../src/sdk/client';
55

6+
import { afterEach, describe, expect, it, vi } from 'vitest';
67
import { cleanupOtel, mockSdkInit } from '../helpers/mockSdkInit';
78

89
describe('Integration | breadcrumbs', () => {
9-
const beforeSendTransaction = jest.fn(() => null);
10+
const beforeSendTransaction = vi.fn(() => null);
1011

1112
afterEach(() => {
1213
cleanupOtel();
1314
});
1415

1516
describe('without tracing', () => {
1617
it('correctly adds & retrieves breadcrumbs', async () => {
17-
const beforeSend = jest.fn(() => null);
18-
const beforeBreadcrumb = jest.fn(breadcrumb => breadcrumb);
18+
const beforeSend = vi.fn(() => null);
19+
const beforeBreadcrumb = vi.fn(breadcrumb => breadcrumb);
1920

2021
mockSdkInit({ beforeSend, beforeBreadcrumb });
2122

@@ -50,8 +51,8 @@ describe('Integration | breadcrumbs', () => {
5051
});
5152

5253
it('handles parallel scopes', async () => {
53-
const beforeSend = jest.fn(() => null);
54-
const beforeBreadcrumb = jest.fn(breadcrumb => breadcrumb);
54+
const beforeSend = vi.fn(() => null);
55+
const beforeBreadcrumb = vi.fn(breadcrumb => breadcrumb);
5556

5657
mockSdkInit({ beforeSend, beforeBreadcrumb });
5758

@@ -96,8 +97,8 @@ describe('Integration | breadcrumbs', () => {
9697
});
9798

9899
it('correctly adds & retrieves breadcrumbs', async () => {
99-
const beforeSend = jest.fn(() => null);
100-
const beforeBreadcrumb = jest.fn(breadcrumb => breadcrumb);
100+
const beforeSend = vi.fn(() => null);
101+
const beforeBreadcrumb = vi.fn(breadcrumb => breadcrumb);
101102

102103
mockSdkInit({ beforeSend, beforeBreadcrumb, beforeSendTransaction, tracesSampleRate: 1 });
103104

@@ -141,8 +142,8 @@ describe('Integration | breadcrumbs', () => {
141142
});
142143

143144
it('correctly adds & retrieves breadcrumbs for the current isolation span only', async () => {
144-
const beforeSend = jest.fn(() => null);
145-
const beforeBreadcrumb = jest.fn(breadcrumb => breadcrumb);
145+
const beforeSend = vi.fn(() => null);
146+
const beforeBreadcrumb = vi.fn(breadcrumb => breadcrumb);
146147

147148
mockSdkInit({ beforeSend, beforeBreadcrumb, beforeSendTransaction, tracesSampleRate: 1 });
148149

@@ -193,8 +194,8 @@ describe('Integration | breadcrumbs', () => {
193194
});
194195

195196
it('ignores scopes inside of root span', async () => {
196-
const beforeSend = jest.fn(() => null);
197-
const beforeBreadcrumb = jest.fn(breadcrumb => breadcrumb);
197+
const beforeSend = vi.fn(() => null);
198+
const beforeBreadcrumb = vi.fn(breadcrumb => breadcrumb);
198199

199200
mockSdkInit({ beforeSend, beforeBreadcrumb, beforeSendTransaction, tracesSampleRate: 1 });
200201

@@ -234,8 +235,8 @@ describe('Integration | breadcrumbs', () => {
234235
});
235236

236237
it('handles deep nesting of scopes', async () => {
237-
const beforeSend = jest.fn(() => null);
238-
const beforeBreadcrumb = jest.fn(breadcrumb => breadcrumb);
238+
const beforeSend = vi.fn(() => null);
239+
const beforeBreadcrumb = vi.fn(breadcrumb => breadcrumb);
239240

240241
mockSdkInit({ beforeSend, beforeBreadcrumb, beforeSendTransaction, tracesSampleRate: 1 });
241242

@@ -292,8 +293,8 @@ describe('Integration | breadcrumbs', () => {
292293
});
293294

294295
it('correctly adds & retrieves breadcrumbs in async spans', async () => {
295-
const beforeSend = jest.fn(() => null);
296-
const beforeBreadcrumb = jest.fn(breadcrumb => breadcrumb);
296+
const beforeSend = vi.fn(() => null);
297+
const beforeBreadcrumb = vi.fn(breadcrumb => breadcrumb);
297298

298299
mockSdkInit({ beforeSend, beforeBreadcrumb, beforeSendTransaction, tracesSampleRate: 1 });
299300

packages/node/test/integration/console.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import * as SentryCore from '@sentry/core';
22
import { resetInstrumentationHandlers } from '@sentry/core';
3+
import { afterEach, describe, expect, it, vi } from 'vitest';
34
import { getClient } from '../../src';
45
import type { NodeClient } from '../../src';
56
import { consoleIntegration } from '../../src/integrations/console';
67

7-
const addBreadcrumbSpy = jest.spyOn(SentryCore, 'addBreadcrumb');
8+
const addBreadcrumbSpy = vi.spyOn(SentryCore, 'addBreadcrumb');
89

9-
jest.spyOn(console, 'log').mockImplementation(() => {
10+
vi.spyOn(console, 'log').mockImplementation(() => {
1011
// noop so that we don't spam the logs
1112
});
1213

1314
afterEach(() => {
14-
jest.clearAllMocks();
15+
vi.clearAllMocks();
1516
resetInstrumentationHandlers();
1617
});
1718

0 commit comments

Comments
 (0)