From 6585d9797bfe1efe5e8a6d3b35e6b400bab76e9c Mon Sep 17 00:00:00 2001 From: Onur Temizkan Date: Fri, 26 Aug 2022 12:33:26 +0100 Subject: [PATCH 1/3] ref(tests): Add `TestEnv` class to Node integration tests. --- .../suites/express/handle-error/test.ts | 6 +- .../baggage-header-assign/test.ts | 66 ++--- .../baggage-header-out-bad-tx-name/test.ts | 6 +- .../sentry-trace/baggage-header-out/test.ts | 6 +- .../test.ts | 19 +- .../baggage-other-vendors/test.ts | 15 +- .../baggage-transaction-name/test.ts | 6 +- .../sentry-trace/trace-header-assign/test.ts | 13 +- .../sentry-trace/trace-header-out/test.ts | 6 +- .../suites/express/tracing/test.ts | 18 +- .../addBreadcrumb/empty-obj/test.ts | 6 +- .../multiple_breadcrumbs/test.ts | 6 +- .../addBreadcrumb/simple_breadcrumb/test.ts | 6 +- .../captureException/catched-error/test.ts | 8 +- .../captureException/empty-obj/test.ts | 8 +- .../captureException/simple-error/test.ts | 8 +- .../captureMessage/simple_message/test.ts | 8 +- .../captureMessage/with_level/test.ts | 6 +- .../configureScope/clear_scope/test.ts | 6 +- .../configureScope/set_properties/test.ts | 6 +- .../setContext/multiple-contexts/test.ts | 10 +- .../non-serializable-context/test.ts | 10 +- .../setContext/simple-context/test.ts | 10 +- .../setExtra/multiple-extras/test.ts | 8 +- .../setExtra/non-serializable-extra/test.ts | 8 +- .../public-api/setExtra/simple-extra/test.ts | 8 +- .../setExtras/consecutive-calls/test.ts | 8 +- .../setExtras/multiple-extras/test.ts | 8 +- .../public-api/setTag/with-primitives/test.ts | 8 +- .../setTags/with-primitives/test.ts | 8 +- .../public-api/setUser/unset_user/test.ts | 6 +- .../public-api/setUser/update_user/test.ts | 6 +- .../startTransaction/basic-usage/test.ts | 6 +- .../with-nested-spans/test.ts | 6 +- .../withScope/nested-scopes/test.ts | 6 +- .../crashed-session-aggregate/test.ts | 22 +- .../errored-session-aggregate/test.ts | 12 +- .../sessions/exited-session-aggregate/test.ts | 12 +- .../suites/tracing/apollo-graphql/test.ts | 6 +- .../tracing/auto-instrument/mongodb/test.ts | 6 +- .../tracing/auto-instrument/mysql/test.ts | 6 +- .../suites/tracing/auto-instrument/pg/test.ts | 6 +- .../suites/tracing/prisma-orm/test.ts | 6 +- .../tracing/tracePropagationTargets/test.ts | 10 +- .../node-integration-tests/utils/index.ts | 277 +++++++++--------- .../integration/test/server/action.test.ts | 42 ++- .../integration/test/server/loader.test.ts | 48 ++- .../integration/test/server/utils/helpers.ts | 37 ++- 48 files changed, 396 insertions(+), 439 deletions(-) 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 47b74368933c..e8be4081203c 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'; +import { assertSentryEvent, TestEnv } from '../../../utils/index'; test('should capture and send Express controller error.', async () => { - const { url, server } = await runServer(__dirname, `${__dirname}/server.ts`); - const event = await getEnvelopeRequest({ url: `${url}/express`, server }); + const env = await TestEnv.init(__dirname, `${__dirname}/server.ts`); + const event = await env.getEnvelopeRequest({ url: `${env.url}/express` }); 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 ad4a33e94e25..432790cb3de9 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 @@ -1,17 +1,13 @@ import * as path from 'path'; -import { getAPIResponse, runServer } from '../../../../utils/index'; +import { TestEnv } from '../../../../utils/index'; import { TestAPIResponse } from '../server'; test('Should not overwrite baggage if the incoming request already has Sentry baggage data.', async () => { - const { url, server } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); - - const response = (await getAPIResponse( - { url: `${url}/express`, server }, - { - baggage: 'sentry-release=2.0.0,sentry-environment=myEnv', - }, - )) as TestAPIResponse; + const env = await TestEnv.init(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); + const response = (await env.getAPIResponse(`${env.url}/express`, { + baggage: 'sentry-release=2.0.0,sentry-environment=myEnv', + })) as TestAPIResponse; expect(response).toBeDefined(); expect(response).toMatchObject({ @@ -23,15 +19,12 @@ 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, server } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); + const env = await TestEnv.init(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); - const response = (await getAPIResponse( - { url: `${url}/express`, server }, - { - 'sentry-trace': '', - baggage: 'sentry-release=2.0.0,sentry-environment=myEnv,dogs=great', - }, - )) as TestAPIResponse; + const response = (await env.getAPIResponse(`${env.url}/express`, { + 'sentry-trace': '', + baggage: 'sentry-release=2.0.0,sentry-environment=myEnv,dogs=great', + })) as TestAPIResponse; expect(response).toBeDefined(); expect(response).toMatchObject({ @@ -43,14 +36,11 @@ 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, server } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); + const env = await TestEnv.init(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); - const response = (await getAPIResponse( - { url: `${url}/express`, server }, - { - 'sentry-trace': '', - }, - )) as TestAPIResponse; + const response = (await env.getAPIResponse(`${env.url}/express`, { + 'sentry-trace': '', + })) as TestAPIResponse; expect(response).toBeDefined(); expect(response).toMatchObject({ @@ -62,15 +52,12 @@ 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, server } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); + const env = await TestEnv.init(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); - const response = (await getAPIResponse( - { url: `${url}/express`, server }, - { - 'sentry-trace': '', - baggage: 'foo=bar', - }, - )) as TestAPIResponse; + const response = (await env.getAPIResponse(`${env.url}/express`, { + 'sentry-trace': '', + baggage: 'foo=bar', + })) as TestAPIResponse; expect(response).toBeDefined(); expect(response).toMatchObject({ @@ -82,9 +69,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, server } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); + const env = await TestEnv.init(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); - const response = (await getAPIResponse({ url: `${url}/express`, server }, {})) as TestAPIResponse; + const response = (await env.getAPIResponse(`${env.url}/express`, {})) as TestAPIResponse; expect(response).toBeDefined(); expect(response).toMatchObject({ @@ -99,14 +86,11 @@ 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, server } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); + const env = await TestEnv.init(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); - const response = (await getAPIResponse( - { url: `${url}/express`, server }, - { - baggage: 'foo=bar,bar=baz', - }, - )) as TestAPIResponse; + const response = (await env.getAPIResponse(`${env.url}/express`, { + 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 0db548c8c189..b4fa037a9d5c 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 @@ -1,12 +1,12 @@ import * as path from 'path'; -import { getAPIResponse, runServer } from '../../../../utils/index'; +import { TestEnv } from '../../../../utils/index'; import { TestAPIResponse } from '../server'; test('Does not include transaction name if transaction source is not set', async () => { - const { url, server } = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`); + const env = await TestEnv.init(__dirname, `${path.resolve(__dirname, '.')}/server.ts`); - const response = (await getAPIResponse({ url: `${url}/express`, server })) as TestAPIResponse; + const response = (await env.getAPIResponse(`${env.url}/express`)) 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 f3932a8ec885..2650f1d7afc2 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 @@ -1,12 +1,12 @@ import * as path from 'path'; -import { getAPIResponse, runServer } from '../../../../utils/index'; +import { TestEnv } from '../../../../utils/index'; import { TestAPIResponse } from '../server'; test('should attach a `baggage` header to an outgoing request.', async () => { - const { url, server } = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`); + const env = await TestEnv.init(__dirname, `${path.resolve(__dirname, '.')}/server.ts`); - const response = (await getAPIResponse({ url: `${url}/express`, server })) as TestAPIResponse; + const response = (await env.getAPIResponse(`${env.url}/express`)) 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 e57b738e43a3..71e60bc7c4f3 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 @@ -1,18 +1,15 @@ import * as path from 'path'; -import { getAPIResponse, runServer } from '../../../../utils/index'; +import { TestEnv } 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, server } = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`); + const env = await TestEnv.init(__dirname, `${path.resolve(__dirname, '.')}/server.ts`); - const response = (await getAPIResponse( - { url: `${url}/express`, server }, - { - 'sentry-trace': '', - baggage: 'sentry-release=2.1.0,sentry-environment=myEnv', - }, - )) as TestAPIResponse; + const response = (await env.getAPIResponse(`${env.url}/express`, { + 'sentry-trace': '', + baggage: 'sentry-release=2.1.0,sentry-environment=myEnv', + })) as TestAPIResponse; expect(response).toBeDefined(); expect(response).toMatchObject({ @@ -24,9 +21,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, server } = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`); + const env = await TestEnv.init(__dirname, `${path.resolve(__dirname, '.')}/server.ts`); - const response = (await getAPIResponse({ url: `${url}/express`, server }, {})) as TestAPIResponse; + const response = (await env.getAPIResponse(`${env.url}/express`, {})) 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 f52b90035c4d..3f50b71bc265 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 @@ -1,18 +1,15 @@ import * as path from 'path'; -import { getAPIResponse, runServer } from '../../../../utils/index'; +import { TestEnv } 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, server } = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`); + const env = await TestEnv.init(__dirname, `${path.resolve(__dirname, '.')}/server.ts`); - const response = (await getAPIResponse( - { url: `${url}/express`, server }, - { - 'sentry-trace': '', - baggage: 'sentry-release=2.0.0,sentry-environment=myEnv', - }, - )) as TestAPIResponse; + const response = (await env.getAPIResponse(`${env.url}/express`, { + '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 80850af04d27..d698fd2b7aa6 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 @@ -1,12 +1,12 @@ import * as path from 'path'; -import { getAPIResponse, runServer } from '../../../../utils/index'; +import { TestEnv } from '../../../../utils/index'; import { TestAPIResponse } from '../server'; test('Includes transaction in baggage if the transaction name is parameterized', async () => { - const { url, server } = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`); + const env = await TestEnv.init(__dirname, `${path.resolve(__dirname, '.')}/server.ts`); - const response = (await getAPIResponse({ url: `${url}/express`, server })) as TestAPIResponse; + const response = (await env.getAPIResponse(`${env.url}/express`)) 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 4d374f41c4b7..366051b9cac8 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 @@ -1,18 +1,15 @@ import { TRACEPARENT_REGEXP } from '@sentry/utils'; import * as path from 'path'; -import { getAPIResponse, runServer } from '../../../../utils/index'; +import { TestEnv } 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, server } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); + const env = await TestEnv.init(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); - const response = (await getAPIResponse( - { url: `${url}/express`, server }, - { - 'sentry-trace': '12312012123120121231201212312012-1121201211212012-0', - }, - )) as TestAPIResponse; + const response = (await env.getAPIResponse(`${env.url}/express`, { + '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 983fdc59095a..bd4e29e7a807 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 @@ -1,13 +1,13 @@ import { TRACEPARENT_REGEXP } from '@sentry/utils'; import * as path from 'path'; -import { getAPIResponse, runServer } from '../../../../utils/index'; +import { TestEnv } from '../../../../utils/index'; import { TestAPIResponse } from '../server'; test('should attach a `sentry-trace` header to an outgoing request.', async () => { - const { url, server } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); + const env = await TestEnv.init(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); - const response = (await getAPIResponse({ url: `${url}/express`, server })) as TestAPIResponse; + const response = (await env.getAPIResponse(`${env.url}/express`)) 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 e48310c34660..b97ce2991053 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'; +import { assertSentryTransaction, TestEnv } from '../../../utils/index'; test('should create and send transactions for Express routes and spans for middlewares.', async () => { - const { url, server } = await runServer(__dirname, `${__dirname}/server.ts`); - const envelope = await getEnvelopeRequest({ url: `${url}/express`, server }, { envelopeType: 'transaction' }); + const env = await TestEnv.init(__dirname, `${__dirname}/server.ts`); + const envelope = await env.getEnvelopeRequest({ url: `${env.url}/express`, envelopeType: 'transaction' }); 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, server } = await runServer(__dirname, `${__dirname}/server.ts`); - const envelope = await getEnvelopeRequest({ url: `${url}/regex`, server }, { envelopeType: 'transaction' }); + const env = await TestEnv.init(__dirname, `${__dirname}/server.ts`); + const envelope = await env.getEnvelopeRequest({ url: `${env.url}/regex`, envelopeType: 'transaction' }); 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, server } = await runServer(__dirname, `${__dirname}/server.ts`); - const envelope = await getEnvelopeRequest({ url: `${url}/${segment}`, server }, { envelopeType: 'transaction' }); + const env = await TestEnv.init(__dirname, `${__dirname}/server.ts`); + const envelope = await env.getEnvelopeRequest({ url: `${env.url}/${segment}`, envelopeType: 'transaction' }); 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, server } = await runServer(__dirname, `${__dirname}/server.ts`); - const envelope = await getEnvelopeRequest({ url: `${url}/${segment}`, server }, { envelopeType: 'transaction' }); + const env = await TestEnv.init(__dirname, `${__dirname}/server.ts`); + const envelope = await env.getEnvelopeRequest({ url: `${env.url}/${segment}`, envelopeType: 'transaction' }); 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 d8c95360f703..f384ccf292dc 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, getEnvelopeRequest, runServer } from '../../../../utils'; +import { assertSentryEvent, TestEnv } from '../../../../utils'; test('should add an empty breadcrumb, when an empty object is given', async () => { - const config = await runServer(__dirname); - const envelope = await getEnvelopeRequest(config); + const env = await TestEnv.init(__dirname); + const envelope = await env.getEnvelopeRequest(); expect(envelope).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 9f5ca599f891..30dbfa54108f 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, getEnvelopeRequest, runServer } from '../../../../utils'; +import { assertSentryEvent, TestEnv } from '../../../../utils'; test('should add multiple breadcrumbs', async () => { - const config = await runServer(__dirname); - const events = await getEnvelopeRequest(config); + const env = await TestEnv.init(__dirname); + const events = await env.getEnvelopeRequest(); assertSentryEvent(events[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 146d9052c24a..9fb42f2f4685 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, getEnvelopeRequest, runServer } from '../../../../utils'; +import { assertSentryEvent, TestEnv } from '../../../../utils'; test('should add a simple breadcrumb', async () => { - const config = await runServer(__dirname); - const event = await getEnvelopeRequest(config); + const env = await TestEnv.init(__dirname); + const event = await env.getEnvelopeRequest(); assertSentryEvent(event[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 e09f29fae48a..b4939d4d8b4a 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,10 +1,10 @@ -import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils'; +import { assertSentryEvent, TestEnv } from '../../../../utils'; test('should work inside catch block', async () => { - const config = await runServer(__dirname); - const events = await getMultipleEnvelopeRequest(config, { count: 1 }); + const env = await TestEnv.init(__dirname); + const event = await env.getEnvelopeRequest(); - assertSentryEvent(events[0][2], { + assertSentryEvent(event[2], { exception: { values: [ { 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 d0fca28137c6..b30b64c426fd 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,10 +1,10 @@ -import { assertSentryEvent, getEnvelopeRequest, runServer } from '../../../../utils'; +import { assertSentryEvent, TestEnv } from '../../../../utils'; test('should capture an empty object', async () => { - const config = await runServer(__dirname); - const events = await getEnvelopeRequest(config); + const env = await TestEnv.init(__dirname); + const event = await env.getEnvelopeRequest(); - assertSentryEvent(events[2], { + assertSentryEvent(event[2], { exception: { values: [ { 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 f09d75769385..ab09786380b4 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,10 +1,10 @@ -import { assertSentryEvent, getEnvelopeRequest, runServer } from '../../../../utils'; +import { assertSentryEvent, TestEnv } from '../../../../utils'; test('should capture a simple error with message', async () => { - const config = await runServer(__dirname); - const events = await getEnvelopeRequest(config); + const env = await TestEnv.init(__dirname); + const envelope = await env.getEnvelopeRequest(); - assertSentryEvent(events[2], { + assertSentryEvent(envelope[2], { exception: { values: [ { 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 bc940f99eafc..421360d460b7 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,10 +1,10 @@ -import { assertSentryEvent, getEnvelopeRequest, runServer } from '../../../../utils'; +import { assertSentryEvent, TestEnv } from '../../../../utils'; test('should capture a simple message string', async () => { - const config = await runServer(__dirname); - const events = await getEnvelopeRequest(config); + const env = await TestEnv.init(__dirname); + const event = await env.getEnvelopeRequest(); - assertSentryEvent(events[2], { + assertSentryEvent(event[2], { message: 'Message', level: 'info', }); 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 ff327c0c866d..e7f20f96fe9e 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'; +import { assertSentryEvent, TestEnv } from '../../../../utils'; test('should capture with different severity levels', async () => { - const config = await runServer(__dirname); - const events = await getMultipleEnvelopeRequest(config, { count: 6 }); + const env = await TestEnv.init(__dirname); + const events = await env.getMultipleEnvelopeRequest({ count: 6 }); assertSentryEvent(events[0][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 dc9a62621f84..63c82f7b4054 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 @@ -1,10 +1,10 @@ import { Event } from '@sentry/node'; -import { assertSentryEvent, getEnvelopeRequest, runServer } from '../../../../utils'; +import { assertSentryEvent, TestEnv } from '../../../../utils'; test('should clear previously set properties of a scope', async () => { - const config = await runServer(__dirname); - const envelope = await getEnvelopeRequest(config); + const env = await TestEnv.init(__dirname); + const envelope = await env.getEnvelopeRequest(); 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 353c7ff2a951..43f82ac64383 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, getEnvelopeRequest, runServer } from '../../../../utils'; +import { assertSentryEvent, TestEnv } from '../../../../utils'; test('should set different properties of a scope', async () => { - const config = await runServer(__dirname); - const envelope = await getEnvelopeRequest(config); + const env = await TestEnv.init(__dirname); + const envelope = await env.getEnvelopeRequest(); assertSentryEvent(envelope[2], { message: 'configured_scope', 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 a365f63af9fa..aaf53e26d83f 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 @@ -1,12 +1,12 @@ import { Event } from '@sentry/node'; -import { assertSentryEvent, getEnvelopeRequest, runServer } from '../../../../utils'; +import { assertSentryEvent, TestEnv } from '../../../../utils'; test('should record multiple contexts', async () => { - const config = await runServer(__dirname); - const events = await getEnvelopeRequest(config); + const env = await TestEnv.init(__dirname); + const envelope = await env.getEnvelopeRequest(); - assertSentryEvent(events[2], { + assertSentryEvent(envelope[2], { message: 'multiple_contexts', contexts: { context_1: { @@ -17,5 +17,5 @@ test('should record multiple contexts', async () => { }, }); - expect((events[0] as Event).contexts?.context_3).not.toBeDefined(); + expect((envelope[0] as Event).contexts?.context_3).not.toBeDefined(); }); 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 d685215aa0c7..03c8727d3b7e 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 @@ -1,15 +1,15 @@ import { Event } from '@sentry/node'; -import { assertSentryEvent, getEnvelopeRequest, runServer } from '../../../../utils'; +import { assertSentryEvent, TestEnv } from '../../../../utils'; test('should normalize non-serializable context', async () => { - const config = await runServer(__dirname); - const events = await getEnvelopeRequest(config); + const env = await TestEnv.init(__dirname); + const event = await env.getEnvelopeRequest(); - assertSentryEvent(events[2], { + assertSentryEvent(event[2], { message: 'non_serializable', contexts: {}, }); - expect((events[0] as Event).contexts?.context_3).not.toBeDefined(); + expect((event[0] as Event).contexts?.context_3).not.toBeDefined(); }); 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 16a4ab8486d3..94758652bbfb 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 @@ -1,12 +1,12 @@ import { Event } from '@sentry/node'; -import { assertSentryEvent, getEnvelopeRequest, runServer } from '../../../../utils'; +import { assertSentryEvent, TestEnv } from '../../../../utils'; test('should set a simple context', async () => { - const config = await runServer(__dirname); - const envelopes = await getEnvelopeRequest(config); + const env = await TestEnv.init(__dirname); + const envelope = await env.getEnvelopeRequest(); - assertSentryEvent(envelopes[2], { + assertSentryEvent(envelope[2], { message: 'simple_context_object', contexts: { foo: { @@ -15,5 +15,5 @@ test('should set a simple context', async () => { }, }); - expect((envelopes[2] as Event).contexts?.context_3).not.toBeDefined(); + expect((envelope[2] as Event).contexts?.context_3).not.toBeDefined(); }); 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 0bad46b894de..6caf42fc4b2b 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,10 +1,10 @@ -import { assertSentryEvent, getEnvelopeRequest, runServer } from '../../../../utils'; +import { assertSentryEvent, TestEnv } from '../../../../utils'; test('should record multiple extras of different types', async () => { - const config = await runServer(__dirname); - const events = await getEnvelopeRequest(config); + const env = await TestEnv.init(__dirname); + const event = await env.getEnvelopeRequest(); - assertSentryEvent(events[2], { + assertSentryEvent(event[2], { message: 'multiple_extras', extra: { extra_1: { foo: 'bar', baz: { qux: 'quux' } }, 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 9af242805d36..167a636d3ed8 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,10 +1,10 @@ -import { assertSentryEvent, getEnvelopeRequest, runServer } from '../../../../utils'; +import { assertSentryEvent, TestEnv } from '../../../../utils'; test('should normalize non-serializable extra', async () => { - const config = await runServer(__dirname); - const events = await getEnvelopeRequest(config); + const env = await TestEnv.init(__dirname); + const event = await env.getEnvelopeRequest(); - assertSentryEvent(events[2], { + assertSentryEvent(event[2], { message: 'non_serializable', extra: {}, }); 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 4441f0939000..952db2f17dee 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,10 +1,10 @@ -import { assertSentryEvent, getEnvelopeRequest, runServer } from '../../../../utils'; +import { assertSentryEvent, TestEnv } from '../../../../utils'; test('should set a simple extra', async () => { - const config = await runServer(__dirname); - const events = await getEnvelopeRequest(config); + const env = await TestEnv.init(__dirname); + const event = await env.getEnvelopeRequest(); - assertSentryEvent(events[2], { + assertSentryEvent(event[2], { message: 'simple_extra', extra: { foo: { 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 87a5f10994d2..bec43b67e6cd 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,10 +1,10 @@ -import { assertSentryEvent, getEnvelopeRequest, runServer } from '../../../../utils'; +import { assertSentryEvent, TestEnv } from '../../../../utils'; test('should set extras from multiple consecutive calls', async () => { - const config = await runServer(__dirname); - const envelopes = await getEnvelopeRequest(config); + const env = await TestEnv.init(__dirname); + const envelope = await env.getEnvelopeRequest(); - assertSentryEvent(envelopes[2], { + assertSentryEvent(envelope[2], { message: 'consecutive_calls', extra: { extra: [], Infinity: 2, null: 0, obj: { foo: ['bar', 'baz', 1] } }, }); 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 4174ca98df93..a860d5654177 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,10 +1,10 @@ -import { assertSentryEvent, getEnvelopeRequest, runServer } from '../../../../utils'; +import { assertSentryEvent, TestEnv } from '../../../../utils'; test('should record an extras object', async () => { - const config = await runServer(__dirname); - const events = await getEnvelopeRequest(config); + const env = await TestEnv.init(__dirname); + const event = await env.getEnvelopeRequest(); - assertSentryEvent(events[2], { + assertSentryEvent(event[2], { message: 'multiple_extras', extra: { extra_1: [1, ['foo'], 'bar'], 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 16c81c323a58..11a4eee61f30 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,10 +1,10 @@ -import { assertSentryEvent, getEnvelopeRequest, runServer } from '../../../../utils'; +import { assertSentryEvent, TestEnv } from '../../../../utils'; test('should set primitive tags', async () => { - const config = await runServer(__dirname); - const events = await getEnvelopeRequest(config); + const env = await TestEnv.init(__dirname); + const event = await env.getEnvelopeRequest(); - assertSentryEvent(events[2], { + assertSentryEvent(event[2], { message: 'primitive_tags', tags: { tag_1: 'foo', 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 16c81c323a58..11a4eee61f30 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,10 +1,10 @@ -import { assertSentryEvent, getEnvelopeRequest, runServer } from '../../../../utils'; +import { assertSentryEvent, TestEnv } from '../../../../utils'; test('should set primitive tags', async () => { - const config = await runServer(__dirname); - const events = await getEnvelopeRequest(config); + const env = await TestEnv.init(__dirname); + const event = await env.getEnvelopeRequest(); - assertSentryEvent(events[2], { + assertSentryEvent(event[2], { message: 'primitive_tags', tags: { tag_1: 'foo', 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 2f9934bd87e7..1945608623f2 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 @@ -1,10 +1,10 @@ import { Event } from '@sentry/node'; -import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils'; +import { assertSentryEvent, TestEnv } from '../../../../utils'; test('should unset user', async () => { - const config = await runServer(__dirname); - const events = await getMultipleEnvelopeRequest(config, { count: 3 }); + const env = await TestEnv.init(__dirname); + const events = await env.getMultipleEnvelopeRequest({ count: 3 }); assertSentryEvent(events[0][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 442b1982dd2a..40c3c663a502 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'; +import { assertSentryEvent, TestEnv } from '../../../../utils'; test('should update user', async () => { - const config = await runServer(__dirname); - const envelopes = await getMultipleEnvelopeRequest(config, { count: 2 }); + const env = await TestEnv.init(__dirname); + const envelopes = await env.getMultipleEnvelopeRequest({ count: 2 }); assertSentryEvent(envelopes[0][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 1583a09e3e4a..6a5377e2717b 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'; +import { assertSentryTransaction, TestEnv } from '../../../../utils'; test('should send a manually started transaction when @sentry/tracing is imported using unnamed import.', async () => { - const config = await runServer(__dirname); - const envelope = await getEnvelopeRequest(config, { envelopeType: 'transaction' }); + const env = await TestEnv.init(__dirname); + const envelope = await env.getEnvelopeRequest({ envelopeType: 'transaction' }); assertSentryTransaction(envelope[2], { transaction: 'test_transaction_1', 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 84712ef3dbc3..0f23709ae9cf 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'; +import { assertSentryTransaction, TestEnv } from '../../../../utils'; test('should report finished spans as children of the root transaction.', async () => { - const config = await runServer(__dirname); - const envelope = await getEnvelopeRequest(config, { envelopeType: 'transaction' }); + const env = await TestEnv.init(__dirname); + const envelope = await env.getEnvelopeRequest({ envelopeType: 'transaction' }); 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 daea172170c9..a9dcdcf7bd9d 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 @@ -1,10 +1,10 @@ import { Event } from '@sentry/node'; -import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils'; +import { assertSentryEvent, TestEnv } from '../../../../utils'; test('should allow nested scoping', async () => { - const config = await runServer(__dirname); - const events = await getMultipleEnvelopeRequest(config, { count: 5 }); + const env = await TestEnv.init(__dirname); + const events = await env.getMultipleEnvelopeRequest({ count: 5 }); assertSentryEvent(events[0][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 742e13d4d87d..e47bafb4797a 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 @@ -1,27 +1,25 @@ import nock from 'nock'; import path from 'path'; -import { getMultipleEnvelopeRequest, runServer } from '../../../utils'; +import { TestEnv } from '../../../utils'; test('should aggregate successful and crashed sessions', async () => { - const { url, server } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); + const env = await TestEnv.init(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); const envelope = ( await Promise.race([ - getMultipleEnvelopeRequest({ url: `${url}/success`, server }, { endServer: false, envelopeType: 'sessions' }), - getMultipleEnvelopeRequest( - { url: `${url}/error_unhandled`, server }, - { endServer: false, envelopeType: 'sessions' }, - ), - getMultipleEnvelopeRequest( - { url: `${url}/success_next`, server }, - { endServer: false, envelopeType: 'sessions' }, - ), + env.getMultipleEnvelopeRequest({ url: `${env.url}/success`, endServer: false, envelopeType: 'sessions' }), + env.getMultipleEnvelopeRequest({ url: `${env.url}/error_unhandled`, endServer: false, envelopeType: 'sessions' }), + env.getMultipleEnvelopeRequest({ + url: `${env.url}/success_next`, + endServer: false, + envelopeType: 'sessions', + }), ]) )[0]; nock.cleanAll(); - server.close(); + env.server.close(); expect(envelope[0]).toMatchObject({ sent_at: expect.any(String), 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 4a04ba331108..0112da40d1f4 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 @@ -1,17 +1,17 @@ import path from 'path'; -import { getEnvelopeRequest, runServer } from '../../../utils'; +import { TestEnv } from '../../../utils'; test('should aggregate successful, crashed and erroneous sessions', async () => { - const { url, server } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); + const env = await TestEnv.init(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); const aggregateSessionEnvelope = await Promise.race([ - getEnvelopeRequest({ url: `${url}/success`, server }, { endServer: false, envelopeType: 'sessions' }), - getEnvelopeRequest({ url: `${url}/error_handled`, server }, { endServer: false, envelopeType: 'sessions' }), - getEnvelopeRequest({ url: `${url}/error_unhandled`, server }, { endServer: false, envelopeType: 'sessions' }), + env.getEnvelopeRequest({ url: `${env.url}/success`, endServer: false, envelopeType: 'sessions' }), + env.getEnvelopeRequest({ url: `${env.url}/error_handled`, endServer: false, envelopeType: 'sessions' }), + env.getEnvelopeRequest({ url: `${env.url}/error_unhandled`, endServer: false, envelopeType: 'sessions' }), ]); - await new Promise(resolve => server.close(resolve)); + await new Promise(resolve => env.server.close(resolve)); expect(aggregateSessionEnvelope[0]).toMatchObject({ sent_at: expect.any(String), 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 1d8c2d72a651..c610533aa605 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 @@ -1,19 +1,19 @@ import nock from 'nock'; import path from 'path'; -import { getEnvelopeRequest, runServer } from '../../../utils'; +import { TestEnv } from '../../../utils'; test('should aggregate successful sessions', async () => { - const { url, server } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); + const env = await TestEnv.init(__dirname, `${path.resolve(__dirname, '..')}/server.ts`); const envelope = await Promise.race([ - getEnvelopeRequest({ url: `${url}/success`, server }, { endServer: false, envelopeType: 'sessions' }), - getEnvelopeRequest({ url: `${url}/success_next`, server }, { endServer: false, envelopeType: 'sessions' }), - getEnvelopeRequest({ url: `${url}/success_slow`, server }, { endServer: false, envelopeType: 'sessions' }), + env.getEnvelopeRequest({ url: `${env.url}/success`, endServer: false, envelopeType: 'sessions' }), + env.getEnvelopeRequest({ url: `${env.url}/success_next`, endServer: false, envelopeType: 'sessions' }), + env.getEnvelopeRequest({ url: `${env.url}/success_slow`, endServer: false, envelopeType: 'sessions' }), ]); nock.cleanAll(); - server.close(); + env.server.close(); expect(envelope).toHaveLength(3); expect(envelope[0]).toMatchObject({ 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 8ba91822c704..d78afdcb38b5 100644 --- a/packages/node-integration-tests/suites/tracing/apollo-graphql/test.ts +++ b/packages/node-integration-tests/suites/tracing/apollo-graphql/test.ts @@ -1,11 +1,11 @@ -import { assertSentryTransaction, conditionalTest, getEnvelopeRequest, runServer } from '../../../utils'; +import { assertSentryTransaction, conditionalTest, TestEnv } from '../../../utils'; // Node 10 is not supported by `graphql-js` // 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 config = await runServer(__dirname); - const envelope = await getEnvelopeRequest(config, { envelopeType: 'transaction' }); + const env = await TestEnv.init(__dirname); + const envelope = await env.getEnvelopeRequest({ envelopeType: 'transaction' }); 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 44a6d9543642..fda3695c1eb3 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 @@ -1,6 +1,6 @@ import { MongoMemoryServer } from 'mongodb-memory-server-global'; -import { assertSentryTransaction, conditionalTest, getEnvelopeRequest, runServer } from '../../../../utils'; +import { assertSentryTransaction, conditionalTest, TestEnv } from '../../../../utils'; // This test can take longer. jest.setTimeout(15000); @@ -20,8 +20,8 @@ conditionalTest({ min: 12 })('MongoDB Test', () => { }); test('should auto-instrument `mongodb` package.', async () => { - const config = await runServer(__dirname); - const envelope = await getEnvelopeRequest(config, { envelopeType: 'transaction' }); + const env = await TestEnv.init(__dirname); + const envelope = await env.getEnvelopeRequest({ envelopeType: 'transaction' }); 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 bcd451ffe8d6..3b96f2cafec0 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'; +import { assertSentryTransaction, TestEnv } from '../../../../utils'; test('should auto-instrument `mysql` package.', async () => { - const config = await runServer(__dirname); - const envelope = await getEnvelopeRequest(config, { envelopeType: 'transaction' }); + const env = await TestEnv.init(__dirname); + const envelope = await env.getEnvelopeRequest({ envelopeType: 'transaction' }); 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 bea37770b5e6..edfa67cee9d7 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 @@ -1,4 +1,4 @@ -import { assertSentryTransaction, getEnvelopeRequest, runServer } from '../../../../utils'; +import { assertSentryTransaction, TestEnv } from '../../../../utils'; class PgClient { // https://node-postgres.com/api/client#clientquery @@ -29,8 +29,8 @@ beforeAll(() => { }); test('should auto-instrument `pg` package.', async () => { - const config = await runServer(__dirname); - const envelope = await getEnvelopeRequest(config, { envelopeType: 'transaction' }); + const env = await TestEnv.init(__dirname); + const envelope = await env.getEnvelopeRequest({ envelopeType: 'transaction' }); 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 a75578c33006..acb1ed87941c 100644 --- a/packages/node-integration-tests/suites/tracing/prisma-orm/test.ts +++ b/packages/node-integration-tests/suites/tracing/prisma-orm/test.ts @@ -1,9 +1,9 @@ -import { assertSentryTransaction, conditionalTest, getEnvelopeRequest, runServer } from '../../../utils'; +import { assertSentryTransaction, conditionalTest, TestEnv } from '../../../utils'; conditionalTest({ min: 12 })('Prisma ORM Integration', () => { test('should instrument Prisma client for tracing.', async () => { - const config = await runServer(__dirname); - const envelope = await getEnvelopeRequest(config, { envelopeType: 'transaction' }); + const env = await TestEnv.init(__dirname); + const envelope = await env.getEnvelopeRequest({ envelopeType: 'transaction' }); 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 dfed6feea099..01b75ab10330 100644 --- a/packages/node-integration-tests/suites/tracing/tracePropagationTargets/test.ts +++ b/packages/node-integration-tests/suites/tracing/tracePropagationTargets/test.ts @@ -1,6 +1,6 @@ import nock from 'nock'; -import { runScenario, runServer } from '../../../utils'; +import { TestEnv, runScenario } from '../../../utils'; test('HttpIntegration should instrument correct requests when tracePropagationTargets option is provided', async () => { const match1 = nock('http://match-this-url.com') @@ -27,13 +27,13 @@ test('HttpIntegration should instrument correct requests when tracePropagationTa .matchHeader('sentry-trace', val => val === undefined) .reply(200); - const { url, server } = await runServer(__dirname); - await runScenario(url); + const env = await TestEnv.init(__dirname); + await runScenario(env.url); - server.close(); + env.server.close(); nock.cleanAll(); - await new Promise(resolve => server.close(resolve)); + await new Promise(resolve => env.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 4724eabd0246..5af54b065686 100644 --- a/packages/node-integration-tests/utils/index.ts +++ b/packages/node-integration-tests/utils/index.ts @@ -15,6 +15,9 @@ export type TestServerConfig = { }; export type DataCollectorOptions = { + // Optional custom URL + url?: string; + // The expected amount of requests to the envelope endpoint. // If the amount of sent requests is lower than `count`, this function will not resolve. count?: number; @@ -88,78 +91,18 @@ export const parseEnvelope = (body: string): Array> => { }; /** - * Intercepts and extracts up to a number of requests containing Sentry envelopes. + * Sends a get request to given URL. + * Flushes the Sentry event queue. * - * @param {TestServerConfig} config The url and the server instance. - * @param {DataCollectorOptions} options - * @returns The intercepted envelopes. + * @param {string} url + * @return {*} {Promise} */ -export const getMultipleEnvelopeRequest = async ( - config: TestServerConfig, - options: DataCollectorOptions, -): Promise[][]> => { - const envelopeTypeArray = - typeof options.envelopeType === 'string' - ? [options.envelopeType] - : options.envelopeType || (['event'] as EnvelopeItemType[]); - - const resProm = setupNock( - config.server, - options.count || 1, - typeof options.endServer === 'undefined' ? true : options.endServer, - envelopeTypeArray, - ); - - void makeRequest(options.method || 'get', config.url); - return resProm; -}; - -const setupNock = async ( - server: http.Server, - count: number, - endServer: boolean, - envelopeType: EnvelopeItemType[], -): Promise[][]> => { - return new Promise(resolve => { - const envelopes: Record[][] = []; - const mock = nock('https://dsn.ingest.sentry.io') - .persist() - .post('/api/1337/envelope/', body => { - const envelope = parseEnvelope(body); - - if (envelopeType.includes(envelope[1].type as EnvelopeItemType)) { - envelopes.push(envelope); - } else { - return false; - } - - if (count === envelopes.length) { - nock.removeInterceptor(mock); - - if (endServer) { - // Cleaning nock only before the server is closed, - // not to break tests that use simultaneous requests to the server. - // Ex: Remix scope bleed tests. - nock.cleanAll(); - - server.close(() => { - resolve(envelopes); - }); - } - - resolve(envelopes); - } - - return true; - }); - - mock - .query(true) // accept any query params - used for sentry_key param - .reply(200); - }); -}; +export async function runScenario(url: string): Promise { + await axios.get(url); + await Sentry.flush(); +} -const makeRequest = async (method: 'get' | 'post', url: string): Promise => { +async function makeRequest(method: 'get' | 'post' = 'get', url: string): Promise { try { if (method === 'get') { await axios.get(url); @@ -171,84 +114,136 @@ const makeRequest = async (method: 'get' | 'post', url: string): Promise = // So, we do nothing. logger.warn(e); } -}; +} -/** - * Sends a get request to given URL, with optional headers. Returns the response. - * Ends the server instance and flushes the Sentry event queue. - * - * @param {TestServerConfig} config The url and the server instance. - * @param {Record} [headers] - * @return {*} {Promise} - */ -export const getAPIResponse = async (config: TestServerConfig, headers?: Record): Promise => { - const { data } = await axios.get(config.url, { headers: headers || {} }); +export class TestEnv { + public constructor(public readonly server: http.Server, public readonly url: string) { + this.server = server; + this.url = url; + } - await Sentry.flush(); - config.server.close(); + /** + * Starts a test server and returns the TestEnv instance + * + * @param {string} testDir + * @param {string} [serverPath] + * @param {string} [scenarioPath] + * @return {*} {Promise} + */ + public static async init(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'); + + const server = await new Promise(resolve => { + // eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-member-access + const app = require(serverPath || defaultServerPath).default as Express; + + app.get('/test', (_req, res) => { + try { + require(scenarioPath || `${testDir}/scenario`); + } finally { + res.status(200).end(); + } + }); - return data; -}; + const server = app.listen(port, () => { + resolve(server); + }); + }); -/** - * Intercepts and extracts a single request containing a Sentry envelope - * - * @param {TestServerConfig} config The url and the server instance. - * @param {DataCollectorOptions} options - * @returns The extracted envelope. - */ -export const getEnvelopeRequest = async ( - config: TestServerConfig, - options?: DataCollectorOptions, -): Promise>> => { - return (await getMultipleEnvelopeRequest(config, { ...options, count: 1 }))[0]; -}; + return new TestEnv(server, url); + } -/** - * Runs a test server - * - * @param {string} testDir - * @param {string} [serverPath] - * @param {string} [scenarioPath] - * @return {*} {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'); - - const server = await new Promise(resolve => { - // eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-member-access - const app = require(serverPath || defaultServerPath).default as Express; - - app.get('/test', (_req, res) => { - try { - require(scenarioPath || `${testDir}/scenario`); - } finally { - res.status(200).end(); - } - }); + /** + * Intercepts and extracts up to a number of requests containing Sentry envelopes. + * + * @param {DataCollectorOptions} options + * @returns The intercepted envelopes. + */ + public async getMultipleEnvelopeRequest(options: DataCollectorOptions): Promise[][]> { + const envelopeTypeArray = + typeof options.envelopeType === 'string' + ? [options.envelopeType] + : options.envelopeType || (['event'] as EnvelopeItemType[]); + + const resProm = this.setupNock( + options.count || 1, + typeof options.endServer === 'undefined' ? true : options.endServer, + envelopeTypeArray, + ); + + void makeRequest(options.method, options.url || this.url); + return resProm; + } - const server = app.listen(port, () => { - resolve(server); - }); - }); + /** + * Intercepts and extracts a single request containing a Sentry envelope + * + * @param {DataCollectorOptions} options + * @returns The extracted envelope. + */ + public async getEnvelopeRequest(options?: DataCollectorOptions): Promise>> { + return (await this.getMultipleEnvelopeRequest({ ...options, count: 1 }))[0]; + } - return { url, server }; -} + /** + * Sends a get request to given URL, with optional headers. Returns the response. + * Ends the server instance and flushes the Sentry event queue. + * + * @param {Record} [headers] + * @return {*} {Promise} + */ + public async getAPIResponse(url?: string, headers?: Record): Promise { + const { data } = await axios.get(url || this.url, { headers: headers || {} }); -/** - * Sends a get request to given URL. - * Flushes the Sentry event queue. - * - * @param {string} url - * @return {*} {Promise} - */ -export async function runScenario(url: string): Promise { - await axios.get(url); - await Sentry.flush(); + await Sentry.flush(); + this.server.close(); + + return data; + } + + public async setupNock( + count: number, + endServer: boolean, + envelopeType: EnvelopeItemType[], + ): Promise[][]> { + return new Promise(resolve => { + const envelopes: Record[][] = []; + const mock = nock('https://dsn.ingest.sentry.io') + .persist() + .post('/api/1337/envelope/', body => { + const envelope = parseEnvelope(body); + + if (envelopeType.includes(envelope[1].type as EnvelopeItemType)) { + envelopes.push(envelope); + } else { + return false; + } + + if (count === envelopes.length) { + nock.removeInterceptor(mock); + + if (endServer) { + // Cleaning nock only before the server is closed, + // not to break tests that use simultaneous requests to the server. + // Ex: Remix scope bleed tests. + nock.cleanAll(); + + this.server.close(() => { + resolve(envelopes); + }); + } + + resolve(envelopes); + } + + return true; + }); + + mock + .query(true) // accept any query params - used for sentry_key param + .reply(200); + }); + } } diff --git a/packages/remix/test/integration/test/server/action.test.ts b/packages/remix/test/integration/test/server/action.test.ts index c32202d77d6b..b5a0dc082c9a 100644 --- a/packages/remix/test/integration/test/server/action.test.ts +++ b/packages/remix/test/integration/test/server/action.test.ts @@ -1,19 +1,13 @@ -import { - assertSentryTransaction, - getEnvelopeRequest, - runServer, - getMultipleEnvelopeRequest, - assertSentryEvent, -} from './utils/helpers'; +import { assertSentryTransaction, assertSentryEvent, RemixTestEnv } from './utils/helpers'; 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 config = await runServer(adapter); - const url = `${config.url}/action-json-response/123123`; - const envelope = await getEnvelopeRequest({ ...config, url }, { method: 'post', envelopeType: 'transaction' }); + const env = await RemixTestEnv.init(adapter); + const url = `${env.url}/action-json-response/123123`; + const envelope = await env.getEnvelopeRequest({ url, method: 'post', envelopeType: 'transaction' }); const transaction = envelope[2]; assertSentryTransaction(transaction, { @@ -40,13 +34,15 @@ describe.each(['builtin', 'express'])('Remix API Actions with adapter = %s', ada }); it('reports an error thrown from the action', async () => { - const config = await runServer(adapter); - const url = `${config.url}/action-json-response/-1`; + const env = await RemixTestEnv.init(adapter); + const url = `${env.url}/action-json-response/-1`; - const [transaction, event] = await getMultipleEnvelopeRequest( - { ...config, url }, - { count: 2, method: 'post', envelopeType: ['transaction', 'event'] }, - ); + const [transaction, event] = await env.getMultipleEnvelopeRequest({ + url, + count: 2, + method: 'post', + envelopeType: ['transaction', 'event'], + }); assertSentryTransaction(transaction[2], { contexts: { @@ -80,13 +76,15 @@ describe.each(['builtin', 'express'])('Remix API Actions with adapter = %s', ada }); it('handles a thrown 500 response', async () => { - const config = await runServer(adapter); - const url = `${config.url}/action-json-response/-2`; + const env = await RemixTestEnv.init(adapter); + const url = `${env.url}/action-json-response/-2`; - const [transaction_1, event, transaction_2] = await getMultipleEnvelopeRequest( - { ...config, url }, - { count: 3, method: 'post', envelopeType: ['transaction', 'event'] }, - ); + const [transaction_1, event, transaction_2] = await env.getMultipleEnvelopeRequest({ + url, + count: 3, + method: 'post', + envelopeType: ['transaction', 'event'], + }); 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 98fec6592222..8d8b0b93ce00 100644 --- a/packages/remix/test/integration/test/server/loader.test.ts +++ b/packages/remix/test/integration/test/server/loader.test.ts @@ -1,10 +1,4 @@ -import { - assertSentryTransaction, - getEnvelopeRequest, - runServer, - getMultipleEnvelopeRequest, - assertSentryEvent, -} from './utils/helpers'; +import { assertSentryTransaction, RemixTestEnv, assertSentryEvent } from './utils/helpers'; import { Event } from '@sentry/types'; jest.spyOn(console, 'error').mockImplementation(); @@ -12,13 +6,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 config = await runServer(adapter); - const url = `${config.url}/loader-json-response/-2`; + const env = await RemixTestEnv.init(adapter); + const url = `${env.url}/loader-json-response/-2`; - const envelopes = await getMultipleEnvelopeRequest( - { ...config, url }, - { count: 2, envelopeType: ['transaction', 'event'] }, - ); + const envelopes = await env.getMultipleEnvelopeRequest({ url, count: 2, envelopeType: ['transaction', 'event'] }); const event = envelopes[0][2].type === 'transaction' ? envelopes[1][2] : envelopes[0][2]; const transaction = envelopes[0][2].type === 'transaction' ? envelopes[0][2] : envelopes[1][2]; @@ -55,9 +46,9 @@ describe.each(['builtin', 'express'])('Remix API Loaders with adapter = %s', ada }); it('correctly instruments a parameterized Remix API loader', async () => { - const config = await runServer(adapter); - const url = `${config.url}/loader-json-response/123123`; - const envelope = await getEnvelopeRequest({ ...config, url }, { envelopeType: 'transaction' }); + const env = await RemixTestEnv.init(adapter); + const url = `${env.url}/loader-json-response/123123`; + const envelope = await env.getEnvelopeRequest({ url, envelopeType: 'transaction' }); const transaction = envelope[2]; assertSentryTransaction(transaction, { @@ -83,13 +74,14 @@ describe.each(['builtin', 'express'])('Remix API Loaders with adapter = %s', ada }); it('handles a thrown 500 response', async () => { - const config = await runServer(adapter); - const url = `${config.url}/loader-json-response/-1`; + const env = await RemixTestEnv.init(adapter); + const url = `${env.url}/loader-json-response/-1`; - const [transaction_1, event, transaction_2] = await getMultipleEnvelopeRequest( - { ...config, url }, - { count: 3, envelopeType: ['transaction', 'event'] }, - ); + const [transaction_1, event, transaction_2] = await env.getMultipleEnvelopeRequest({ + url, + count: 3, + envelopeType: ['transaction', 'event'], + }); assertSentryTransaction(transaction_1[2], { contexts: { @@ -144,16 +136,16 @@ describe.each(['builtin', 'express'])('Remix API Loaders with adapter = %s', ada }); it('makes sure scope does not bleed between requests', async () => { - const { url, server } = await runServer(adapter); + const env = await RemixTestEnv.init(adapter); const envelopes = await Promise.all([ - getEnvelopeRequest({ url: `${url}/scope-bleed/1`, server }, { endServer: false, envelopeType: 'transaction' }), - getEnvelopeRequest({ url: `${url}/scope-bleed/2`, server }, { endServer: false, envelopeType: 'transaction' }), - getEnvelopeRequest({ url: `${url}/scope-bleed/3`, server }, { endServer: false, envelopeType: 'transaction' }), - getEnvelopeRequest({ url: `${url}/scope-bleed/4`, server }, { endServer: false, envelopeType: 'transaction' }), + env.getEnvelopeRequest({ url: `${env.url}/scope-bleed/1`, endServer: false, envelopeType: 'transaction' }), + env.getEnvelopeRequest({ url: `${env.url}/scope-bleed/2`, endServer: false, envelopeType: 'transaction' }), + env.getEnvelopeRequest({ url: `${env.url}/scope-bleed/3`, endServer: false, envelopeType: 'transaction' }), + env.getEnvelopeRequest({ url: `${env.url}/scope-bleed/4`, endServer: false, envelopeType: 'transaction' }), ]); - await new Promise(resolve => server.close(resolve)); + await new Promise(resolve => env.server.close(resolve)); envelopes.forEach(envelope => { const tags = envelope[2].tags as NonNullable; diff --git a/packages/remix/test/integration/test/server/utils/helpers.ts b/packages/remix/test/integration/test/server/utils/helpers.ts index 0273a5de43a2..a2cfedca0f94 100644 --- a/packages/remix/test/integration/test/server/utils/helpers.ts +++ b/packages/remix/test/integration/test/server/utils/helpers.ts @@ -2,33 +2,32 @@ 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 { TestEnv } from '../../../../../../node-integration-tests/utils'; import * as http from 'http'; export * from '../../../../../../node-integration-tests/utils'; -/** - * Runs a test server - * @returns URL - */ -export async function runServer(adapter: string = 'builtin'): Promise { - const requestHandlerFactory = - adapter === 'express' ? wrapExpressCreateRequestHandler(createRequestHandler) : createRequestHandler; +export class RemixTestEnv extends TestEnv { + private constructor(public readonly server: http.Server, public readonly url: string) { + super(server, url); + } - const port = await getPortPromise(); + public static async init(adapter: string = 'builtin'): Promise { + const requestHandlerFactory = + adapter === 'express' ? wrapExpressCreateRequestHandler(createRequestHandler) : createRequestHandler; - const server = await new Promise(resolve => { - const app = express(); + const port = await getPortPromise(); - app.all('*', requestHandlerFactory({ build: require('../../../build') })); + const server = await new Promise(resolve => { + const app = express(); - const server = app.listen(port, () => { - resolve(server); + app.all('*', requestHandlerFactory({ build: require('../../../build') })); + + const server = app.listen(port, () => { + resolve(server); + }); }); - }); - return { - url: `http://localhost:${port}`, - server, - }; + return new RemixTestEnv(server, `http://localhost:${port}`); + } } From 94f7c3193ef5b57d7baa407c9436ff0ed25dc8d4 Mon Sep 17 00:00:00 2001 From: Onur Temizkan Date: Fri, 26 Aug 2022 12:52:20 +0100 Subject: [PATCH 2/3] Update README. --- packages/node-integration-tests/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/node-integration-tests/README.md b/packages/node-integration-tests/README.md index e439f5556e13..e4354ad4fdee 100644 --- a/packages/node-integration-tests/README.md +++ b/packages/node-integration-tests/README.md @@ -33,9 +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` and [`http.Server`](https://nodejs.org/dist/latest-v16.x/docs/api/http.html#class-httpserver) instance 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. +`TestEnv` class contains methods to create and execute requests on a test server instance. `TestEnv.init()` which starts a test server and returns a `TestEnv` instance must be called by each test. The test server is automatically shut down after each test, if a data collection helper method such as `getEnvelopeRequest` and `getAPIResponse` is used. Tests that do not use those helper methods will need to end the server manually. -The responsibility of ending the server is delegated to the test case. Data collection helpers such as `getEnvelopeRequest` and `getAPIResponse` expect the server instance to be available and finish it before their resolution. Tests that do not use those helpers will need to end the server manually. +`TestEnv` instance has two public properties: `url` and `server`. The `url` property is the base URL for the server. The `http.Server` instance is used to finish the server eventually. Nock interceptors are internally used to capture envelope requests by `getEnvelopeRequest` and `getMultipleEnvelopeRequest` helpers. After capturing required requests, the interceptors are removed. Nock can manually be used inside the test cases to intercept requests but should be removed before the test ends, as not to cause flakiness. From f9a6e0b57205f679c17ad4215685cbafa20a0161 Mon Sep 17 00:00:00 2001 From: Onur Temizkan Date: Fri, 26 Aug 2022 12:53:23 +0100 Subject: [PATCH 3/3] Fix ESLint. --- .../suites/tracing/tracePropagationTargets/test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/node-integration-tests/suites/tracing/tracePropagationTargets/test.ts b/packages/node-integration-tests/suites/tracing/tracePropagationTargets/test.ts index 01b75ab10330..1209c59da46a 100644 --- a/packages/node-integration-tests/suites/tracing/tracePropagationTargets/test.ts +++ b/packages/node-integration-tests/suites/tracing/tracePropagationTargets/test.ts @@ -1,6 +1,6 @@ import nock from 'nock'; -import { TestEnv, runScenario } from '../../../utils'; +import { runScenario, TestEnv } from '../../../utils'; test('HttpIntegration should instrument correct requests when tracePropagationTargets option is provided', async () => { const match1 = nock('http://match-this-url.com')