Skip to content

Commit

Permalink
test(node): Add integration tests for new node transports (#4816)
Browse files Browse the repository at this point in the history
  • Loading branch information
lforst authored Mar 29, 2022
1 parent b2836b8 commit 52dafca
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 6 deletions.
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

0 comments on commit 52dafca

Please sign in to comment.