Skip to content

Commit 37b14bf

Browse files
authored
test(node): Add setContext integration tests (#4787)
1 parent 398976c commit 37b14bf

File tree

6 files changed

+103
-0
lines changed

6 files changed

+103
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as Sentry from '@sentry/node';
2+
3+
Sentry.init({
4+
dsn: 'https://[email protected]/1337',
5+
release: '1.0',
6+
});
7+
8+
Sentry.setContext('context_1', {
9+
foo: 'bar',
10+
baz: {
11+
qux: 'quux',
12+
},
13+
});
14+
15+
Sentry.setContext('context_2', {
16+
1: 'foo',
17+
bar: false,
18+
});
19+
20+
Sentry.setContext('context_3', null);
21+
22+
Sentry.captureMessage('multiple_contexts');
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Event } from '@sentry/node';
2+
3+
import { assertSentryEvent, getEventRequest, runServer } from '../../../../utils';
4+
5+
test('should record multiple contexts', async () => {
6+
const url = await runServer(__dirname);
7+
const requestBody = await getEventRequest(url);
8+
9+
assertSentryEvent(requestBody, {
10+
message: 'multiple_contexts',
11+
contexts: {
12+
context_1: {
13+
foo: 'bar',
14+
baz: { qux: 'quux' },
15+
},
16+
context_2: { 1: 'foo', bar: false },
17+
},
18+
});
19+
20+
expect((requestBody as Event).contexts?.context_3).not.toBeDefined();
21+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as Sentry from '@sentry/node';
2+
3+
Sentry.init({
4+
dsn: 'https://[email protected]/1337',
5+
release: '1.0',
6+
});
7+
8+
type Circular = {
9+
self?: Circular;
10+
};
11+
12+
const objCircular: Circular = {};
13+
objCircular.self = objCircular;
14+
15+
Sentry.setContext('non_serializable', objCircular);
16+
17+
Sentry.captureMessage('non_serializable');
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Event } from '@sentry/node';
2+
3+
import { assertSentryEvent, getEventRequest, runServer } from '../../../../utils';
4+
5+
test('should normalize non-serializable context', async () => {
6+
const url = await runServer(__dirname);
7+
const requestBody = await getEventRequest(url);
8+
9+
assertSentryEvent(requestBody, {
10+
message: 'non_serializable',
11+
contexts: {},
12+
});
13+
14+
expect((requestBody as Event).contexts?.context_3).not.toBeDefined();
15+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as Sentry from '@sentry/node';
2+
3+
Sentry.init({
4+
dsn: 'https://[email protected]/1337',
5+
release: '1.0',
6+
});
7+
8+
Sentry.setContext('foo', { bar: 'baz' });
9+
Sentry.captureMessage('simple_context_object');
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Event } from '@sentry/node';
2+
3+
import { assertSentryEvent, getEventRequest, runServer } from '../../../../utils';
4+
5+
test('should set a simple context', async () => {
6+
const url = await runServer(__dirname);
7+
const requestBody = await getEventRequest(url);
8+
9+
assertSentryEvent(requestBody, {
10+
message: 'simple_context_object',
11+
contexts: {
12+
foo: {
13+
bar: 'baz',
14+
},
15+
},
16+
});
17+
18+
expect((requestBody as Event).contexts?.context_3).not.toBeDefined();
19+
});

0 commit comments

Comments
 (0)