Skip to content

Commit 270c6b6

Browse files
committed
test: Add tests to demonstrate wrong scope data loss
1 parent 0b8540f commit 270c6b6

File tree

4 files changed

+91
-0
lines changed
  • dev-packages

4 files changed

+91
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Sentry.setTag('global', 'tag');
2+
setTimeout(() => {
3+
Sentry.withScope(scope => {
4+
scope.setTag('local', 'tag');
5+
throw new Error('test error');
6+
});
7+
}, 10);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { expect } from '@playwright/test';
2+
import type { Event } from '@sentry/types';
3+
4+
import { sentryTest } from '../../../../utils/fixtures';
5+
import { getFirstSentryEnvelopeRequest } from '../../../../utils/helpers';
6+
7+
sentryTest('scope is applied to thrown error', async ({ getLocalTestPath, page }) => {
8+
const url = await getLocalTestPath({ testDir: __dirname });
9+
10+
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url);
11+
12+
const ex = eventData.exception?.values ? eventData.exception.values[0] : undefined;
13+
14+
expect(eventData.tags).toMatchObject({ global: 'tag', local: 'tag' });
15+
expect(ex?.value).toBe('test error');
16+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { loggingTransport, startExpressServerAndSendPortToRunner } from '@sentry-internal/node-integration-tests';
2+
import * as Sentry from '@sentry/node-experimental';
3+
import express from 'express';
4+
5+
const app = express();
6+
7+
Sentry.init({
8+
dsn: 'https://[email protected]/1337',
9+
release: '1.0',
10+
transport: loggingTransport,
11+
});
12+
13+
app.use(Sentry.Handlers.requestHandler());
14+
15+
Sentry.setTag('global', 'tag');
16+
17+
app.get('/test/express', () => {
18+
Sentry.withScope(scope => {
19+
scope.setTag('local', 'tag');
20+
throw new Error('test_error');
21+
})
22+
});
23+
24+
app.use(Sentry.Handlers.errorHandler());
25+
26+
startExpressServerAndSendPortToRunner(app);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';
2+
3+
afterAll(() => {
4+
cleanupChildProcesses();
5+
});
6+
7+
test('applies withScope scope to thrown error', done => {
8+
const runner = createRunner(__dirname, 'server.ts')
9+
.ignore('session', 'sessions')
10+
.expect({
11+
event: {
12+
exception: {
13+
values: [
14+
{
15+
mechanism: {
16+
type: 'middleware',
17+
handled: false,
18+
},
19+
type: 'Error',
20+
value: 'test_error',
21+
stacktrace: {
22+
frames: expect.arrayContaining([
23+
expect.objectContaining({
24+
function: expect.any(String),
25+
lineno: expect.any(Number),
26+
colno: expect.any(Number),
27+
}),
28+
]),
29+
},
30+
},
31+
],
32+
},
33+
tags: {
34+
'global': 'tag',
35+
'local': 'tag', // This tag is missing :(
36+
}
37+
},
38+
})
39+
.start(done);
40+
41+
expect(() => runner.makeRequest('get', '/test/express')).rejects.toThrow();
42+
});

0 commit comments

Comments
 (0)