Skip to content

Commit

Permalink
test(node): Add setContext integration tests (#4787)
Browse files Browse the repository at this point in the history
  • Loading branch information
onurtemizkan authored Mar 25, 2022
1 parent 398976c commit 37b14bf
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as Sentry from '@sentry/node';

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
release: '1.0',
});

Sentry.setContext('context_1', {
foo: 'bar',
baz: {
qux: 'quux',
},
});

Sentry.setContext('context_2', {
1: 'foo',
bar: false,
});

Sentry.setContext('context_3', null);

Sentry.captureMessage('multiple_contexts');
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Event } from '@sentry/node';

import { assertSentryEvent, getEventRequest, runServer } from '../../../../utils';

test('should record multiple contexts', async () => {
const url = await runServer(__dirname);
const requestBody = await getEventRequest(url);

assertSentryEvent(requestBody, {
message: 'multiple_contexts',
contexts: {
context_1: {
foo: 'bar',
baz: { qux: 'quux' },
},
context_2: { 1: 'foo', bar: false },
},
});

expect((requestBody as Event).contexts?.context_3).not.toBeDefined();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as Sentry from '@sentry/node';

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
release: '1.0',
});

type Circular = {
self?: Circular;
};

const objCircular: Circular = {};
objCircular.self = objCircular;

Sentry.setContext('non_serializable', objCircular);

Sentry.captureMessage('non_serializable');
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Event } from '@sentry/node';

import { assertSentryEvent, getEventRequest, runServer } from '../../../../utils';

test('should normalize non-serializable context', async () => {
const url = await runServer(__dirname);
const requestBody = await getEventRequest(url);

assertSentryEvent(requestBody, {
message: 'non_serializable',
contexts: {},
});

expect((requestBody as Event).contexts?.context_3).not.toBeDefined();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as Sentry from '@sentry/node';

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
release: '1.0',
});

Sentry.setContext('foo', { bar: 'baz' });
Sentry.captureMessage('simple_context_object');
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Event } from '@sentry/node';

import { assertSentryEvent, getEventRequest, runServer } from '../../../../utils';

test('should set a simple context', async () => {
const url = await runServer(__dirname);
const requestBody = await getEventRequest(url);

assertSentryEvent(requestBody, {
message: 'simple_context_object',
contexts: {
foo: {
bar: 'baz',
},
},
});

expect((requestBody as Event).contexts?.context_3).not.toBeDefined();
});

0 comments on commit 37b14bf

Please sign in to comment.