Skip to content

Commit 2cd8283

Browse files
mydeaandreiborza
andcommitted
test(node): Add test to show how re-init works (#12016)
Just a test showing how stuff behaves if: 1. `init` is called without DSN 2. `httpIntegration` is manually added 3. `init` is called again, later, with DSN --------- Co-authored-by: Andrei <[email protected]>
1 parent 723f987 commit 2cd8283

File tree

2 files changed

+120
-0
lines changed
  • dev-packages/node-integration-tests/suites/express/multiple-init

2 files changed

+120
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { loggingTransport } from '@sentry-internal/node-integration-tests';
2+
import * as Sentry from '@sentry/node';
3+
4+
Sentry.init({
5+
// No dsn, means client is disabled
6+
// dsn: 'https://[email protected]/1337',
7+
release: '1.0',
8+
transport: loggingTransport,
9+
});
10+
11+
// We add http integration to ensure request isolation etc. works
12+
const initialClient = Sentry.getClient();
13+
initialClient?.addIntegration(Sentry.httpIntegration());
14+
15+
// Store this so we can update the client later
16+
const initialCurrentScope = Sentry.getCurrentScope();
17+
18+
import { startExpressServerAndSendPortToRunner } from '@sentry-internal/node-integration-tests';
19+
import express from 'express';
20+
21+
const app = express();
22+
23+
Sentry.setTag('global', 'tag');
24+
25+
app.get('/test/no-init', (_req, res) => {
26+
Sentry.addBreadcrumb({ message: 'no init breadcrumb' });
27+
Sentry.setTag('no-init', 'tag');
28+
29+
res.send({});
30+
});
31+
32+
app.get('/test/init', (_req, res) => {
33+
// Call init again, but with DSN
34+
Sentry.init({
35+
dsn: 'https://[email protected]/1337',
36+
release: '1.0',
37+
transport: loggingTransport,
38+
});
39+
// Set this on initial scope, to ensure it can be inherited
40+
initialCurrentScope.setClient(Sentry.getClient()!);
41+
42+
Sentry.addBreadcrumb({ message: 'init breadcrumb' });
43+
Sentry.setTag('init', 'tag');
44+
45+
res.send({});
46+
});
47+
48+
app.get('/test/error/:id', (req, res) => {
49+
const id = req.params.id;
50+
Sentry.addBreadcrumb({ message: `error breadcrumb ${id}` });
51+
Sentry.setTag('error', id);
52+
53+
Sentry.captureException(new Error(`This is an exception ${id}`));
54+
55+
res.send({});
56+
});
57+
58+
Sentry.setupExpressErrorHandler(app);
59+
60+
startExpressServerAndSendPortToRunner(app);
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';
2+
3+
afterAll(() => {
4+
cleanupChildProcesses();
5+
});
6+
7+
test('allows to call init multiple times', done => {
8+
const runner = createRunner(__dirname, 'server.ts')
9+
.ignore('session', 'sessions')
10+
.expect({
11+
event: {
12+
exception: {
13+
values: [
14+
{
15+
value: 'This is an exception 2',
16+
},
17+
],
18+
},
19+
breadcrumbs: [
20+
{
21+
message: 'error breadcrumb 2',
22+
timestamp: expect.any(Number),
23+
},
24+
],
25+
tags: {
26+
global: 'tag',
27+
error: '2',
28+
},
29+
},
30+
})
31+
.expect({
32+
event: {
33+
exception: {
34+
values: [
35+
{
36+
value: 'This is an exception 3',
37+
},
38+
],
39+
},
40+
breadcrumbs: [
41+
{
42+
message: 'error breadcrumb 3',
43+
timestamp: expect.any(Number),
44+
},
45+
],
46+
tags: {
47+
global: 'tag',
48+
error: '3',
49+
},
50+
},
51+
})
52+
.start(done);
53+
54+
runner
55+
.makeRequest('get', '/test/no-init')
56+
.then(() => runner.makeRequest('get', '/test/error/1'))
57+
.then(() => runner.makeRequest('get', '/test/init'))
58+
.then(() => runner.makeRequest('get', '/test/error/2'))
59+
.then(() => runner.makeRequest('get', '/test/error/3'));
60+
});

0 commit comments

Comments
 (0)