Skip to content

Commit 52dafca

Browse files
authored
test(node): Add integration tests for new node transports (#4816)
1 parent b2836b8 commit 52dafca

File tree

5 files changed

+89
-6
lines changed

5 files changed

+89
-6
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as Sentry from '@sentry/node';
2+
3+
Sentry.init({
4+
dsn: 'https://[email protected]/1337',
5+
release: '1.0',
6+
_experiments: {
7+
newTransport: true, // use new transport
8+
},
9+
});
10+
11+
Sentry.captureException(new Error('test_simple_error'));
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { getMultipleEnvelopeRequest, runServer } from '../../../../utils';
2+
3+
test('should correctly send envelope', async () => {
4+
const url = await runServer(__dirname);
5+
const envelopes = await getMultipleEnvelopeRequest(url, 2);
6+
7+
const errorEnvelope = envelopes[1];
8+
9+
expect(errorEnvelope).toHaveLength(3);
10+
expect(errorEnvelope[2]).toMatchObject({
11+
exception: {
12+
values: [
13+
{
14+
type: 'Error',
15+
value: 'test_simple_error',
16+
},
17+
],
18+
},
19+
release: '1.0',
20+
event_id: expect.any(String),
21+
timestamp: expect.any(Number),
22+
});
23+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2+
import '@sentry/tracing';
3+
4+
import * as Sentry from '@sentry/node';
5+
6+
Sentry.init({
7+
dsn: 'https://[email protected]/1337',
8+
release: '1.0',
9+
tracesSampleRate: 1.0,
10+
_experiments: {
11+
newTransport: true, // use new transport
12+
},
13+
});
14+
15+
const transaction = Sentry.startTransaction({ name: 'test_transaction_1' });
16+
17+
transaction.finish();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { assertSentryTransaction, getEnvelopeRequest, runServer } from '../../../../utils';
2+
3+
test('should send a manually started transaction when @sentry/tracing is imported using unnamed import', async () => {
4+
const url = await runServer(__dirname);
5+
const envelope = await getEnvelopeRequest(url);
6+
7+
expect(envelope).toHaveLength(3);
8+
9+
assertSentryTransaction(envelope[2], {
10+
transaction: 'test_transaction_1',
11+
});
12+
});

packages/node-integration-tests/utils/index.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
21
import { parseSemver } from '@sentry/utils';
32
import { Express } from 'express';
43
import * as http from 'http';
@@ -101,25 +100,46 @@ export const getEventRequest = async (url: string): Promise<Record<string, unkno
101100
};
102101

103102
/**
104-
* Intercepts and extracts a request containing a Sentry Envelope
103+
* Intercepts and extracts up to a number of requests containing Sentry envelopes.
105104
*
106-
* @param {string} url
107-
* @return {*} {Promise<Array<Record<string, unknown>>>}
105+
* @param url The url the intercepted requests will be directed to.
106+
* @param count The expected amount of requests to the envelope endpoint. If
107+
* the amount of sentrequests is lower than`count`, this function will not resolve.
108+
* @returns The intercepted envelopes.
108109
*/
109-
export const getEnvelopeRequest = async (url: string): Promise<Array<Record<string, unknown>>> => {
110+
export const getMultipleEnvelopeRequest = async (url: string, count: number): Promise<Record<string, unknown>[][]> => {
111+
const envelopes: Record<string, unknown>[][] = [];
112+
110113
return new Promise(resolve => {
111114
nock('https://dsn.ingest.sentry.io')
112115
.post('/api/1337/envelope/', body => {
113116
const envelope = parseEnvelope(body);
114-
resolve(envelope);
117+
envelopes.push(envelope);
118+
119+
if (count === envelopes.length) {
120+
resolve(envelopes);
121+
}
122+
115123
return true;
116124
})
125+
.times(count)
126+
.query(true) // accept any query params - used for sentry_key param
117127
.reply(200);
118128

119129
http.get(url);
120130
});
121131
};
122132

133+
/**
134+
* Intercepts and extracts a single request containing a Sentry envelope
135+
*
136+
* @param url The url the intercepted request will be directed to.
137+
* @returns The extracted envelope.
138+
*/
139+
export const getEnvelopeRequest = async (url: string): Promise<Array<Record<string, unknown>>> => {
140+
return (await getMultipleEnvelopeRequest(url, 1))[0];
141+
};
142+
123143
/**
124144
* Runs a test server
125145
*

0 commit comments

Comments
 (0)