Skip to content

ref(tests): Refactor Node integration tests further. #5596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/node-integration-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ 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.
`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.

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. Test that do not use those helpers will need to end the server manually.

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, not to cause flakiness.

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:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { assertSentryEvent, getEnvelopeRequest, runServer } from '../../../utils/index';

test('should capture and send Express controller error.', async () => {
const { url, server, scope } = await runServer(__dirname, `${__dirname}/server.ts`);
const event = await getEnvelopeRequest({ url: `${url}/express`, server, scope });
const { url, server } = await runServer(__dirname, `${__dirname}/server.ts`);
const event = await getEnvelopeRequest({ url: `${url}/express`, server });

expect((event[2] as any).exception.values[0].stacktrace.frames.length).toBeGreaterThan(0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ 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, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`);
const { url, server } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`);

const response = (await getAPIResponse(
{ url: `${url}/express`, server, scope },
{ url: `${url}/express`, server },
{
baggage: 'sentry-release=2.0.0,sentry-environment=myEnv',
},
Expand All @@ -23,10 +23,10 @@ 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, scope } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`);
const { url, server } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`);

const response = (await getAPIResponse(
{ url: `${url}/express`, server, scope },
{ url: `${url}/express`, server },
{
'sentry-trace': '',
baggage: 'sentry-release=2.0.0,sentry-environment=myEnv,dogs=great',
Expand All @@ -43,10 +43,10 @@ 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, scope } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`);
const { url, server } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`);

const response = (await getAPIResponse(
{ url: `${url}/express`, server, scope },
{ url: `${url}/express`, server },
{
'sentry-trace': '',
},
Expand All @@ -62,10 +62,10 @@ 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, scope } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`);
const { url, server } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`);

const response = (await getAPIResponse(
{ url: `${url}/express`, server, scope },
{ url: `${url}/express`, server },
{
'sentry-trace': '',
baggage: 'foo=bar',
Expand All @@ -82,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, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`);
const { url, server } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`);

const response = (await getAPIResponse({ url: `${url}/express`, server, scope }, {})) as TestAPIResponse;
const response = (await getAPIResponse({ url: `${url}/express`, server }, {})) as TestAPIResponse;

expect(response).toBeDefined();
expect(response).toMatchObject({
Expand All @@ -99,10 +99,10 @@ 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, scope } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`);
const { url, server } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`);

const response = (await getAPIResponse(
{ url: `${url}/express`, server, scope },
{ url: `${url}/express`, server },
{
baggage: 'foo=bar,bar=baz',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`);
const { url, server } = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`);

const response = (await getAPIResponse({ url: `${url}/express`, server, scope })) as TestAPIResponse;
const response = (await getAPIResponse({ url: `${url}/express`, server })) as TestAPIResponse;
const baggageString = response.test_data.baggage;

expect(response).toBeDefined();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`);
const { url, server } = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`);

const response = (await getAPIResponse({ url: `${url}/express`, server, scope })) as TestAPIResponse;
const response = (await getAPIResponse({ url: `${url}/express`, server })) as TestAPIResponse;

expect(response).toBeDefined();
expect(response).toMatchObject({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ 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, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`);
const { url, server } = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`);

const response = (await getAPIResponse(
{ url: `${url}/express`, server, scope },
{ url: `${url}/express`, server },
{
'sentry-trace': '',
baggage: 'sentry-release=2.1.0,sentry-environment=myEnv',
Expand All @@ -24,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, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`);
const { url, server } = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`);

const response = (await getAPIResponse({ url: `${url}/express`, server, scope }, {})) as TestAPIResponse;
const response = (await getAPIResponse({ url: `${url}/express`, server }, {})) as TestAPIResponse;

expect(response).toBeDefined();
expect(response).toMatchObject({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ 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, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`);
const { url, server } = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`);

const response = (await getAPIResponse(
{ url: `${url}/express`, server, scope },
{ url: `${url}/express`, server },
{
'sentry-trace': '',
baggage: 'sentry-release=2.0.0,sentry-environment=myEnv',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`);
const { url, server } = await runServer(__dirname, `${path.resolve(__dirname, '.')}/server.ts`);

const response = (await getAPIResponse({ url: `${url}/express`, server, scope })) as TestAPIResponse;
const response = (await getAPIResponse({ url: `${url}/express`, server })) as TestAPIResponse;

expect(response).toBeDefined();
expect(response).toMatchObject({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ 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, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`);
const { url, server } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`);

const response = (await getAPIResponse(
{ url: `${url}/express`, server, scope },
{ url: `${url}/express`, server },
{
'sentry-trace': '12312012123120121231201212312012-1121201211212012-0',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, server, scope } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`);
const { url, server } = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`);

const response = (await getAPIResponse({ url: `${url}/express`, server, scope })) as TestAPIResponse;
const response = (await getAPIResponse({ url: `${url}/express`, server })) as TestAPIResponse;

expect(response).toBeDefined();
expect(response).toMatchObject({
Expand Down
16 changes: 8 additions & 8 deletions packages/node-integration-tests/suites/express/tracing/test.ts
Original file line number Diff line number Diff line change
@@ -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, server, scope } = await runServer(__dirname, `${__dirname}/server.ts`);
const envelope = await getEnvelopeRequest({ url: `${url}/express`, server, scope });
const { url, server } = await runServer(__dirname, `${__dirname}/server.ts`);
const envelope = await getEnvelopeRequest({ url: `${url}/express`, server }, { envelopeType: 'transaction' });

expect(envelope).toHaveLength(3);

Expand All @@ -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, scope } = await runServer(__dirname, `${__dirname}/server.ts`);
const envelope = await getEnvelopeRequest({ url: `${url}/regex`, server, scope });
const { url, server } = await runServer(__dirname, `${__dirname}/server.ts`);
const envelope = await getEnvelopeRequest({ url: `${url}/regex`, server }, { envelopeType: 'transaction' });

expect(envelope).toHaveLength(3);

Expand All @@ -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, scope } = await runServer(__dirname, `${__dirname}/server.ts`);
const envelope = await getEnvelopeRequest({ url: `${url}/${segment}`, server, scope });
const { url, server } = await runServer(__dirname, `${__dirname}/server.ts`);
const envelope = await getEnvelopeRequest({ url: `${url}/${segment}`, server }, { envelopeType: 'transaction' });

expect(envelope).toHaveLength(3);

Expand Down Expand Up @@ -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, scope } = await runServer(__dirname, `${__dirname}/server.ts`);
const envelope = await getEnvelopeRequest({ url: `${url}/${segment}`, server, scope });
const { url, server } = await runServer(__dirname, `${__dirname}/server.ts`);
const envelope = await getEnvelopeRequest({ url: `${url}/${segment}`, server }, { envelopeType: 'transaction' });

expect(envelope).toHaveLength(3);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils';
import { assertSentryEvent, getEnvelopeRequest, runServer } from '../../../../utils';

test('should add an empty breadcrumb, when an empty object is given', async () => {
const config = await runServer(__dirname);
const envelopes = await getMultipleEnvelopeRequest(config, { count: 2 });
const errorEnvelope = envelopes[1];
const envelope = await getEnvelopeRequest(config);

expect(errorEnvelope).toHaveLength(3);
expect(envelope).toHaveLength(3);

assertSentryEvent(errorEnvelope[2], {
assertSentryEvent(envelope[2], {
message: 'test-empty-obj',
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../

test('should add multiple breadcrumbs', async () => {
const config = await runServer(__dirname);
const envelopes = await getMultipleEnvelopeRequest(config, { count: 2 });
const events = await getMultipleEnvelopeRequest(config, { count: 1 });

assertSentryEvent(envelopes[1][2], {
assertSentryEvent(events[0][2], {
message: 'test_multi_breadcrumbs',
breadcrumbs: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils';
import { assertSentryEvent, getEnvelopeRequest, runServer } from '../../../../utils';

test('should add a simple breadcrumb', async () => {
const config = await runServer(__dirname);
const envelopes = await getMultipleEnvelopeRequest(config, { count: 2 });
const event = await getEnvelopeRequest(config);

assertSentryEvent(envelopes[1][2], {
assertSentryEvent(event[2], {
message: 'test_simple',
breadcrumbs: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils';
import { assertSentryEvent, getEnvelopeRequest, runServer } from '../../../../utils';

test('should work inside catch block', async () => {
const config = await runServer(__dirname);
const envelopes = await getMultipleEnvelopeRequest(config, { count: 2 });
const errorEnvelope = envelopes[1];
const event = await getEnvelopeRequest(config);

assertSentryEvent(errorEnvelope[2], {
assertSentryEvent(event[2], {
exception: {
values: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils';
import { assertSentryEvent, getEnvelopeRequest, runServer } from '../../../../utils';

test('should capture an empty object', async () => {
const config = await runServer(__dirname);
const envelopes = await getMultipleEnvelopeRequest(config, { count: 2 });
const errorEnvelope = envelopes[1];
const event = await getEnvelopeRequest(config);

assertSentryEvent(errorEnvelope[2], {
assertSentryEvent(event[2], {
exception: {
values: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils';
import { assertSentryEvent, getEnvelopeRequest, runServer } from '../../../../utils';

test('should capture a simple error with message', async () => {
const config = await runServer(__dirname);
const envelopes = await getMultipleEnvelopeRequest(config, { count: 2 });
const errorEnvelope = envelopes[1];
const envelope = await getEnvelopeRequest(config);

assertSentryEvent(errorEnvelope[2], {
assertSentryEvent(envelope[2], {
exception: {
values: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../../../utils';
import { assertSentryEvent, getEnvelopeRequest, runServer } from '../../../../utils';

test('should capture a simple message string', async () => {
const config = await runServer(__dirname);
const envelopes = await getMultipleEnvelopeRequest(config, { count: 2 });
const errorEnvelope = envelopes[1];
const event = await getEnvelopeRequest(config);

assertSentryEvent(errorEnvelope[2], {
assertSentryEvent(event[2], {
message: 'Message',
level: 'info',
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@ import { assertSentryEvent, getMultipleEnvelopeRequest, runServer } from '../../

test('should capture with different severity levels', async () => {
const config = await runServer(__dirname);
const envelopes = await getMultipleEnvelopeRequest(config, { count: 12 });
const events = await getMultipleEnvelopeRequest(config, { count: 6 });

assertSentryEvent(envelopes[1][2], {
assertSentryEvent(events[0][2], {
message: 'debug_message',
level: 'debug',
});

assertSentryEvent(envelopes[3][2], {
assertSentryEvent(events[1][2], {
message: 'info_message',
level: 'info',
});

assertSentryEvent(envelopes[5][2], {
assertSentryEvent(events[2][2], {
message: 'warning_message',
level: 'warning',
});

assertSentryEvent(envelopes[7][2], {
assertSentryEvent(events[3][2], {
message: 'error_message',
level: 'error',
});

assertSentryEvent(envelopes[9][2], {
assertSentryEvent(events[4][2], {
message: 'fatal_message',
level: 'fatal',
});

assertSentryEvent(envelopes[11][2], {
assertSentryEvent(events[5][2], {
message: 'log_message',
level: 'log',
});
Expand Down
Loading