Skip to content

Commit 8ef5f09

Browse files
committed
test(node-cron): node-integration
1 parent 50509aa commit 8ef5f09

File tree

4 files changed

+106
-1
lines changed

4 files changed

+106
-1
lines changed

dev-packages/node-integration-tests/suites/cron/node-cron/scenario.ts renamed to dev-packages/node-integration-tests/suites/cron/node-cron/base/scenario.ts

File renamed without changes.

dev-packages/node-integration-tests/suites/cron/node-cron/test.ts renamed to dev-packages/node-integration-tests/suites/cron/node-cron/base/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { afterAll, expect, test } from 'vitest';
2-
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';
2+
import { cleanupChildProcesses, createRunner } from '../../../../utils/runner';
33

44
afterAll(() => {
55
cleanupChildProcesses();
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import * as Sentry from '@sentry/node';
2+
import { loggingTransport } from '@sentry-internal/node-integration-tests';
3+
import * as cron from 'node-cron';
4+
5+
Sentry.init({
6+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
7+
release: '1.0',
8+
transport: loggingTransport,
9+
});
10+
11+
const cronWithCheckIn = Sentry.cron.instrumentNodeCron(cron, { isolateTrace: true });
12+
13+
let closeNext1 = false;
14+
let closeNext2 = false;
15+
16+
const task = cronWithCheckIn.schedule(
17+
'* * * * * *',
18+
() => {
19+
if (closeNext1) {
20+
// https://github.com/node-cron/node-cron/issues/317
21+
setImmediate(() => {
22+
task.stop();
23+
});
24+
25+
throw new Error('Error in cron job');
26+
}
27+
28+
// eslint-disable-next-line no-console
29+
console.log('You will see this message every second');
30+
closeNext1 = true;
31+
},
32+
{ name: 'my-cron-job' },
33+
);
34+
35+
const task2 = cronWithCheckIn.schedule(
36+
'* * * * * *',
37+
() => {
38+
if (closeNext2) {
39+
// https://github.com/node-cron/node-cron/issues/317
40+
setImmediate(() => {
41+
task2.stop();
42+
});
43+
44+
throw new Error('Error in cron job 2');
45+
}
46+
47+
// eslint-disable-next-line no-console
48+
console.log('You will see this message every second');
49+
closeNext2 = true;
50+
},
51+
{ name: 'my-2nd-cron-job' },
52+
);
53+
54+
setTimeout(() => {
55+
process.exit();
56+
}, 5000);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { afterAll, expect, test } from 'vitest';
2+
import { cleanupChildProcesses, createRunner } from '../../../../utils/runner';
3+
4+
afterAll(() => {
5+
cleanupChildProcesses();
6+
});
7+
8+
test('node-cron instrumentation', async () => {
9+
let firstErrorTraceId: string | undefined;
10+
11+
await createRunner(__dirname, 'scenario.ts')
12+
.ignore('check_in')
13+
.expect({
14+
event: event => {
15+
const traceId = event.contexts?.trace?.trace_id;
16+
const spanId = event.contexts?.trace?.span_id;
17+
18+
expect(traceId).toMatch(/[a-f\d]{32}/);
19+
expect(spanId).toMatch(/[a-f\d]{16}/);
20+
21+
firstErrorTraceId = traceId;
22+
23+
expect(event.exception?.values?.[0]).toMatchObject({
24+
type: 'Error',
25+
value: expect.stringMatching(/^Error in cron job( 2)?$/),
26+
mechanism: { type: 'auto.function.node-cron.instrumentNodeCron', handled: false },
27+
});
28+
},
29+
})
30+
.expect({
31+
event: event => {
32+
const traceId = event.contexts?.trace?.trace_id;
33+
const spanId = event.contexts?.trace?.span_id;
34+
35+
expect(traceId).toMatch(/[a-f\d]{32}/);
36+
expect(spanId).toMatch(/[a-f\d]{16}/);
37+
38+
expect(traceId).not.toBe(firstErrorTraceId);
39+
40+
expect(event.exception?.values?.[0]).toMatchObject({
41+
type: 'Error',
42+
value: expect.stringMatching(/^Error in cron job( 2)?$/),
43+
mechanism: { type: 'auto.function.node-cron.instrumentNodeCron', handled: false },
44+
});
45+
},
46+
})
47+
.start()
48+
.completed();
49+
});

0 commit comments

Comments
 (0)