Skip to content

Commit 42d045c

Browse files
authored
test(node): Add --import test to integration tests (#14471)
We do test `--import=instrument.mjs` in our e2e tests but there was nothing in the Node integration tests so I added this.
1 parent bb55c25 commit 42d045c

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { loggingTransport } from '@sentry-internal/node-integration-tests';
2+
import * as Sentry from '@sentry/node';
3+
4+
Sentry.init({
5+
dsn: 'https://[email protected]/1337',
6+
release: '1.0',
7+
tracesSampleRate: 1.0,
8+
tracePropagationTargets: [/\/v0/, 'v1'],
9+
integrations: [],
10+
transport: loggingTransport,
11+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as http from 'http';
2+
import * as Sentry from '@sentry/node';
3+
4+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
5+
Sentry.startSpan({ name: 'test_span' }, async () => {
6+
await makeHttpRequest(`${process.env.SERVER_URL}/api/v0`);
7+
await makeHttpRequest(`${process.env.SERVER_URL}/api/v1`);
8+
await makeHttpRequest(`${process.env.SERVER_URL}/api/v2`);
9+
await makeHttpRequest(`${process.env.SERVER_URL}/api/v3`);
10+
});
11+
12+
function makeHttpRequest(url) {
13+
return new Promise(resolve => {
14+
http
15+
.request(url, httpRes => {
16+
httpRes.on('data', () => {
17+
// we don't care about data
18+
});
19+
httpRes.on('end', () => {
20+
resolve();
21+
});
22+
})
23+
.end();
24+
});
25+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { join } from 'path';
2+
import { conditionalTest } from '../../../../utils';
3+
import { createRunner } from '../../../../utils/runner';
4+
import { createTestServer } from '../../../../utils/server';
5+
6+
conditionalTest({ min: 18 })('outgoing sampled http requests are correctly instrumented in ESM', () => {
7+
test('outgoing sampled http requests are correctly instrumented in ESM', done => {
8+
expect.assertions(11);
9+
10+
createTestServer(done)
11+
.get('/api/v0', headers => {
12+
expect(headers['baggage']).toEqual(expect.any(String));
13+
expect(headers['sentry-trace']).toEqual(expect.stringMatching(/^([a-f0-9]{32})-([a-f0-9]{16})-1$/));
14+
expect(headers['sentry-trace']).not.toEqual('00000000000000000000000000000000-0000000000000000-1');
15+
})
16+
.get('/api/v1', headers => {
17+
expect(headers['baggage']).toEqual(expect.any(String));
18+
expect(headers['sentry-trace']).toEqual(expect.stringMatching(/^([a-f0-9]{32})-([a-f0-9]{16})-1$/));
19+
expect(headers['sentry-trace']).not.toEqual('00000000000000000000000000000000-0000000000000000-1');
20+
})
21+
.get('/api/v2', headers => {
22+
expect(headers['baggage']).toBeUndefined();
23+
expect(headers['sentry-trace']).toBeUndefined();
24+
})
25+
.get('/api/v3', headers => {
26+
expect(headers['baggage']).toBeUndefined();
27+
expect(headers['sentry-trace']).toBeUndefined();
28+
})
29+
.start()
30+
.then(([SERVER_URL, closeTestServer]) => {
31+
const instrumentPath = join(__dirname, 'instrument.mjs');
32+
createRunner(__dirname, 'scenario.mjs')
33+
.withFlags('--import', instrumentPath)
34+
.withEnv({ SERVER_URL })
35+
.expect({
36+
transaction: {
37+
// we're not too concerned with the actual transaction here since this is tested elsewhere
38+
},
39+
})
40+
.start(closeTestServer);
41+
});
42+
});
43+
});

dev-packages/node-integration-tests/utils/runner.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,8 @@ export function createRunner(...paths: string[]) {
338338
const output = data.toString();
339339
logs.push(output.trim());
340340

341+
if (process.env.DEBUG) log('stderr line', output);
342+
341343
if (ensureNoErrorOutput) {
342344
complete(new Error(`Expected no error output but got: '${output}'`));
343345
}

0 commit comments

Comments
 (0)