Skip to content

Commit 0ea3626

Browse files
committed
test(node): Check buffered spans are sent when the process exits on its own
1 parent c81b971 commit 0ea3626

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as Sentry from '@sentry/node';
2+
import { loggingTransport } from '@sentry-internal/node-integration-tests';
3+
4+
Sentry.init({
5+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
6+
release: '1.0',
7+
tracesSampleRate: 1.0,
8+
transport: loggingTransport,
9+
});
10+
11+
Sentry.startSpan({ name: 'cron-job' }, () => {
12+
Sentry.startSpan({ name: 'step' }, () => {});
13+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { expect, test } from 'vitest';
2+
import { createRunner } from '../../../utils/runner';
3+
4+
test('sends buffered spans when the process exits on its own', async () => {
5+
await createRunner(__dirname, 'scenario.ts')
6+
.withoutAutoFlush()
7+
.expect({
8+
span: container => {
9+
expect(container.items.map(span => span.name).sort()).toEqual(['cron-job', 'step']);
10+
},
11+
})
12+
.start()
13+
.completed();
14+
});

‎dev-packages/node-integration-tests/utils/runner/createRunner.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ export function createRunner(...paths: string[]) {
180180
let withEnv: Record<string, string | undefined> = {};
181181
let withSentryServer = false;
182182
let ensureNoErrorOutput = false;
183+
let autoFlush = true;
183184
// When set, the test using this runner expects `completed()` to reject (e.g. `test.fails` variants
184185
// created via `createEsmAndCjsTests` with `failsOnEsm`/`failsOnCjs`). We suppress the captured-log
185186
// dump in that case, since the failure is expected and the output would just be noise.
@@ -330,6 +331,11 @@ export function createRunner(...paths: string[]) {
330331
unordered = true;
331332
return this;
332333
},
334+
/** Skip the injected `beforeExit` flush, so the test sees only what the SDK sends on its own. */
335+
withoutAutoFlush: function () {
336+
autoFlush = false;
337+
return this;
338+
},
333339
ensureNoErrorOutput: function () {
334340
if (expectedEnvelopes.length > 0) {
335341
throw new Error('You should not use `ensureNoErrorOutput` when using `expect`!');
@@ -630,7 +636,7 @@ export function createRunner(...paths: string[]) {
630636
// `should-exit`, `ensureNoErrorOutput`) verify the child exits
631637
// naturally and auto-flush would delay that with retrying HTTP
632638
// requests to the fake DSN.
633-
const wantsAutoFlush = !ensureNoErrorOutput && hasExpectations;
639+
const wantsAutoFlush = autoFlush && !ensureNoErrorOutput && hasExpectations;
634640
const runtime = getRuntime();
635641
const childFlags = wantsAutoFlush ? [...buildAutoFlushFlags(flags, testPath, runtime), ...flags] : flags;
636642
const entryPath = buildScenario(runtime, testPath);

0 commit comments

Comments
 (0)