From f02d6b60bd28fcee47179fb41d267c933678b4ea Mon Sep 17 00:00:00 2001 From: Onur Temizkan Date: Fri, 25 Mar 2022 15:20:32 +0000 Subject: [PATCH] test(node): Add `setContext` integration tests. --- .../setContext/multiple-contexts/scenario.ts | 22 +++++++++++++++++++ .../setContext/multiple-contexts/test.ts | 21 ++++++++++++++++++ .../non-serializable-context/scenario.ts | 17 ++++++++++++++ .../non-serializable-context/test.ts | 15 +++++++++++++ .../setContext/simple-context/scenario.ts | 9 ++++++++ .../setContext/simple-context/test.ts | 19 ++++++++++++++++ 6 files changed, 103 insertions(+) create mode 100644 packages/node-integration-tests/suites/public-api/setContext/multiple-contexts/scenario.ts create mode 100644 packages/node-integration-tests/suites/public-api/setContext/multiple-contexts/test.ts create mode 100644 packages/node-integration-tests/suites/public-api/setContext/non-serializable-context/scenario.ts create mode 100644 packages/node-integration-tests/suites/public-api/setContext/non-serializable-context/test.ts create mode 100644 packages/node-integration-tests/suites/public-api/setContext/simple-context/scenario.ts create mode 100644 packages/node-integration-tests/suites/public-api/setContext/simple-context/test.ts diff --git a/packages/node-integration-tests/suites/public-api/setContext/multiple-contexts/scenario.ts b/packages/node-integration-tests/suites/public-api/setContext/multiple-contexts/scenario.ts new file mode 100644 index 000000000000..b0e44a65943c --- /dev/null +++ b/packages/node-integration-tests/suites/public-api/setContext/multiple-contexts/scenario.ts @@ -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'); diff --git a/packages/node-integration-tests/suites/public-api/setContext/multiple-contexts/test.ts b/packages/node-integration-tests/suites/public-api/setContext/multiple-contexts/test.ts new file mode 100644 index 000000000000..46d3de6bc2bf --- /dev/null +++ b/packages/node-integration-tests/suites/public-api/setContext/multiple-contexts/test.ts @@ -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(); +}); diff --git a/packages/node-integration-tests/suites/public-api/setContext/non-serializable-context/scenario.ts b/packages/node-integration-tests/suites/public-api/setContext/non-serializable-context/scenario.ts new file mode 100644 index 000000000000..52fec94ab6e4 --- /dev/null +++ b/packages/node-integration-tests/suites/public-api/setContext/non-serializable-context/scenario.ts @@ -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'); diff --git a/packages/node-integration-tests/suites/public-api/setContext/non-serializable-context/test.ts b/packages/node-integration-tests/suites/public-api/setContext/non-serializable-context/test.ts new file mode 100644 index 000000000000..2ea859de480c --- /dev/null +++ b/packages/node-integration-tests/suites/public-api/setContext/non-serializable-context/test.ts @@ -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(); +}); diff --git a/packages/node-integration-tests/suites/public-api/setContext/simple-context/scenario.ts b/packages/node-integration-tests/suites/public-api/setContext/simple-context/scenario.ts new file mode 100644 index 000000000000..43713f2768fb --- /dev/null +++ b/packages/node-integration-tests/suites/public-api/setContext/simple-context/scenario.ts @@ -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'); diff --git a/packages/node-integration-tests/suites/public-api/setContext/simple-context/test.ts b/packages/node-integration-tests/suites/public-api/setContext/simple-context/test.ts new file mode 100644 index 000000000000..cdabe34d8680 --- /dev/null +++ b/packages/node-integration-tests/suites/public-api/setContext/simple-context/test.ts @@ -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(); +});