From 979a1e33ab2819415e685b3cc5791204165d9173 Mon Sep 17 00:00:00 2001 From: Onur Temizkan Date: Fri, 12 Aug 2022 19:28:52 +0100 Subject: [PATCH 1/5] ref(test): Refactor Node integration tests to prevent nock leaks. --- .../node-integration-tests/jest.config.js | 1 + .../suites/express/handle-error/test.ts | 4 +- .../baggage-header-assign/test.ts | 63 +++++++----- .../baggage-header-out-bad-tx-name/test.ts | 4 +- .../sentry-trace/baggage-header-out/test.ts | 4 +- .../test.ts | 17 ++-- .../baggage-other-vendors/test.ts | 13 ++- .../baggage-transaction-name/test.ts | 4 +- .../sentry-trace/trace-header-assign/test.ts | 11 ++- .../sentry-trace/trace-header-out/test.ts | 4 +- .../suites/express/tracing/test.ts | 16 ++-- .../addBreadcrumb/empty-obj/test.ts | 4 +- .../multiple_breadcrumbs/test.ts | 4 +- .../addBreadcrumb/simple_breadcrumb/test.ts | 4 +- .../captureException/catched-error/test.ts | 4 +- .../captureException/empty-obj/test.ts | 4 +- .../captureException/simple-error/test.ts | 4 +- .../captureMessage/simple_message/test.ts | 4 +- .../captureMessage/with_level/test.ts | 4 +- .../configureScope/clear_scope/test.ts | 4 +- .../configureScope/set_properties/test.ts | 4 +- .../setContext/multiple-contexts/test.ts | 4 +- .../non-serializable-context/test.ts | 4 +- .../setContext/simple-context/test.ts | 4 +- .../setExtra/multiple-extras/test.ts | 4 +- .../setExtra/non-serializable-extra/test.ts | 4 +- .../public-api/setExtra/simple-extra/test.ts | 4 +- .../setExtras/consecutive-calls/test.ts | 4 +- .../setExtras/multiple-extras/test.ts | 4 +- .../public-api/setTag/with-primitives/test.ts | 4 +- .../setTags/with-primitives/test.ts | 4 +- .../public-api/setUser/unset_user/test.ts | 4 +- .../public-api/setUser/update_user/test.ts | 4 +- .../startTransaction/basic-usage/test.ts | 4 +- .../with-nested-spans/test.ts | 4 +- .../withScope/nested-scopes/test.ts | 4 +- .../crashed-session-aggregate/test.ts | 12 ++- .../errored-session-aggregate/test.ts | 11 ++- .../sessions/exited-session-aggregate/test.ts | 11 ++- .../suites/tracing/apollo-graphql/test.ts | 4 +- .../tracing/auto-instrument/mongodb/test.ts | 5 +- .../tracing/auto-instrument/mysql/test.ts | 4 +- .../suites/tracing/auto-instrument/pg/test.ts | 4 +- .../suites/tracing/prisma-orm/test.ts | 4 +- .../tracing/tracePropagationTargets/test.ts | 7 +- .../node-integration-tests/utils/index.ts | 95 +++++++++++++------ 46 files changed, 236 insertions(+), 162 deletions(-) diff --git a/packages/node-integration-tests/jest.config.js b/packages/node-integration-tests/jest.config.js index 9a2862dd9b8d..54ea7ebfb22f 100644 --- a/packages/node-integration-tests/jest.config.js +++ b/packages/node-integration-tests/jest.config.js @@ -4,4 +4,5 @@ module.exports = { globalSetup: '/utils/setup-tests.ts', ...baseConfig, testMatch: ['**/test.ts'], + testTimeout: 10000, }; diff --git a/packages/node-integration-tests/suites/express/handle-error/test.ts b/packages/node-integration-tests/suites/express/handle-error/test.ts index 9ae4586f6510..62380c2d6285 100644 --- a/packages/node-integration-tests/suites/express/handle-error/test.ts +++ b/packages/node-integration-tests/suites/express/handle-error/test.ts @@ -1,8 +1,8 @@ import { assertSentryEvent, getEnvelopeRequest, runServer } from '../../../utils/index'; test('should capture and send Express controller error.', async () => { - const url = await runServer(__dirname, `${__dirname}/server.ts`); - const event = await getEnvelopeRequest(`${url}/express`); + const { url, server, scope } = await runServer(__dirname, `${__dirname}/server.ts`); + const event = await getEnvelopeRequest({ url: `${url}/express`, server, scope }); expect((event[2] as any).exception.values[0].stacktrace.frames.length).toBeGreaterThan(0); diff --git a/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-assign/test.ts b/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-assign/test.ts index a8dc964e9c72..1f1cf4f84b61 100644 --- a/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-assign/test.ts +++ b/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-assign/test.ts @@ -4,11 +4,14 @@ import { getAPIResponse, runServer } from '../../../../utils/index'; import { TestAPIResponse } from '../server'; test('Should not overwrite baggage if the incoming request already has Sentry baggage data.', async () => { - const url = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); + const { url, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); - const response = (await getAPIResponse(new URL(`${url}/express`), { - baggage: 'sentry-release=2.0.0,sentry-environment=myEnv', - })) as TestAPIResponse; + const response = (await getAPIResponse( + { url: `${url}/express`, server, scope }, + { + baggage: 'sentry-release=2.0.0,sentry-environment=myEnv', + }, + )) as TestAPIResponse; expect(response).toBeDefined(); expect(response).toMatchObject({ @@ -20,12 +23,15 @@ test('Should not overwrite baggage if the incoming request already has Sentry ba }); test('Should propagate sentry trace baggage data from an incoming to an outgoing request.', async () => { - const url = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); + const { url, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); - const response = (await getAPIResponse(new URL(`${url}/express`), { - 'sentry-trace': '', - baggage: 'sentry-release=2.0.0,sentry-environment=myEnv,dogs=great', - })) as TestAPIResponse; + const response = (await getAPIResponse( + { url: `${url}/express`, server, scope }, + { + 'sentry-trace': '', + baggage: 'sentry-release=2.0.0,sentry-environment=myEnv,dogs=great', + }, + )) as TestAPIResponse; expect(response).toBeDefined(); expect(response).toMatchObject({ @@ -37,11 +43,14 @@ test('Should propagate sentry trace baggage data from an incoming to an outgoing }); test('Should propagate empty baggage if sentry-trace header is present in incoming request but no baggage header', async () => { - const url = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); + const { url, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); - const response = (await getAPIResponse(new URL(`${url}/express`), { - 'sentry-trace': '', - })) as TestAPIResponse; + const response = (await getAPIResponse( + { url: `${url}/express`, server, scope }, + { + 'sentry-trace': '', + }, + )) as TestAPIResponse; expect(response).toBeDefined(); expect(response).toMatchObject({ @@ -53,12 +62,15 @@ test('Should propagate empty baggage if sentry-trace header is present in incomi }); test('Should propagate empty sentry and ignore original 3rd party baggage entries if sentry-trace header is present', async () => { - const url = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); + const { url, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); - const response = (await getAPIResponse(new URL(`${url}/express`), { - 'sentry-trace': '', - baggage: 'foo=bar', - })) as TestAPIResponse; + const response = (await getAPIResponse( + { url: `${url}/express`, server, scope }, + { + 'sentry-trace': '', + baggage: 'foo=bar', + }, + )) as TestAPIResponse; expect(response).toBeDefined(); expect(response).toMatchObject({ @@ -70,9 +82,9 @@ test('Should propagate empty sentry and ignore original 3rd party baggage entrie }); test('Should populate and propagate sentry baggage if sentry-trace header does not exist', async () => { - const url = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); + const { url, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); - const response = (await getAPIResponse(new URL(`${url}/express`), {})) as TestAPIResponse; + const response = (await getAPIResponse({ url: `${url}/express`, server, scope }, {})) as TestAPIResponse; expect(response).toBeDefined(); expect(response).toMatchObject({ @@ -87,11 +99,14 @@ test('Should populate and propagate sentry baggage if sentry-trace header does n }); test('Should populate Sentry and ignore 3rd party content if sentry-trace header does not exist', async () => { - const url = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); + const { url, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); - const response = (await getAPIResponse(new URL(`${url}/express`), { - baggage: 'foo=bar,bar=baz', - })) as TestAPIResponse; + const response = (await getAPIResponse( + { url: `${url}/express`, server, scope }, + { + baggage: 'foo=bar,bar=baz', + }, + )) as TestAPIResponse; expect(response).toBeDefined(); expect(response).toMatchObject({ diff --git a/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-out-bad-tx-name/test.ts b/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-out-bad-tx-name/test.ts index eb969e6e3a8a..00000b7f9f35 100644 --- a/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-out-bad-tx-name/test.ts +++ b/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-out-bad-tx-name/test.ts @@ -4,9 +4,9 @@ import { getAPIResponse, runServer } from '../../../../utils/index'; import { TestAPIResponse } from '../server'; test('Does not include transaction name if transaction source is not set', async () => { - const url = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`); + const { url, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`); - const response = (await getAPIResponse(new URL(`${url}/express`))) as TestAPIResponse; + const response = (await getAPIResponse({ url: `${url}/express`, server, scope })) as TestAPIResponse; const baggageString = response.test_data.baggage; expect(response).toBeDefined(); diff --git a/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-out/test.ts b/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-out/test.ts index 099e4e4bd65f..b7c8acf0f6ba 100644 --- a/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-out/test.ts +++ b/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-out/test.ts @@ -4,9 +4,9 @@ import { getAPIResponse, runServer } from '../../../../utils/index'; import { TestAPIResponse } from '../server'; test('should attach a `baggage` header to an outgoing request.', async () => { - const url = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`); + const { url, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`); - const response = (await getAPIResponse(new URL(`${url}/express`))) as TestAPIResponse; + const response = (await getAPIResponse({ url: `${url}/express`, server, scope })) as TestAPIResponse; expect(response).toBeDefined(); expect(response).toMatchObject({ diff --git a/packages/node-integration-tests/suites/express/sentry-trace/baggage-other-vendors-with-sentry-entries/test.ts b/packages/node-integration-tests/suites/express/sentry-trace/baggage-other-vendors-with-sentry-entries/test.ts index 30c86c349092..39d10c95ea65 100644 --- a/packages/node-integration-tests/suites/express/sentry-trace/baggage-other-vendors-with-sentry-entries/test.ts +++ b/packages/node-integration-tests/suites/express/sentry-trace/baggage-other-vendors-with-sentry-entries/test.ts @@ -4,12 +4,15 @@ import { getAPIResponse, runServer } from '../../../../utils/index'; import { TestAPIResponse } from '../server'; test('should ignore sentry-values in `baggage` header of a third party vendor and overwrite them with incoming DSC', async () => { - const url = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`); + const { url, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`); - const response = (await getAPIResponse(new URL(`${url}/express`), { - 'sentry-trace': '', - baggage: 'sentry-release=2.1.0,sentry-environment=myEnv', - })) as TestAPIResponse; + const response = (await getAPIResponse( + { url: `${url}/express`, server, scope }, + { + 'sentry-trace': '', + baggage: 'sentry-release=2.1.0,sentry-environment=myEnv', + }, + )) as TestAPIResponse; expect(response).toBeDefined(); expect(response).toMatchObject({ @@ -21,9 +24,9 @@ test('should ignore sentry-values in `baggage` header of a third party vendor an }); test('should ignore sentry-values in `baggage` header of a third party vendor and overwrite them with new DSC', async () => { - const url = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`); + const { url, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`); - const response = (await getAPIResponse(new URL(`${url}/express`), {})) as TestAPIResponse; + const response = (await getAPIResponse({ url: `${url}/express`, server, scope }, {})) as TestAPIResponse; expect(response).toBeDefined(); expect(response).toMatchObject({ diff --git a/packages/node-integration-tests/suites/express/sentry-trace/baggage-other-vendors/test.ts b/packages/node-integration-tests/suites/express/sentry-trace/baggage-other-vendors/test.ts index 34cadd277a9e..3f00344dab1a 100644 --- a/packages/node-integration-tests/suites/express/sentry-trace/baggage-other-vendors/test.ts +++ b/packages/node-integration-tests/suites/express/sentry-trace/baggage-other-vendors/test.ts @@ -4,12 +4,15 @@ import { getAPIResponse, runServer } from '../../../../utils/index'; import { TestAPIResponse } from '../server'; test('should merge `baggage` header of a third party vendor with the Sentry DSC baggage items', async () => { - const url = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`); + const { url, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`); - const response = (await getAPIResponse(new URL(`${url}/express`), { - 'sentry-trace': '', - baggage: 'sentry-release=2.0.0,sentry-environment=myEnv', - })) as TestAPIResponse; + const response = (await getAPIResponse( + { url: `${url}/express`, server, scope }, + { + 'sentry-trace': '', + baggage: 'sentry-release=2.0.0,sentry-environment=myEnv', + }, + )) as TestAPIResponse; expect(response).toBeDefined(); expect(response).toMatchObject({ diff --git a/packages/node-integration-tests/suites/express/sentry-trace/baggage-transaction-name/test.ts b/packages/node-integration-tests/suites/express/sentry-trace/baggage-transaction-name/test.ts index 34ff4a64bf94..fd265232a575 100644 --- a/packages/node-integration-tests/suites/express/sentry-trace/baggage-transaction-name/test.ts +++ b/packages/node-integration-tests/suites/express/sentry-trace/baggage-transaction-name/test.ts @@ -4,9 +4,9 @@ import { getAPIResponse, runServer } from '../../../../utils/index'; import { TestAPIResponse } from '../server'; test('Includes transaction in baggage if the transaction name is parameterized', async () => { - const url = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`); + const { url, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`); - const response = (await getAPIResponse(new URL(`${url}/express`))) as TestAPIResponse; + const response = (await getAPIResponse({ url: `${url}/express`, server, scope })) as TestAPIResponse; expect(response).toBeDefined(); expect(response).toMatchObject({ diff --git a/packages/node-integration-tests/suites/express/sentry-trace/trace-header-assign/test.ts b/packages/node-integration-tests/suites/express/sentry-trace/trace-header-assign/test.ts index 47382b3fdb4b..ebd3c850382c 100644 --- a/packages/node-integration-tests/suites/express/sentry-trace/trace-header-assign/test.ts +++ b/packages/node-integration-tests/suites/express/sentry-trace/trace-header-assign/test.ts @@ -5,11 +5,14 @@ import { getAPIResponse, runServer } from '../../../../utils/index'; import { TestAPIResponse } from '../server'; test('Should assign `sentry-trace` header which sets parent trace id of an outgoing request.', async () => { - const url = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); + const { url, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); - const response = (await getAPIResponse(new URL(`${url}/express`), { - 'sentry-trace': '12312012123120121231201212312012-1121201211212012-0', - })) as TestAPIResponse; + const response = (await getAPIResponse( + { url: `${url}/express`, server, scope }, + { + 'sentry-trace': '12312012123120121231201212312012-1121201211212012-0', + }, + )) as TestAPIResponse; expect(response).toBeDefined(); expect(response).toMatchObject({ diff --git a/packages/node-integration-tests/suites/express/sentry-trace/trace-header-out/test.ts b/packages/node-integration-tests/suites/express/sentry-trace/trace-header-out/test.ts index 788a3f7086ab..3ad850e9e029 100644 --- a/packages/node-integration-tests/suites/express/sentry-trace/trace-header-out/test.ts +++ b/packages/node-integration-tests/suites/express/sentry-trace/trace-header-out/test.ts @@ -5,9 +5,9 @@ import { getAPIResponse, runServer } from '../../../../utils/index'; import { TestAPIResponse } from '../server'; test('should attach a `sentry-trace` header to an outgoing request.', async () => { - const url = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); + const { url, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); - const response = (await getAPIResponse(new URL(`${url}/express`))) as TestAPIResponse; + const response = (await getAPIResponse({ url: `${url}/express`, server, scope })) as TestAPIResponse; expect(response).toBeDefined(); expect(response).toMatchObject({ diff --git a/packages/node-integration-tests/suites/express/tracing/test.ts b/packages/node-integration-tests/suites/express/tracing/test.ts index 043a91aeeb60..316dc87405b8 100644 --- a/packages/node-integration-tests/suites/express/tracing/test.ts +++ b/packages/node-integration-tests/suites/express/tracing/test.ts @@ -1,8 +1,8 @@ import { assertSentryTransaction, getEnvelopeRequest, runServer } from '../../../utils/index'; test('should create and send transactions for Express routes and spans for middlewares.', async () => { - const url = await runServer(__dirname, `${__dirname}/server.ts`); - const envelope = await getEnvelopeRequest(`${url}/express`); + const { url, server, scope } = await runServer(__dirname, `${__dirname}/server.ts`); + const envelope = await getEnvelopeRequest({ url: `${url}/express`, server, scope }); expect(envelope).toHaveLength(3); @@ -29,8 +29,8 @@ test('should create and send transactions for Express routes and spans for middl }); test('should set a correct transaction name for routes specified in RegEx', async () => { - const url = await runServer(__dirname, `${__dirname}/server.ts`); - const envelope = await getEnvelopeRequest(`${url}/regex`); + const { url, server, scope } = await runServer(__dirname, `${__dirname}/server.ts`); + const envelope = await getEnvelopeRequest({ url: `${url}/regex`, server, scope }); expect(envelope).toHaveLength(3); @@ -57,8 +57,8 @@ test('should set a correct transaction name for routes specified in RegEx', asyn test.each([['array1'], ['array5']])( 'should set a correct transaction name for routes consisting of arrays of routes', async segment => { - const url = await runServer(__dirname, `${__dirname}/server.ts`); - const envelope = await getEnvelopeRequest(`${url}/${segment}`); + const { url, server, scope } = await runServer(__dirname, `${__dirname}/server.ts`); + const envelope = await getEnvelopeRequest({ url: `${url}/${segment}`, server, scope }); expect(envelope).toHaveLength(3); @@ -93,8 +93,8 @@ test.each([ ['arr/requiredPath/optionalPath/'], ['arr/requiredPath/optionalPath/lastParam'], ])('should handle more complex regexes in route arrays correctly', async segment => { - const url = await runServer(__dirname, `${__dirname}/server.ts`); - const envelope = await getEnvelopeRequest(`${url}/${segment}`); + const { url, server, scope } = await runServer(__dirname, `${__dirname}/server.ts`); + const envelope = await getEnvelopeRequest({ url: `${url}/${segment}`, server, scope }); expect(envelope).toHaveLength(3); diff --git a/packages/node-integration-tests/suites/public-api/addBreadcrumb/empty-obj/test.ts b/packages/node-integration-tests/suites/public-api/addBreadcrumb/empty-obj/test.ts index be9c0b6c15b6..48a394d346af 100644 --- a/packages/node-integration-tests/suites/public-api/addBreadcrumb/empty-obj/test.ts +++ b/packages/node-integration-tests/suites/public-api/addBreadcrumb/empty-obj/test.ts @@ -1,8 +1,8 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils'; test('should add an empty breadcrumb, when an empty object is given', async () => { - const url = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(url, 2); + const config = await runServer(__dirname); + const envelopes = await getMultipleEnvelopeRequest(config, 2); const errorEnvelope = envelopes[1]; expect(errorEnvelope).toHaveLength(3); diff --git a/packages/node-integration-tests/suites/public-api/addBreadcrumb/multiple_breadcrumbs/test.ts b/packages/node-integration-tests/suites/public-api/addBreadcrumb/multiple_breadcrumbs/test.ts index 38ef745cb28e..b8095ac46a69 100644 --- a/packages/node-integration-tests/suites/public-api/addBreadcrumb/multiple_breadcrumbs/test.ts +++ b/packages/node-integration-tests/suites/public-api/addBreadcrumb/multiple_breadcrumbs/test.ts @@ -1,8 +1,8 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils'; test('should add multiple breadcrumbs', async () => { - const url = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(url, 2); + const config = await runServer(__dirname); + const envelopes = await getMultipleEnvelopeRequest(config, 2); assertSentryEvent(envelopes[1][2], { message: 'test_multi_breadcrumbs', diff --git a/packages/node-integration-tests/suites/public-api/addBreadcrumb/simple_breadcrumb/test.ts b/packages/node-integration-tests/suites/public-api/addBreadcrumb/simple_breadcrumb/test.ts index d5fc2ef8df73..440c6758abe0 100644 --- a/packages/node-integration-tests/suites/public-api/addBreadcrumb/simple_breadcrumb/test.ts +++ b/packages/node-integration-tests/suites/public-api/addBreadcrumb/simple_breadcrumb/test.ts @@ -1,8 +1,8 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils'; test('should add a simple breadcrumb', async () => { - const url = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(url, 2); + const config = await runServer(__dirname); + const envelopes = await getMultipleEnvelopeRequest(config, 2); assertSentryEvent(envelopes[1][2], { message: 'test_simple', diff --git a/packages/node-integration-tests/suites/public-api/captureException/catched-error/test.ts b/packages/node-integration-tests/suites/public-api/captureException/catched-error/test.ts index 2070199f77d9..8f88bfceb3dc 100644 --- a/packages/node-integration-tests/suites/public-api/captureException/catched-error/test.ts +++ b/packages/node-integration-tests/suites/public-api/captureException/catched-error/test.ts @@ -1,8 +1,8 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils'; test('should work inside catch block', async () => { - const url = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(url, 2); + const config = await runServer(__dirname); + const envelopes = await getMultipleEnvelopeRequest(config, 2); const errorEnvelope = envelopes[1]; assertSentryEvent(errorEnvelope[2], { diff --git a/packages/node-integration-tests/suites/public-api/captureException/empty-obj/test.ts b/packages/node-integration-tests/suites/public-api/captureException/empty-obj/test.ts index 0df21996f08c..82e9d5563fd3 100644 --- a/packages/node-integration-tests/suites/public-api/captureException/empty-obj/test.ts +++ b/packages/node-integration-tests/suites/public-api/captureException/empty-obj/test.ts @@ -1,8 +1,8 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils'; test('should capture an empty object', async () => { - const url = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(url, 2); + const config = await runServer(__dirname); + const envelopes = await getMultipleEnvelopeRequest(config, 2); const errorEnvelope = envelopes[1]; assertSentryEvent(errorEnvelope[2], { diff --git a/packages/node-integration-tests/suites/public-api/captureException/simple-error/test.ts b/packages/node-integration-tests/suites/public-api/captureException/simple-error/test.ts index 66ca0410377a..57d55fc1e369 100644 --- a/packages/node-integration-tests/suites/public-api/captureException/simple-error/test.ts +++ b/packages/node-integration-tests/suites/public-api/captureException/simple-error/test.ts @@ -1,8 +1,8 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils'; test('should capture a simple error with message', async () => { - const url = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(url, 2); + const config = await runServer(__dirname); + const envelopes = await getMultipleEnvelopeRequest(config, 2); const errorEnvelope = envelopes[1]; assertSentryEvent(errorEnvelope[2], { diff --git a/packages/node-integration-tests/suites/public-api/captureMessage/simple_message/test.ts b/packages/node-integration-tests/suites/public-api/captureMessage/simple_message/test.ts index 2ecc0e86720d..98107da3a13a 100644 --- a/packages/node-integration-tests/suites/public-api/captureMessage/simple_message/test.ts +++ b/packages/node-integration-tests/suites/public-api/captureMessage/simple_message/test.ts @@ -1,8 +1,8 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils'; test('should capture a simple message string', async () => { - const url = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(url, 2); + const config = await runServer(__dirname); + const envelopes = await getMultipleEnvelopeRequest(config, 2); const errorEnvelope = envelopes[1]; assertSentryEvent(errorEnvelope[2], { diff --git a/packages/node-integration-tests/suites/public-api/captureMessage/with_level/test.ts b/packages/node-integration-tests/suites/public-api/captureMessage/with_level/test.ts index 18bd395b148d..e77651cf3691 100644 --- a/packages/node-integration-tests/suites/public-api/captureMessage/with_level/test.ts +++ b/packages/node-integration-tests/suites/public-api/captureMessage/with_level/test.ts @@ -1,8 +1,8 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils'; test('should capture with different severity levels', async () => { - const url = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(url, 12); + const config = await runServer(__dirname); + const envelopes = await getMultipleEnvelopeRequest(config, 12); assertSentryEvent(envelopes[1][2], { message: 'debug_message', diff --git a/packages/node-integration-tests/suites/public-api/configureScope/clear_scope/test.ts b/packages/node-integration-tests/suites/public-api/configureScope/clear_scope/test.ts index c510bc22489c..dc9a62621f84 100644 --- a/packages/node-integration-tests/suites/public-api/configureScope/clear_scope/test.ts +++ b/packages/node-integration-tests/suites/public-api/configureScope/clear_scope/test.ts @@ -3,8 +3,8 @@ import { Event } from '@sentry/node'; import { assertSentryEvent, getEnvelopeRequest, runServer } from '../../../../utils'; test('should clear previously set properties of a scope', async () => { - const url = await runServer(__dirname); - const envelope = await getEnvelopeRequest(url); + const config = await runServer(__dirname); + const envelope = await getEnvelopeRequest(config); assertSentryEvent(envelope[2], { message: 'cleared_scope', diff --git a/packages/node-integration-tests/suites/public-api/configureScope/set_properties/test.ts b/packages/node-integration-tests/suites/public-api/configureScope/set_properties/test.ts index 17d7c9b8df42..af1de39169d8 100644 --- a/packages/node-integration-tests/suites/public-api/configureScope/set_properties/test.ts +++ b/packages/node-integration-tests/suites/public-api/configureScope/set_properties/test.ts @@ -1,8 +1,8 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils'; test('should set different properties of a scope', async () => { - const url = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(url, 2); + const config = await runServer(__dirname); + const envelopes = await getMultipleEnvelopeRequest(config, 2); const errorEnvelope = envelopes[1]; assertSentryEvent(errorEnvelope[2], { 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 index f1c8981fad9a..00c908c55026 100644 --- 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 @@ -3,8 +3,8 @@ import { Event } from '@sentry/node'; import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils'; test('should record multiple contexts', async () => { - const url = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(url, 2); + const config = await runServer(__dirname); + const envelopes = await getMultipleEnvelopeRequest(config, 2); const errorEnvelope = envelopes[1]; assertSentryEvent(errorEnvelope[2], { 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 index 26b5fe8c7025..92b7c3d6cbaf 100644 --- 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 @@ -3,8 +3,8 @@ import { Event } from '@sentry/node'; import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils'; test('should normalize non-serializable context', async () => { - const url = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(url, 2); + const config = await runServer(__dirname); + const envelopes = await getMultipleEnvelopeRequest(config, 2); const errorEnvelope = envelopes[1]; assertSentryEvent(errorEnvelope[2], { 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 index 362afb9e55e4..d1a8e8143f0c 100644 --- 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 @@ -3,8 +3,8 @@ import { Event } from '@sentry/node'; import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils'; test('should set a simple context', async () => { - const url = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(url, 2); + const config = await runServer(__dirname); + const envelopes = await getMultipleEnvelopeRequest(config, 2); const errorEnvelope = envelopes[1]; assertSentryEvent(errorEnvelope[2], { diff --git a/packages/node-integration-tests/suites/public-api/setExtra/multiple-extras/test.ts b/packages/node-integration-tests/suites/public-api/setExtra/multiple-extras/test.ts index 428ebd7f45c4..a6cf6a17e844 100644 --- a/packages/node-integration-tests/suites/public-api/setExtra/multiple-extras/test.ts +++ b/packages/node-integration-tests/suites/public-api/setExtra/multiple-extras/test.ts @@ -1,8 +1,8 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils'; test('should record multiple extras of different types', async () => { - const url = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(url, 2); + const config = await runServer(__dirname); + const envelopes = await getMultipleEnvelopeRequest(config, 2); const errorEnvelope = envelopes[1]; assertSentryEvent(errorEnvelope[2], { diff --git a/packages/node-integration-tests/suites/public-api/setExtra/non-serializable-extra/test.ts b/packages/node-integration-tests/suites/public-api/setExtra/non-serializable-extra/test.ts index 3cd2ca078eeb..83efc0735535 100644 --- a/packages/node-integration-tests/suites/public-api/setExtra/non-serializable-extra/test.ts +++ b/packages/node-integration-tests/suites/public-api/setExtra/non-serializable-extra/test.ts @@ -1,8 +1,8 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils'; test('should normalize non-serializable extra', async () => { - const url = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(url, 2); + const config = await runServer(__dirname); + const envelopes = await getMultipleEnvelopeRequest(config, 2); const errorEnvelope = envelopes[1]; assertSentryEvent(errorEnvelope[2], { diff --git a/packages/node-integration-tests/suites/public-api/setExtra/simple-extra/test.ts b/packages/node-integration-tests/suites/public-api/setExtra/simple-extra/test.ts index 33bfe641bfa3..47a387107b75 100644 --- a/packages/node-integration-tests/suites/public-api/setExtra/simple-extra/test.ts +++ b/packages/node-integration-tests/suites/public-api/setExtra/simple-extra/test.ts @@ -1,8 +1,8 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils'; test('should set a simple extra', async () => { - const url = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(url, 2); + const config = await runServer(__dirname); + const envelopes = await getMultipleEnvelopeRequest(config, 2); const errorEnvelope = envelopes[1]; assertSentryEvent(errorEnvelope[2], { diff --git a/packages/node-integration-tests/suites/public-api/setExtras/consecutive-calls/test.ts b/packages/node-integration-tests/suites/public-api/setExtras/consecutive-calls/test.ts index 464324c97fdf..c01656b238dd 100644 --- a/packages/node-integration-tests/suites/public-api/setExtras/consecutive-calls/test.ts +++ b/packages/node-integration-tests/suites/public-api/setExtras/consecutive-calls/test.ts @@ -1,8 +1,8 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils'; test('should set extras from multiple consecutive calls', async () => { - const url = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(url, 2); + const config = await runServer(__dirname); + const envelopes = await getMultipleEnvelopeRequest(config, 2); const errorEnvelope = envelopes[1]; assertSentryEvent(errorEnvelope[2], { diff --git a/packages/node-integration-tests/suites/public-api/setExtras/multiple-extras/test.ts b/packages/node-integration-tests/suites/public-api/setExtras/multiple-extras/test.ts index d23c8e815a06..cec73a28769b 100644 --- a/packages/node-integration-tests/suites/public-api/setExtras/multiple-extras/test.ts +++ b/packages/node-integration-tests/suites/public-api/setExtras/multiple-extras/test.ts @@ -1,8 +1,8 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils'; test('should record an extras object', async () => { - const url = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(url, 2); + const config = await runServer(__dirname); + const envelopes = await getMultipleEnvelopeRequest(config, 2); const errorEnvelope = envelopes[1]; assertSentryEvent(errorEnvelope[2], { diff --git a/packages/node-integration-tests/suites/public-api/setTag/with-primitives/test.ts b/packages/node-integration-tests/suites/public-api/setTag/with-primitives/test.ts index 88d78b70c655..2e5d504af1c4 100644 --- a/packages/node-integration-tests/suites/public-api/setTag/with-primitives/test.ts +++ b/packages/node-integration-tests/suites/public-api/setTag/with-primitives/test.ts @@ -1,8 +1,8 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils'; test('should set primitive tags', async () => { - const url = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(url, 2); + const config = await runServer(__dirname); + const envelopes = await getMultipleEnvelopeRequest(config, 2); const envelope = envelopes[1]; assertSentryEvent(envelope[2], { diff --git a/packages/node-integration-tests/suites/public-api/setTags/with-primitives/test.ts b/packages/node-integration-tests/suites/public-api/setTags/with-primitives/test.ts index 88d78b70c655..2e5d504af1c4 100644 --- a/packages/node-integration-tests/suites/public-api/setTags/with-primitives/test.ts +++ b/packages/node-integration-tests/suites/public-api/setTags/with-primitives/test.ts @@ -1,8 +1,8 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils'; test('should set primitive tags', async () => { - const url = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(url, 2); + const config = await runServer(__dirname); + const envelopes = await getMultipleEnvelopeRequest(config, 2); const envelope = envelopes[1]; assertSentryEvent(envelope[2], { diff --git a/packages/node-integration-tests/suites/public-api/setUser/unset_user/test.ts b/packages/node-integration-tests/suites/public-api/setUser/unset_user/test.ts index 736ed1fdf38c..71bfc068543a 100644 --- a/packages/node-integration-tests/suites/public-api/setUser/unset_user/test.ts +++ b/packages/node-integration-tests/suites/public-api/setUser/unset_user/test.ts @@ -3,8 +3,8 @@ import { Event } from '@sentry/node'; import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils'; test('should unset user', async () => { - const url = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(url, 6); + const config = await runServer(__dirname); + const envelopes = await getMultipleEnvelopeRequest(config, 6); assertSentryEvent(envelopes[1][2], { message: 'no_user', diff --git a/packages/node-integration-tests/suites/public-api/setUser/update_user/test.ts b/packages/node-integration-tests/suites/public-api/setUser/update_user/test.ts index 27c7de89fe45..33b3f9ea47bf 100644 --- a/packages/node-integration-tests/suites/public-api/setUser/update_user/test.ts +++ b/packages/node-integration-tests/suites/public-api/setUser/update_user/test.ts @@ -1,8 +1,8 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils'; test('should update user', async () => { - const url = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(url, 4); + const config = await runServer(__dirname); + const envelopes = await getMultipleEnvelopeRequest(config, 4); assertSentryEvent(envelopes[1][2], { message: 'first_user', diff --git a/packages/node-integration-tests/suites/public-api/startTransaction/basic-usage/test.ts b/packages/node-integration-tests/suites/public-api/startTransaction/basic-usage/test.ts index ae1bf263b27a..f254afcee734 100644 --- a/packages/node-integration-tests/suites/public-api/startTransaction/basic-usage/test.ts +++ b/packages/node-integration-tests/suites/public-api/startTransaction/basic-usage/test.ts @@ -1,8 +1,8 @@ import { assertSentryTransaction, getEnvelopeRequest, runServer } from '../../../../utils'; test('should send a manually started transaction when @sentry/tracing is imported using unnamed import.', async () => { - const url = await runServer(__dirname); - const envelope = await getEnvelopeRequest(url); + const config = await runServer(__dirname); + const envelope = await getEnvelopeRequest(config); expect(envelope).toHaveLength(3); diff --git a/packages/node-integration-tests/suites/public-api/startTransaction/with-nested-spans/test.ts b/packages/node-integration-tests/suites/public-api/startTransaction/with-nested-spans/test.ts index 4a39a9d16f4c..eb1070b5cddc 100644 --- a/packages/node-integration-tests/suites/public-api/startTransaction/with-nested-spans/test.ts +++ b/packages/node-integration-tests/suites/public-api/startTransaction/with-nested-spans/test.ts @@ -1,8 +1,8 @@ import { assertSentryTransaction, getEnvelopeRequest, runServer } from '../../../../utils'; test('should report finished spans as children of the root transaction.', async () => { - const url = await runServer(__dirname); - const envelope = await getEnvelopeRequest(url); + const config = await runServer(__dirname); + const envelope = await getEnvelopeRequest(config); expect(envelope).toHaveLength(3); diff --git a/packages/node-integration-tests/suites/public-api/withScope/nested-scopes/test.ts b/packages/node-integration-tests/suites/public-api/withScope/nested-scopes/test.ts index b6399b8dd78e..2244190fce26 100644 --- a/packages/node-integration-tests/suites/public-api/withScope/nested-scopes/test.ts +++ b/packages/node-integration-tests/suites/public-api/withScope/nested-scopes/test.ts @@ -3,8 +3,8 @@ import { Event } from '@sentry/node'; import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils'; test('should allow nested scoping', async () => { - const url = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(url, 10); + const config = await runServer(__dirname); + const envelopes = await getMultipleEnvelopeRequest(config, 10); assertSentryEvent(envelopes[1][2], { message: 'root_before', diff --git a/packages/node-integration-tests/suites/sessions/crashed-session-aggregate/test.ts b/packages/node-integration-tests/suites/sessions/crashed-session-aggregate/test.ts index 13051fd9c47a..2472993f163d 100644 --- a/packages/node-integration-tests/suites/sessions/crashed-session-aggregate/test.ts +++ b/packages/node-integration-tests/suites/sessions/crashed-session-aggregate/test.ts @@ -3,13 +3,17 @@ import path from 'path'; import { getMultipleEnvelopeRequest, runServer } from '../../../utils'; test('should aggregate successful and crashed sessions', async () => { - const url = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); + const { url, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); const envelopes = await Promise.race([ - getMultipleEnvelopeRequest(`${url}/success`, 2), - getMultipleEnvelopeRequest(`${url}/error_unhandled`, 2), - getMultipleEnvelopeRequest(`${url}/success_next`, 2), + getMultipleEnvelopeRequest({ url: `${url}/success`, server, scope }, 2, 'get', false), + getMultipleEnvelopeRequest({ url: `${url}/error_unhandled`, server, scope }, 2, 'get', false), + getMultipleEnvelopeRequest({ url: `${url}/success_next`, server, scope }, 2, 'get', false), ]); + + scope.persist(false); + server.close(); + const envelope = envelopes[1]; expect(envelope[0]).toMatchObject({ diff --git a/packages/node-integration-tests/suites/sessions/errored-session-aggregate/test.ts b/packages/node-integration-tests/suites/sessions/errored-session-aggregate/test.ts index 0f3130cd9b09..8e78efad15f9 100644 --- a/packages/node-integration-tests/suites/sessions/errored-session-aggregate/test.ts +++ b/packages/node-integration-tests/suites/sessions/errored-session-aggregate/test.ts @@ -3,14 +3,17 @@ import path from 'path'; import { getMultipleEnvelopeRequest, runServer } from '../../../utils'; test('should aggregate successful, crashed and erroneous sessions', async () => { - const url = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); + const { url, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); const envelopes = await Promise.race([ - getMultipleEnvelopeRequest(`${url}/success`, 3), - getMultipleEnvelopeRequest(`${url}/error_handled`, 3), - getMultipleEnvelopeRequest(`${url}/error_unhandled`, 3), + getMultipleEnvelopeRequest({ url: `${url}/success`, server, scope }, 3, 'get', false), + getMultipleEnvelopeRequest({ url: `${url}/error_handled`, server, scope }, 3, 'get', false), + getMultipleEnvelopeRequest({ url: `${url}/error_unhandled`, server, scope }, 3, 'get', false), ]); + scope.persist(false); + await new Promise(resolve => server.close(resolve)); + expect(envelopes).toHaveLength(3); const aggregateSessionEnvelope = envelopes[2]; expect(aggregateSessionEnvelope[0]).toMatchObject({ diff --git a/packages/node-integration-tests/suites/sessions/exited-session-aggregate/test.ts b/packages/node-integration-tests/suites/sessions/exited-session-aggregate/test.ts index 0f63f83df59d..b8fb6f19e4c6 100644 --- a/packages/node-integration-tests/suites/sessions/exited-session-aggregate/test.ts +++ b/packages/node-integration-tests/suites/sessions/exited-session-aggregate/test.ts @@ -3,14 +3,17 @@ import path from 'path'; import { getEnvelopeRequest, runServer } from '../../../utils'; test('should aggregate successful sessions', async () => { - const url = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); + const { url, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); const envelope = await Promise.race([ - getEnvelopeRequest(`${url}/success`), - getEnvelopeRequest(`${url}/success_next`), - getEnvelopeRequest(`${url}/success_slow`), + getEnvelopeRequest({ url: `${url}/success`, server, scope }, 'get', false), + getEnvelopeRequest({ url: `${url}/success_next`, server, scope }, 'get', false), + getEnvelopeRequest({ url: `${url}/success_slow`, server, scope }, 'get', false), ]); + scope.persist(false); + server.close(); + expect(envelope).toHaveLength(3); expect(envelope[0]).toMatchObject({ sent_at: expect.any(String), diff --git a/packages/node-integration-tests/suites/tracing/apollo-graphql/test.ts b/packages/node-integration-tests/suites/tracing/apollo-graphql/test.ts index c387175281b9..d80d52cf91df 100644 --- a/packages/node-integration-tests/suites/tracing/apollo-graphql/test.ts +++ b/packages/node-integration-tests/suites/tracing/apollo-graphql/test.ts @@ -4,8 +4,8 @@ import { assertSentryTransaction, conditionalTest, getEnvelopeRequest, runServer // Ref: https://github.com/graphql/graphql-js/blob/main/package.json conditionalTest({ min: 12 })('GraphQL/Apollo Tests', () => { test('should instrument GraphQL and Apollo Server.', async () => { - const url = await runServer(__dirname); - const envelope = await getEnvelopeRequest(url); + const config = await runServer(__dirname); + const envelope = await getEnvelopeRequest(config); expect(envelope).toHaveLength(3); diff --git a/packages/node-integration-tests/suites/tracing/auto-instrument/mongodb/test.ts b/packages/node-integration-tests/suites/tracing/auto-instrument/mongodb/test.ts index 76cadd1518dd..2645c54f70a7 100644 --- a/packages/node-integration-tests/suites/tracing/auto-instrument/mongodb/test.ts +++ b/packages/node-integration-tests/suites/tracing/auto-instrument/mongodb/test.ts @@ -18,9 +18,8 @@ conditionalTest({ min: 12 })('MongoDB Test', () => { }); test('should auto-instrument `mongodb` package.', async () => { - const url = await runServer(__dirname); - - const envelope = await getEnvelopeRequest(url); + const config = await runServer(__dirname); + const envelope = await getEnvelopeRequest(config); expect(envelope).toHaveLength(3); diff --git a/packages/node-integration-tests/suites/tracing/auto-instrument/mysql/test.ts b/packages/node-integration-tests/suites/tracing/auto-instrument/mysql/test.ts index 848667aceedf..f65d9d54710d 100644 --- a/packages/node-integration-tests/suites/tracing/auto-instrument/mysql/test.ts +++ b/packages/node-integration-tests/suites/tracing/auto-instrument/mysql/test.ts @@ -1,8 +1,8 @@ import { assertSentryTransaction, getEnvelopeRequest, runServer } from '../../../../utils'; test('should auto-instrument `mysql` package.', async () => { - const url = await runServer(__dirname); - const envelope = await getEnvelopeRequest(url); + const config = await runServer(__dirname); + const envelope = await getEnvelopeRequest(config); expect(envelope).toHaveLength(3); diff --git a/packages/node-integration-tests/suites/tracing/auto-instrument/pg/test.ts b/packages/node-integration-tests/suites/tracing/auto-instrument/pg/test.ts index 258909f57451..4b48b98e23ca 100644 --- a/packages/node-integration-tests/suites/tracing/auto-instrument/pg/test.ts +++ b/packages/node-integration-tests/suites/tracing/auto-instrument/pg/test.ts @@ -29,8 +29,8 @@ beforeAll(() => { }); test('should auto-instrument `pg` package.', async () => { - const url = await runServer(__dirname); - const envelope = await getEnvelopeRequest(url); + const config = await runServer(__dirname); + const envelope = await getEnvelopeRequest(config); expect(envelope).toHaveLength(3); diff --git a/packages/node-integration-tests/suites/tracing/prisma-orm/test.ts b/packages/node-integration-tests/suites/tracing/prisma-orm/test.ts index 91e6f39da889..da884f70caf8 100644 --- a/packages/node-integration-tests/suites/tracing/prisma-orm/test.ts +++ b/packages/node-integration-tests/suites/tracing/prisma-orm/test.ts @@ -2,8 +2,8 @@ import { assertSentryTransaction, conditionalTest, getEnvelopeRequest, runServer conditionalTest({ min: 12 })('Prisma ORM Integration', () => { test('should instrument Prisma client for tracing.', async () => { - const url = await runServer(__dirname); - const envelope = await getEnvelopeRequest(url); + const config = await runServer(__dirname); + const envelope = await getEnvelopeRequest(config); assertSentryTransaction(envelope[2], { transaction: 'Test Transaction', diff --git a/packages/node-integration-tests/suites/tracing/tracePropagationTargets/test.ts b/packages/node-integration-tests/suites/tracing/tracePropagationTargets/test.ts index 1ab85bf859c7..a861126c1aa5 100644 --- a/packages/node-integration-tests/suites/tracing/tracePropagationTargets/test.ts +++ b/packages/node-integration-tests/suites/tracing/tracePropagationTargets/test.ts @@ -27,8 +27,11 @@ test('HttpIntegration should instrument correct requests when tracePropagationTa .matchHeader('sentry-trace', val => val === undefined) .reply(200); - const serverUrl = await runServer(__dirname); - await runScenario(serverUrl); + const { url, scope, server } = await runServer(__dirname); + await runScenario(url); + + scope.persist(false); + await new Promise(resolve => server.close(resolve)); expect(match1.isDone()).toBe(true); expect(match2.isDone()).toBe(true); diff --git a/packages/node-integration-tests/utils/index.ts b/packages/node-integration-tests/utils/index.ts index 5e317864e344..d32220ddb9e2 100644 --- a/packages/node-integration-tests/utils/index.ts +++ b/packages/node-integration-tests/utils/index.ts @@ -7,6 +7,13 @@ import { RequestOptions } from 'https'; import nock from 'nock'; import * as path from 'path'; import { getPortPromise } from 'portfinder'; + +type TestServerConfig = { + url: string; + server: http.Server; + scope: nock.Scope; +}; + /** * Returns`describe` or `describe.skip` depending on allowed major versions of Node. * @@ -68,48 +75,69 @@ export const parseEnvelope = (body: string): Array> => { /** * Intercepts and extracts up to a number of requests containing Sentry envelopes. * - * @param url The url the intercepted requests will be directed to. + * @param config The url, server instance and the nock scope. * @param count The expected amount of requests to the envelope endpoint. If * the amount of sentrequests is lower than`count`, this function will not resolve. * @param method The method of the request. Defaults to `GET`. + * @param endServer: Whether to stop the server after the requests have been intercepted. * @returns The intercepted envelopes. */ export const getMultipleEnvelopeRequest = async ( - url: string, + config: TestServerConfig, count: number, method: 'get' | 'post' = 'get', + endServer: boolean = true, ): Promise[][]> => { - const envelopes: Record[][] = []; - // eslint-disable-next-line no-async-promise-executor - return new Promise(async resolve => { - nock('https://dsn.ingest.sentry.io') + return ( + await Promise.all([setupNock(config.scope, config.server, count, endServer), makeRequest(method, config.url)]) + )[0]; +}; + +const setupNock = async ( + scope: nock.Scope, + server: http.Server, + count: number, + endServer: boolean, +): Promise[][]> => { + return new Promise(resolve => { + const envelopes: Record[][] = []; + + scope .post('/api/1337/envelope/', body => { const envelope = parseEnvelope(body); + envelopes.push(envelope); if (count === envelopes.length) { + if (endServer) { + scope.persist(false); + server.close(() => { + resolve(envelopes); + }); + } resolve(envelopes); } return true; }) - .times(count) .query(true) // accept any query params - used for sentry_key param .reply(200); + }); +}; - try { - if (method === 'get') { - await axios.get(url); - } else { - await axios.post(url); - } - } catch (e) { - // We sometimes expect the request to fail, but not the test. - // So, we do nothing. - logger.warn(e); +const makeRequest = async (method: 'get' | 'post', url: string): Promise => { + try { + if (method === 'get') { + await axios.get(url); + } else { + await axios.post(url); } - }); + } catch (e) { + // We sometimes expect the request to fail, but not the test. + // So, we do nothing. + logger.warn(e); + } }; /** @@ -119,8 +147,10 @@ export const getMultipleEnvelopeRequest = async ( * @param {Record} [headers] * @return {*} {Promise} */ -export const getAPIResponse = async (url: URL, headers?: Record): Promise => { +export const getAPIResponse = async (config: TestServerConfig, headers?: Record): Promise => { return new Promise(resolve => { + const url = new URL(config.url); + http.get( headers ? ({ @@ -138,7 +168,10 @@ export const getAPIResponse = async (url: URL, headers?: Record) body += chunk; }); response.on('end', function () { - resolve(JSON.parse(body)); + config.scope.persist(false); + config.server.close(() => { + resolve(JSON.parse(body)); + }); }); }, ); @@ -153,10 +186,11 @@ export const getAPIResponse = async (url: URL, headers?: Record) * @returns The extracted envelope. */ export const getEnvelopeRequest = async ( - url: string, + config: TestServerConfig, method: 'get' | 'post' = 'get', + endServer: boolean = true, ): Promise>> => { - return (await getMultipleEnvelopeRequest(url, 1, method))[0]; + return (await getMultipleEnvelopeRequest(config, 1, method, endServer))[0]; }; /** @@ -167,12 +201,16 @@ export const getEnvelopeRequest = async ( * @param {string} [scenarioPath] * @return {*} {Promise} */ -export async function runServer(testDir: string, serverPath?: string, scenarioPath?: string): Promise { +export async function runServer( + testDir: string, + serverPath?: string, + scenarioPath?: string, +): Promise { const port = await getPortPromise(); const url = `http://localhost:${port}/test`; const defaultServerPath = path.resolve(process.cwd(), 'utils', 'defaults', 'server'); - await new Promise(resolve => { + const { server, scope } = await new Promise<{ server: http.Server; scope: nock.Scope }>(resolve => { // eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-member-access const app = require(serverPath || defaultServerPath).default as Express; @@ -185,14 +223,13 @@ export async function runServer(testDir: string, serverPath?: string, scenarioPa }); const server = app.listen(port, () => { - resolve(); - setTimeout(() => { - server.close(); - }, 4000); + const scope = nock('https://dsn.ingest.sentry.io').persist(); + + resolve({ server, scope }); }); }); - return url; + return { url, server, scope }; } export async function runScenario(serverUrl: string): Promise { From 71c1174d64550463b09ef286a336242abd18738d Mon Sep 17 00:00:00 2001 From: Onur Temizkan Date: Mon, 15 Aug 2022 13:34:36 +0100 Subject: [PATCH 2/5] Refactor Remix integration tests to use new structure. --- .../node-integration-tests/utils/index.ts | 2 +- packages/remix/test/integration/package.json | 1 + .../integration/test/server/action.test.ts | 18 ++++++------- .../integration/test/server/loader.test.ts | 18 ++++++------- .../integration/test/server/utils/helpers.ts | 26 +++++++++++++------ 5 files changed, 38 insertions(+), 27 deletions(-) diff --git a/packages/node-integration-tests/utils/index.ts b/packages/node-integration-tests/utils/index.ts index d32220ddb9e2..45315390f9c5 100644 --- a/packages/node-integration-tests/utils/index.ts +++ b/packages/node-integration-tests/utils/index.ts @@ -8,7 +8,7 @@ import nock from 'nock'; import * as path from 'path'; import { getPortPromise } from 'portfinder'; -type TestServerConfig = { +export type TestServerConfig = { url: string; server: http.Server; scope: nock.Scope; diff --git a/packages/remix/test/integration/package.json b/packages/remix/test/integration/package.json index 09f370fe8d69..5a9173a68f69 100644 --- a/packages/remix/test/integration/package.json +++ b/packages/remix/test/integration/package.json @@ -19,6 +19,7 @@ "@remix-run/dev": "^1.6.5", "@types/react": "^17.0.47", "@types/react-dom": "^17.0.17", + "nock": "^13.1.0", "typescript": "^4.2.4" }, "resolutions": { diff --git a/packages/remix/test/integration/test/server/action.test.ts b/packages/remix/test/integration/test/server/action.test.ts index a1fd810af020..ccb4c8a510f7 100644 --- a/packages/remix/test/integration/test/server/action.test.ts +++ b/packages/remix/test/integration/test/server/action.test.ts @@ -11,9 +11,9 @@ jest.spyOn(console, 'error').mockImplementation(); // Repeat tests for each adapter describe.each(['builtin', 'express'])('Remix API Actions with adapter = %s', adapter => { it('correctly instruments a parameterized Remix API action', async () => { - const baseURL = await runServer(adapter); - const url = `${baseURL}/action-json-response/123123`; - const envelope = await getEnvelopeRequest(url, 'post'); + const config = await runServer(adapter); + const url = `${config.url}/action-json-response/123123`; + const envelope = await getEnvelopeRequest({ ...config, url }, 'post'); const transaction = envelope[2]; assertSentryTransaction(transaction, { @@ -40,10 +40,10 @@ describe.each(['builtin', 'express'])('Remix API Actions with adapter = %s', ada }); it('reports an error thrown from the action', async () => { - const baseURL = await runServer(adapter); - const url = `${baseURL}/action-json-response/-1`; + const config = await runServer(adapter); + const url = `${config.url}/action-json-response/-1`; - const [transaction, event] = await getMultipleEnvelopeRequest(url, 2, 'post'); + const [transaction, event] = await getMultipleEnvelopeRequest({ ...config, url }, 2, 'post'); assertSentryTransaction(transaction[2], { contexts: { @@ -77,10 +77,10 @@ describe.each(['builtin', 'express'])('Remix API Actions with adapter = %s', ada }); it('handles a thrown 500 response', async () => { - const baseURL = await runServer(adapter); - const url = `${baseURL}/action-json-response/-2`; + const config = await runServer(adapter); + const url = `${config.url}/action-json-response/-2`; - const [transaction_1, event, transaction_2] = await getMultipleEnvelopeRequest(url, 3, 'post'); + const [transaction_1, event, transaction_2] = await getMultipleEnvelopeRequest({ ...config, url }, 3, 'post'); assertSentryTransaction(transaction_1[2], { contexts: { diff --git a/packages/remix/test/integration/test/server/loader.test.ts b/packages/remix/test/integration/test/server/loader.test.ts index be0d2c06c609..9b3a2fbd1671 100644 --- a/packages/remix/test/integration/test/server/loader.test.ts +++ b/packages/remix/test/integration/test/server/loader.test.ts @@ -11,10 +11,10 @@ jest.spyOn(console, 'error').mockImplementation(); // Repeat tests for each adapter describe.each(['builtin', 'express'])('Remix API Loaders with adapter = %s', adapter => { it('reports an error thrown from the loader', async () => { - const baseURL = await runServer(adapter); - const url = `${baseURL}/loader-json-response/-2`; + const config = await runServer(adapter); + const url = `${config.url}/loader-json-response/-2`; - let [transaction, event] = await getMultipleEnvelopeRequest(url, 2); + let [transaction, event] = await getMultipleEnvelopeRequest({ ...config, url }, 2); // The event envelope is returned before the transaction envelope when using express adapter. // We can update this when we merge the envelope filtering utility. @@ -52,9 +52,9 @@ describe.each(['builtin', 'express'])('Remix API Loaders with adapter = %s', ada }); it('correctly instruments a parameterized Remix API loader', async () => { - const baseURL = await runServer(adapter); - const url = `${baseURL}/loader-json-response/123123`; - const envelope = await getEnvelopeRequest(url); + const config = await runServer(adapter); + const url = `${config.url}/loader-json-response/123123`; + const envelope = await getEnvelopeRequest({ ...config, url }); const transaction = envelope[2]; assertSentryTransaction(transaction, { @@ -80,10 +80,10 @@ describe.each(['builtin', 'express'])('Remix API Loaders with adapter = %s', ada }); it('handles a thrown 500 response', async () => { - const baseURL = await runServer(adapter); - const url = `${baseURL}/loader-json-response/-1`; + const config = await runServer(adapter); + const url = `${config.url}/loader-json-response/-1`; - const [transaction_1, event, transaction_2] = await getMultipleEnvelopeRequest(url, 3); + const [transaction_1, event, transaction_2] = await getMultipleEnvelopeRequest({ ...config, url }, 3); assertSentryTransaction(transaction_1[2], { contexts: { diff --git a/packages/remix/test/integration/test/server/utils/helpers.ts b/packages/remix/test/integration/test/server/utils/helpers.ts index 1833ea878e1c..2e909002a4fe 100644 --- a/packages/remix/test/integration/test/server/utils/helpers.ts +++ b/packages/remix/test/integration/test/server/utils/helpers.ts @@ -2,6 +2,9 @@ import express from 'express'; import { createRequestHandler } from '@remix-run/express'; import { getPortPromise } from 'portfinder'; import { wrapExpressCreateRequestHandler } from '@sentry/remix'; +import type { TestServerConfig } from '../../../../../../node-integration-tests/utils'; +import nock from 'nock'; +import * as http from 'http'; export * from '../../../../../../node-integration-tests/utils'; @@ -9,20 +12,27 @@ export * from '../../../../../../node-integration-tests/utils'; * Runs a test server * @returns URL */ -export async function runServer(adapter: string = 'builtin'): Promise { +export async function runServer(adapter: string = 'builtin'): Promise { const requestHandlerFactory = adapter === 'express' ? wrapExpressCreateRequestHandler(createRequestHandler) : createRequestHandler; - const app = express(); const port = await getPortPromise(); - app.all('*', requestHandlerFactory({ build: require('../../../build') })); + const { server, scope } = await new Promise<{ server: http.Server; scope: nock.Scope }>(resolve => { + const app = express(); - const server = app.listen(port, () => { - setTimeout(() => { - server.close(); - }, 4000); + app.all('*', requestHandlerFactory({ build: require('../../../build') })); + + const server = app.listen(port, () => { + const scope = nock('https://dsn.ingest.sentry.io').persist(); + + resolve({ server, scope }); + }); }); - return `http://localhost:${port}`; + return { + url: `http://localhost:${port}`, + server, + scope, + }; } From c0da11e30224d1d4d8cd1c54cc66bf6f8289e5c1 Mon Sep 17 00:00:00 2001 From: Onur Temizkan Date: Mon, 15 Aug 2022 13:45:26 +0100 Subject: [PATCH 3/5] Remove timeout setting from Jest config. --- packages/node-integration-tests/jest.config.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/node-integration-tests/jest.config.js b/packages/node-integration-tests/jest.config.js index 54ea7ebfb22f..9a2862dd9b8d 100644 --- a/packages/node-integration-tests/jest.config.js +++ b/packages/node-integration-tests/jest.config.js @@ -4,5 +4,4 @@ module.exports = { globalSetup: '/utils/setup-tests.ts', ...baseConfig, testMatch: ['**/test.ts'], - testTimeout: 10000, }; From e519d55c113b30fd4f3d86993aee67ec0c467a68 Mon Sep 17 00:00:00 2001 From: Onur Temizkan Date: Tue, 16 Aug 2022 10:30:40 +0100 Subject: [PATCH 4/5] Update README with the updated structure details. --- packages/node-integration-tests/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/node-integration-tests/README.md b/packages/node-integration-tests/README.md index a68b4a86c60e..962b9f4b88dc 100644 --- a/packages/node-integration-tests/README.md +++ b/packages/node-integration-tests/README.md @@ -33,6 +33,9 @@ A custom server configuration can be used, supplying a script that exports a val `utils/` contains helpers and Sentry-specific assertions that can be used in (`test.ts`). +`runServer` utility function returns an object containing `url`, [`http.Server`](https://nodejs.org/dist/latest-v16.x/docs/api/http.html#class-httpserver) and [`nock.Scope`](https://github.com/nock/nock#usage) instances for the created server. The `url` property is the base URL for the server. The `http.Server` instance is used to finish the server eventually, and the `nock.Scope` instance is used to bind / unbind interceptors for the test case. + +The responsibility of ending the server and interceptor is delegated to the test case. Data collection helpers such as `getEnvelopeRequest` and `getAPIResponse` expect those instances to be available and finish them before their resolution. Test that do not use those helpers will need to end the server and interceptors manually. ## Running Tests Locally Tests can be run locally with: From cbfce50008a51ef907fa4867bece80ed48a84c6d Mon Sep 17 00:00:00 2001 From: Onur Temizkan Date: Tue, 16 Aug 2022 11:21:18 +0100 Subject: [PATCH 5/5] Wrap `count`, `method` and `endServer` in an option. --- .../addBreadcrumb/empty-obj/test.ts | 2 +- .../multiple_breadcrumbs/test.ts | 2 +- .../addBreadcrumb/simple_breadcrumb/test.ts | 2 +- .../captureException/catched-error/test.ts | 2 +- .../captureException/empty-obj/test.ts | 2 +- .../captureException/simple-error/test.ts | 2 +- .../captureMessage/simple_message/test.ts | 2 +- .../captureMessage/with_level/test.ts | 2 +- .../configureScope/set_properties/test.ts | 2 +- .../setContext/multiple-contexts/test.ts | 2 +- .../non-serializable-context/test.ts | 2 +- .../setContext/simple-context/test.ts | 2 +- .../setExtra/multiple-extras/test.ts | 2 +- .../setExtra/non-serializable-extra/test.ts | 2 +- .../public-api/setExtra/simple-extra/test.ts | 2 +- .../setExtras/consecutive-calls/test.ts | 2 +- .../setExtras/multiple-extras/test.ts | 2 +- .../public-api/setTag/with-primitives/test.ts | 2 +- .../setTags/with-primitives/test.ts | 2 +- .../public-api/setUser/unset_user/test.ts | 2 +- .../public-api/setUser/update_user/test.ts | 2 +- .../withScope/nested-scopes/test.ts | 2 +- .../crashed-session-aggregate/test.ts | 6 +-- .../errored-session-aggregate/test.ts | 6 +-- .../sessions/exited-session-aggregate/test.ts | 6 +-- .../node-integration-tests/utils/index.ts | 39 ++++++++++++------- .../integration/test/server/action.test.ts | 9 +++-- .../integration/test/server/loader.test.ts | 4 +- 28 files changed, 63 insertions(+), 51 deletions(-) diff --git a/packages/node-integration-tests/suites/public-api/addBreadcrumb/empty-obj/test.ts b/packages/node-integration-tests/suites/public-api/addBreadcrumb/empty-obj/test.ts index 48a394d346af..4b12eb0ef4d4 100644 --- a/packages/node-integration-tests/suites/public-api/addBreadcrumb/empty-obj/test.ts +++ b/packages/node-integration-tests/suites/public-api/addBreadcrumb/empty-obj/test.ts @@ -2,7 +2,7 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../ test('should add an empty breadcrumb, when an empty object is given', async () => { const config = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(config, 2); + const envelopes = await getMultipleEnvelopeRequest(config, { count: 2 }); const errorEnvelope = envelopes[1]; expect(errorEnvelope).toHaveLength(3); diff --git a/packages/node-integration-tests/suites/public-api/addBreadcrumb/multiple_breadcrumbs/test.ts b/packages/node-integration-tests/suites/public-api/addBreadcrumb/multiple_breadcrumbs/test.ts index b8095ac46a69..95b150153e07 100644 --- a/packages/node-integration-tests/suites/public-api/addBreadcrumb/multiple_breadcrumbs/test.ts +++ b/packages/node-integration-tests/suites/public-api/addBreadcrumb/multiple_breadcrumbs/test.ts @@ -2,7 +2,7 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../ test('should add multiple breadcrumbs', async () => { const config = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(config, 2); + const envelopes = await getMultipleEnvelopeRequest(config, { count: 2 }); assertSentryEvent(envelopes[1][2], { message: 'test_multi_breadcrumbs', diff --git a/packages/node-integration-tests/suites/public-api/addBreadcrumb/simple_breadcrumb/test.ts b/packages/node-integration-tests/suites/public-api/addBreadcrumb/simple_breadcrumb/test.ts index 440c6758abe0..d207f36d452d 100644 --- a/packages/node-integration-tests/suites/public-api/addBreadcrumb/simple_breadcrumb/test.ts +++ b/packages/node-integration-tests/suites/public-api/addBreadcrumb/simple_breadcrumb/test.ts @@ -2,7 +2,7 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../ test('should add a simple breadcrumb', async () => { const config = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(config, 2); + const envelopes = await getMultipleEnvelopeRequest(config, { count: 2 }); assertSentryEvent(envelopes[1][2], { message: 'test_simple', diff --git a/packages/node-integration-tests/suites/public-api/captureException/catched-error/test.ts b/packages/node-integration-tests/suites/public-api/captureException/catched-error/test.ts index 8f88bfceb3dc..4d42c8701cee 100644 --- a/packages/node-integration-tests/suites/public-api/captureException/catched-error/test.ts +++ b/packages/node-integration-tests/suites/public-api/captureException/catched-error/test.ts @@ -2,7 +2,7 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../ test('should work inside catch block', async () => { const config = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(config, 2); + const envelopes = await getMultipleEnvelopeRequest(config, { count: 2 }); const errorEnvelope = envelopes[1]; assertSentryEvent(errorEnvelope[2], { diff --git a/packages/node-integration-tests/suites/public-api/captureException/empty-obj/test.ts b/packages/node-integration-tests/suites/public-api/captureException/empty-obj/test.ts index 82e9d5563fd3..5e08dc6152cd 100644 --- a/packages/node-integration-tests/suites/public-api/captureException/empty-obj/test.ts +++ b/packages/node-integration-tests/suites/public-api/captureException/empty-obj/test.ts @@ -2,7 +2,7 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../ test('should capture an empty object', async () => { const config = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(config, 2); + const envelopes = await getMultipleEnvelopeRequest(config, { count: 2 }); const errorEnvelope = envelopes[1]; assertSentryEvent(errorEnvelope[2], { diff --git a/packages/node-integration-tests/suites/public-api/captureException/simple-error/test.ts b/packages/node-integration-tests/suites/public-api/captureException/simple-error/test.ts index 57d55fc1e369..69c517e0538f 100644 --- a/packages/node-integration-tests/suites/public-api/captureException/simple-error/test.ts +++ b/packages/node-integration-tests/suites/public-api/captureException/simple-error/test.ts @@ -2,7 +2,7 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../ test('should capture a simple error with message', async () => { const config = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(config, 2); + const envelopes = await getMultipleEnvelopeRequest(config, { count: 2 }); const errorEnvelope = envelopes[1]; assertSentryEvent(errorEnvelope[2], { diff --git a/packages/node-integration-tests/suites/public-api/captureMessage/simple_message/test.ts b/packages/node-integration-tests/suites/public-api/captureMessage/simple_message/test.ts index 98107da3a13a..fecbfb81aa60 100644 --- a/packages/node-integration-tests/suites/public-api/captureMessage/simple_message/test.ts +++ b/packages/node-integration-tests/suites/public-api/captureMessage/simple_message/test.ts @@ -2,7 +2,7 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../ test('should capture a simple message string', async () => { const config = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(config, 2); + const envelopes = await getMultipleEnvelopeRequest(config, { count: 2 }); const errorEnvelope = envelopes[1]; assertSentryEvent(errorEnvelope[2], { diff --git a/packages/node-integration-tests/suites/public-api/captureMessage/with_level/test.ts b/packages/node-integration-tests/suites/public-api/captureMessage/with_level/test.ts index e77651cf3691..1d27c28c4f7a 100644 --- a/packages/node-integration-tests/suites/public-api/captureMessage/with_level/test.ts +++ b/packages/node-integration-tests/suites/public-api/captureMessage/with_level/test.ts @@ -2,7 +2,7 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../ test('should capture with different severity levels', async () => { const config = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(config, 12); + const envelopes = await getMultipleEnvelopeRequest(config, { count: 12 }); assertSentryEvent(envelopes[1][2], { message: 'debug_message', diff --git a/packages/node-integration-tests/suites/public-api/configureScope/set_properties/test.ts b/packages/node-integration-tests/suites/public-api/configureScope/set_properties/test.ts index af1de39169d8..9d44c377ccfc 100644 --- a/packages/node-integration-tests/suites/public-api/configureScope/set_properties/test.ts +++ b/packages/node-integration-tests/suites/public-api/configureScope/set_properties/test.ts @@ -2,7 +2,7 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../ test('should set different properties of a scope', async () => { const config = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(config, 2); + const envelopes = await getMultipleEnvelopeRequest(config, { count: 2 }); const errorEnvelope = envelopes[1]; assertSentryEvent(errorEnvelope[2], { 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 index 00c908c55026..d025e850999e 100644 --- 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 @@ -4,7 +4,7 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../ test('should record multiple contexts', async () => { const config = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(config, 2); + const envelopes = await getMultipleEnvelopeRequest(config, { count: 2 }); const errorEnvelope = envelopes[1]; assertSentryEvent(errorEnvelope[2], { 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 index 92b7c3d6cbaf..f82052078bb5 100644 --- 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 @@ -4,7 +4,7 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../ test('should normalize non-serializable context', async () => { const config = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(config, 2); + const envelopes = await getMultipleEnvelopeRequest(config, { count: 2 }); const errorEnvelope = envelopes[1]; assertSentryEvent(errorEnvelope[2], { 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 index d1a8e8143f0c..60ff6a7113a4 100644 --- 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 @@ -4,7 +4,7 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../ test('should set a simple context', async () => { const config = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(config, 2); + const envelopes = await getMultipleEnvelopeRequest(config, { count: 2 }); const errorEnvelope = envelopes[1]; assertSentryEvent(errorEnvelope[2], { diff --git a/packages/node-integration-tests/suites/public-api/setExtra/multiple-extras/test.ts b/packages/node-integration-tests/suites/public-api/setExtra/multiple-extras/test.ts index a6cf6a17e844..0e4f98693a31 100644 --- a/packages/node-integration-tests/suites/public-api/setExtra/multiple-extras/test.ts +++ b/packages/node-integration-tests/suites/public-api/setExtra/multiple-extras/test.ts @@ -2,7 +2,7 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../ test('should record multiple extras of different types', async () => { const config = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(config, 2); + const envelopes = await getMultipleEnvelopeRequest(config, { count: 2 }); const errorEnvelope = envelopes[1]; assertSentryEvent(errorEnvelope[2], { diff --git a/packages/node-integration-tests/suites/public-api/setExtra/non-serializable-extra/test.ts b/packages/node-integration-tests/suites/public-api/setExtra/non-serializable-extra/test.ts index 83efc0735535..ba6c3d9200b3 100644 --- a/packages/node-integration-tests/suites/public-api/setExtra/non-serializable-extra/test.ts +++ b/packages/node-integration-tests/suites/public-api/setExtra/non-serializable-extra/test.ts @@ -2,7 +2,7 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../ test('should normalize non-serializable extra', async () => { const config = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(config, 2); + const envelopes = await getMultipleEnvelopeRequest(config, { count: 2 }); const errorEnvelope = envelopes[1]; assertSentryEvent(errorEnvelope[2], { diff --git a/packages/node-integration-tests/suites/public-api/setExtra/simple-extra/test.ts b/packages/node-integration-tests/suites/public-api/setExtra/simple-extra/test.ts index 47a387107b75..3897ee6fad7e 100644 --- a/packages/node-integration-tests/suites/public-api/setExtra/simple-extra/test.ts +++ b/packages/node-integration-tests/suites/public-api/setExtra/simple-extra/test.ts @@ -2,7 +2,7 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../ test('should set a simple extra', async () => { const config = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(config, 2); + const envelopes = await getMultipleEnvelopeRequest(config, { count: 2 }); const errorEnvelope = envelopes[1]; assertSentryEvent(errorEnvelope[2], { diff --git a/packages/node-integration-tests/suites/public-api/setExtras/consecutive-calls/test.ts b/packages/node-integration-tests/suites/public-api/setExtras/consecutive-calls/test.ts index c01656b238dd..9a33b920f3ea 100644 --- a/packages/node-integration-tests/suites/public-api/setExtras/consecutive-calls/test.ts +++ b/packages/node-integration-tests/suites/public-api/setExtras/consecutive-calls/test.ts @@ -2,7 +2,7 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../ test('should set extras from multiple consecutive calls', async () => { const config = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(config, 2); + const envelopes = await getMultipleEnvelopeRequest(config, { count: 2 }); const errorEnvelope = envelopes[1]; assertSentryEvent(errorEnvelope[2], { diff --git a/packages/node-integration-tests/suites/public-api/setExtras/multiple-extras/test.ts b/packages/node-integration-tests/suites/public-api/setExtras/multiple-extras/test.ts index cec73a28769b..104aa543ebd7 100644 --- a/packages/node-integration-tests/suites/public-api/setExtras/multiple-extras/test.ts +++ b/packages/node-integration-tests/suites/public-api/setExtras/multiple-extras/test.ts @@ -2,7 +2,7 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../ test('should record an extras object', async () => { const config = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(config, 2); + const envelopes = await getMultipleEnvelopeRequest(config, { count: 2 }); const errorEnvelope = envelopes[1]; assertSentryEvent(errorEnvelope[2], { diff --git a/packages/node-integration-tests/suites/public-api/setTag/with-primitives/test.ts b/packages/node-integration-tests/suites/public-api/setTag/with-primitives/test.ts index 2e5d504af1c4..0a7a0be1822f 100644 --- a/packages/node-integration-tests/suites/public-api/setTag/with-primitives/test.ts +++ b/packages/node-integration-tests/suites/public-api/setTag/with-primitives/test.ts @@ -2,7 +2,7 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../ test('should set primitive tags', async () => { const config = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(config, 2); + const envelopes = await getMultipleEnvelopeRequest(config, { count: 2 }); const envelope = envelopes[1]; assertSentryEvent(envelope[2], { diff --git a/packages/node-integration-tests/suites/public-api/setTags/with-primitives/test.ts b/packages/node-integration-tests/suites/public-api/setTags/with-primitives/test.ts index 2e5d504af1c4..0a7a0be1822f 100644 --- a/packages/node-integration-tests/suites/public-api/setTags/with-primitives/test.ts +++ b/packages/node-integration-tests/suites/public-api/setTags/with-primitives/test.ts @@ -2,7 +2,7 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../ test('should set primitive tags', async () => { const config = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(config, 2); + const envelopes = await getMultipleEnvelopeRequest(config, { count: 2 }); const envelope = envelopes[1]; assertSentryEvent(envelope[2], { diff --git a/packages/node-integration-tests/suites/public-api/setUser/unset_user/test.ts b/packages/node-integration-tests/suites/public-api/setUser/unset_user/test.ts index 71bfc068543a..ba11a7552a4e 100644 --- a/packages/node-integration-tests/suites/public-api/setUser/unset_user/test.ts +++ b/packages/node-integration-tests/suites/public-api/setUser/unset_user/test.ts @@ -4,7 +4,7 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../ test('should unset user', async () => { const config = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(config, 6); + const envelopes = await getMultipleEnvelopeRequest(config, { count: 6 }); assertSentryEvent(envelopes[1][2], { message: 'no_user', diff --git a/packages/node-integration-tests/suites/public-api/setUser/update_user/test.ts b/packages/node-integration-tests/suites/public-api/setUser/update_user/test.ts index 33b3f9ea47bf..ed2cc620c949 100644 --- a/packages/node-integration-tests/suites/public-api/setUser/update_user/test.ts +++ b/packages/node-integration-tests/suites/public-api/setUser/update_user/test.ts @@ -2,7 +2,7 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../ test('should update user', async () => { const config = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(config, 4); + const envelopes = await getMultipleEnvelopeRequest(config, { count: 4 }); assertSentryEvent(envelopes[1][2], { message: 'first_user', diff --git a/packages/node-integration-tests/suites/public-api/withScope/nested-scopes/test.ts b/packages/node-integration-tests/suites/public-api/withScope/nested-scopes/test.ts index 2244190fce26..28a4b106d554 100644 --- a/packages/node-integration-tests/suites/public-api/withScope/nested-scopes/test.ts +++ b/packages/node-integration-tests/suites/public-api/withScope/nested-scopes/test.ts @@ -4,7 +4,7 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../ test('should allow nested scoping', async () => { const config = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(config, 10); + const envelopes = await getMultipleEnvelopeRequest(config, { count: 10 }); assertSentryEvent(envelopes[1][2], { message: 'root_before', diff --git a/packages/node-integration-tests/suites/sessions/crashed-session-aggregate/test.ts b/packages/node-integration-tests/suites/sessions/crashed-session-aggregate/test.ts index 2472993f163d..0dbe7ae62dac 100644 --- a/packages/node-integration-tests/suites/sessions/crashed-session-aggregate/test.ts +++ b/packages/node-integration-tests/suites/sessions/crashed-session-aggregate/test.ts @@ -6,9 +6,9 @@ test('should aggregate successful and crashed sessions', async () => { const { url, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); const envelopes = await Promise.race([ - getMultipleEnvelopeRequest({ url: `${url}/success`, server, scope }, 2, 'get', false), - getMultipleEnvelopeRequest({ url: `${url}/error_unhandled`, server, scope }, 2, 'get', false), - getMultipleEnvelopeRequest({ url: `${url}/success_next`, server, scope }, 2, 'get', false), + getMultipleEnvelopeRequest({ url: `${url}/success`, server, scope }, { count: 2, endServer: false }), + getMultipleEnvelopeRequest({ url: `${url}/error_unhandled`, server, scope }, { count: 2, endServer: false }), + getMultipleEnvelopeRequest({ url: `${url}/success_next`, server, scope }, { count: 2, endServer: false }), ]); scope.persist(false); diff --git a/packages/node-integration-tests/suites/sessions/errored-session-aggregate/test.ts b/packages/node-integration-tests/suites/sessions/errored-session-aggregate/test.ts index 8e78efad15f9..ab48b364ab7d 100644 --- a/packages/node-integration-tests/suites/sessions/errored-session-aggregate/test.ts +++ b/packages/node-integration-tests/suites/sessions/errored-session-aggregate/test.ts @@ -6,9 +6,9 @@ test('should aggregate successful, crashed and erroneous sessions', async () => const { url, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); const envelopes = await Promise.race([ - getMultipleEnvelopeRequest({ url: `${url}/success`, server, scope }, 3, 'get', false), - getMultipleEnvelopeRequest({ url: `${url}/error_handled`, server, scope }, 3, 'get', false), - getMultipleEnvelopeRequest({ url: `${url}/error_unhandled`, server, scope }, 3, 'get', false), + getMultipleEnvelopeRequest({ url: `${url}/success`, server, scope }, { count: 3, endServer: false }), + getMultipleEnvelopeRequest({ url: `${url}/error_handled`, server, scope }, { count: 3, endServer: false }), + getMultipleEnvelopeRequest({ url: `${url}/error_unhandled`, server, scope }, { count: 3, endServer: false }), ]); scope.persist(false); diff --git a/packages/node-integration-tests/suites/sessions/exited-session-aggregate/test.ts b/packages/node-integration-tests/suites/sessions/exited-session-aggregate/test.ts index b8fb6f19e4c6..4bf608f49e4a 100644 --- a/packages/node-integration-tests/suites/sessions/exited-session-aggregate/test.ts +++ b/packages/node-integration-tests/suites/sessions/exited-session-aggregate/test.ts @@ -6,9 +6,9 @@ test('should aggregate successful sessions', async () => { const { url, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); const envelope = await Promise.race([ - getEnvelopeRequest({ url: `${url}/success`, server, scope }, 'get', false), - getEnvelopeRequest({ url: `${url}/success_next`, server, scope }, 'get', false), - getEnvelopeRequest({ url: `${url}/success_slow`, server, scope }, 'get', false), + getEnvelopeRequest({ url: `${url}/success`, server, scope }, { endServer: false }), + getEnvelopeRequest({ url: `${url}/success_next`, server, scope }, { endServer: false }), + getEnvelopeRequest({ url: `${url}/success_slow`, server, scope }, { endServer: false }), ]); scope.persist(false); diff --git a/packages/node-integration-tests/utils/index.ts b/packages/node-integration-tests/utils/index.ts index 45315390f9c5..f425b495f6c5 100644 --- a/packages/node-integration-tests/utils/index.ts +++ b/packages/node-integration-tests/utils/index.ts @@ -14,6 +14,18 @@ export type TestServerConfig = { scope: nock.Scope; }; +export type DataCollectorOptions = { + // The expected amount of requests to the envelope endpoint. + // If the amount of sentrequests is lower than`count`, this function will not resolve. + count?: number; + + // The method of the request. + method?: 'get' | 'post'; + + // Whether to stop the server after the requests have been intercepted + endServer?: boolean; +}; + /** * Returns`describe` or `describe.skip` depending on allowed major versions of Node. * @@ -75,22 +87,20 @@ export const parseEnvelope = (body: string): Array> => { /** * Intercepts and extracts up to a number of requests containing Sentry envelopes. * - * @param config The url, server instance and the nock scope. - * @param count The expected amount of requests to the envelope endpoint. If - * the amount of sentrequests is lower than`count`, this function will not resolve. - * @param method The method of the request. Defaults to `GET`. - * @param endServer: Whether to stop the server after the requests have been intercepted. + * @param {TestServerConfig} config The url, server instance and the nock scope. + * @param {DataCollectorOptions} options * @returns The intercepted envelopes. */ export const getMultipleEnvelopeRequest = async ( config: TestServerConfig, - count: number, - method: 'get' | 'post' = 'get', - endServer: boolean = true, + options: DataCollectorOptions, ): Promise[][]> => { // eslint-disable-next-line no-async-promise-executor return ( - await Promise.all([setupNock(config.scope, config.server, count, endServer), makeRequest(method, config.url)]) + await Promise.all([ + setupNock(config.scope, config.server, options.count || 1, options.endServer || true), + makeRequest(options.method || 'get', config.url), + ]) )[0]; }; @@ -143,7 +153,7 @@ const makeRequest = async (method: 'get' | 'post', url: string): Promise = /** * Sends a get request to given URL, with optional headers * - * @param {URL} url + * @param {TestServerConfig} config The url, server instance and the nock scope. * @param {Record} [headers] * @return {*} {Promise} */ @@ -181,16 +191,15 @@ export const getAPIResponse = async (config: TestServerConfig, headers?: Record< /** * Intercepts and extracts a single request containing a Sentry envelope * - * @param url The url the intercepted request will be directed to. - * @param method The method of the request. Defaults to `GET`. + * @param {TestServerConfig} config The url, server instance and the nock scope. + * @param {DataCollectorOptions} options * @returns The extracted envelope. */ export const getEnvelopeRequest = async ( config: TestServerConfig, - method: 'get' | 'post' = 'get', - endServer: boolean = true, + options?: DataCollectorOptions, ): Promise>> => { - return (await getMultipleEnvelopeRequest(config, 1, method, endServer))[0]; + return (await getMultipleEnvelopeRequest(config, { ...options, count: 1 }))[0]; }; /** diff --git a/packages/remix/test/integration/test/server/action.test.ts b/packages/remix/test/integration/test/server/action.test.ts index ccb4c8a510f7..accf02ed4068 100644 --- a/packages/remix/test/integration/test/server/action.test.ts +++ b/packages/remix/test/integration/test/server/action.test.ts @@ -13,7 +13,7 @@ describe.each(['builtin', 'express'])('Remix API Actions with adapter = %s', ada it('correctly instruments a parameterized Remix API action', async () => { const config = await runServer(adapter); const url = `${config.url}/action-json-response/123123`; - const envelope = await getEnvelopeRequest({ ...config, url }, 'post'); + const envelope = await getEnvelopeRequest({ ...config, url }, { method: 'post' }); const transaction = envelope[2]; assertSentryTransaction(transaction, { @@ -43,7 +43,7 @@ describe.each(['builtin', 'express'])('Remix API Actions with adapter = %s', ada const config = await runServer(adapter); const url = `${config.url}/action-json-response/-1`; - const [transaction, event] = await getMultipleEnvelopeRequest({ ...config, url }, 2, 'post'); + const [transaction, event] = await getMultipleEnvelopeRequest({ ...config, url }, { count: 2, method: 'post' }); assertSentryTransaction(transaction[2], { contexts: { @@ -80,7 +80,10 @@ describe.each(['builtin', 'express'])('Remix API Actions with adapter = %s', ada const config = await runServer(adapter); const url = `${config.url}/action-json-response/-2`; - const [transaction_1, event, transaction_2] = await getMultipleEnvelopeRequest({ ...config, url }, 3, 'post'); + const [transaction_1, event, transaction_2] = await getMultipleEnvelopeRequest( + { ...config, url }, + { count: 3, method: 'post' }, + ); assertSentryTransaction(transaction_1[2], { contexts: { diff --git a/packages/remix/test/integration/test/server/loader.test.ts b/packages/remix/test/integration/test/server/loader.test.ts index 9b3a2fbd1671..f5e3adb58c26 100644 --- a/packages/remix/test/integration/test/server/loader.test.ts +++ b/packages/remix/test/integration/test/server/loader.test.ts @@ -14,7 +14,7 @@ describe.each(['builtin', 'express'])('Remix API Loaders with adapter = %s', ada const config = await runServer(adapter); const url = `${config.url}/loader-json-response/-2`; - let [transaction, event] = await getMultipleEnvelopeRequest({ ...config, url }, 2); + let [transaction, event] = await getMultipleEnvelopeRequest({ ...config, url }, { count: 2 }); // The event envelope is returned before the transaction envelope when using express adapter. // We can update this when we merge the envelope filtering utility. @@ -83,7 +83,7 @@ describe.each(['builtin', 'express'])('Remix API Loaders with adapter = %s', ada const config = await runServer(adapter); const url = `${config.url}/loader-json-response/-1`; - const [transaction_1, event, transaction_2] = await getMultipleEnvelopeRequest({ ...config, url }, 3); + const [transaction_1, event, transaction_2] = await getMultipleEnvelopeRequest({ ...config, url }, { count: 3 }); assertSentryTransaction(transaction_1[2], { contexts: {