Skip to content

test(node): Add integration tests for new node transports #4816

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 1 commit into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as Sentry from '@sentry/node';

Sentry.init({
dsn: 'https://[email protected]/1337',
release: '1.0',
_experiments: {
newTransport: true, // use new transport
},
});

Sentry.captureException(new Error('test_simple_error'));
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { getMultipleEnvelopeRequest, runServer } from '../../../../utils';

test('should correctly send envelope', async () => {
const url = await runServer(__dirname);
const envelopes = await getMultipleEnvelopeRequest(url, 2);

const errorEnvelope = envelopes[1];

expect(errorEnvelope).toHaveLength(3);
expect(errorEnvelope[2]).toMatchObject({
exception: {
values: [
{
type: 'Error',
value: 'test_simple_error',
},
],
},
release: '1.0',
event_id: expect.any(String),
timestamp: expect.any(Number),
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import '@sentry/tracing';

import * as Sentry from '@sentry/node';

Sentry.init({
dsn: 'https://[email protected]/1337',
release: '1.0',
tracesSampleRate: 1.0,
_experiments: {
newTransport: true, // use new transport
},
});

const transaction = Sentry.startTransaction({ name: 'test_transaction_1' });

transaction.finish();
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { assertSentryTransaction, getEnvelopeRequest, runServer } from '../../../../utils';

test('should send a manually started transaction when @sentry/tracing is imported using unnamed import', async () => {
const url = await runServer(__dirname);
const envelope = await getEnvelopeRequest(url);

expect(envelope).toHaveLength(3);

assertSentryTransaction(envelope[2], {
transaction: 'test_transaction_1',
});
});
32 changes: 26 additions & 6 deletions packages/node-integration-tests/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { parseSemver } from '@sentry/utils';
import { Express } from 'express';
import * as http from 'http';
Expand Down Expand Up @@ -101,25 +100,46 @@ export const getEventRequest = async (url: string): Promise<Record<string, unkno
};

/**
* Intercepts and extracts a request containing a Sentry Envelope
* Intercepts and extracts up to a number of requests containing Sentry envelopes.
*
* @param {string} url
* @return {*} {Promise<Array<Record<string, unknown>>>}
* @param url The url the intercepted requests will be directed to.
* @param count The expected amount of requests to the envelope endpoint. If
* the amount of sentrequests is lower than`count`, this function will not resolve.
* @returns The intercepted envelopes.
*/
export const getEnvelopeRequest = async (url: string): Promise<Array<Record<string, unknown>>> => {
export const getMultipleEnvelopeRequest = async (url: string, count: number): Promise<Record<string, unknown>[][]> => {
const envelopes: Record<string, unknown>[][] = [];

return new Promise(resolve => {
nock('https://dsn.ingest.sentry.io')
.post('/api/1337/envelope/', body => {
const envelope = parseEnvelope(body);
resolve(envelope);
envelopes.push(envelope);

if (count === envelopes.length) {
resolve(envelopes);
}

return true;
})
.times(count)
.query(true) // accept any query params - used for sentry_key param
.reply(200);

http.get(url);
});
};

/**
* Intercepts and extracts a single request containing a Sentry envelope
*
* @param url The url the intercepted request will be directed to.
* @returns The extracted envelope.
*/
export const getEnvelopeRequest = async (url: string): Promise<Array<Record<string, unknown>>> => {
return (await getMultipleEnvelopeRequest(url, 1))[0];
};

/**
* Runs a test server
*
Expand Down