Skip to content

Commit 77d24eb

Browse files
committed
test(node): Add utility to test esm & cjs instrumentation
1 parent ae8e59e commit 77d24eb

File tree

37 files changed

+417
-299
lines changed

37 files changed

+417
-299
lines changed

dev-packages/node-integration-tests/.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ module.exports = {
1212
},
1313
},
1414
{
15-
files: ['suites/**/*.ts'],
15+
files: ['suites/**/*.ts', 'suites/**/*.mjs'],
1616
parserOptions: {
1717
project: ['tsconfig.test.json'],
1818
sourceType: 'module',
19+
ecmaVersion: 'latest',
1920
},
2021
rules: {
2122
'@typescript-eslint/typedef': 'off',
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
suites/**/tmp_*

dev-packages/node-integration-tests/suites/anr/app-path.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
import * as Sentry from '@sentry/node';
12
import * as assert from 'assert';
23
import * as crypto from 'crypto';
34
import * as path from 'path';
45
import * as url from 'url';
56

6-
import * as Sentry from '@sentry/node';
7-
87
global._sentryDebugIds = { [new Error().stack]: 'aaaaaaaa-aaaa-4aaa-aaaa-aaaaaaaaaa' };
98

109
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));

dev-packages/node-integration-tests/suites/anr/basic-multiple.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
import * as Sentry from '@sentry/node';
12
import * as assert from 'assert';
23
import * as crypto from 'crypto';
34

4-
import * as Sentry from '@sentry/node';
5-
65
global._sentryDebugIds = { [new Error().stack]: 'aaaaaaaa-aaaa-4aaa-aaaa-aaaaaaaaaa' };
76

87
setTimeout(() => {

dev-packages/node-integration-tests/suites/anr/basic.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
import * as Sentry from '@sentry/node';
12
import * as assert from 'assert';
23
import * as crypto from 'crypto';
34

4-
import * as Sentry from '@sentry/node';
5-
65
global._sentryDebugIds = { [new Error().stack]: 'aaaaaaaa-aaaa-4aaa-aaaa-aaaaaaaaaa' };
76

87
setTimeout(() => {

dev-packages/node-integration-tests/suites/anr/indefinite.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
import * as Sentry from '@sentry/node';
12
import * as assert from 'assert';
23
import * as crypto from 'crypto';
34

4-
import * as Sentry from '@sentry/node';
5-
65
setTimeout(() => {
76
process.exit();
87
}, 10000);

dev-packages/node-integration-tests/suites/anr/isolated.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
import * as Sentry from '@sentry/node';
12
import * as assert from 'assert';
23
import * as crypto from 'crypto';
34

4-
import * as Sentry from '@sentry/node';
5-
65
setTimeout(() => {
76
process.exit();
87
}, 10000);
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import * as Sentry from '@sentry/node';
2+
import { loggingTransport } from '@sentry-internal/node-integration-tests';
13
import { spawn } from 'child_process';
24
import { join } from 'path';
3-
import { loggingTransport } from '@sentry-internal/node-integration-tests';
4-
import * as Sentry from '@sentry/node';
55
import { Worker } from 'worker_threads';
66

77
const __dirname = new URL('.', import.meta.url).pathname;
@@ -13,16 +13,18 @@ Sentry.init({
1313
transport: loggingTransport,
1414
});
1515

16-
await new Promise(resolve => {
17-
const child = spawn('sleep', ['a']);
18-
child.on('error', resolve);
19-
child.on('exit', resolve);
20-
});
16+
(async () => {
17+
await new Promise(resolve => {
18+
const child = spawn('sleep', ['a']);
19+
child.on('error', resolve);
20+
child.on('exit', resolve);
21+
});
2122

22-
await new Promise(resolve => {
23-
const worker = new Worker(join(__dirname, 'worker.mjs'));
24-
worker.on('error', resolve);
25-
worker.on('exit', resolve);
26-
});
23+
await new Promise(resolve => {
24+
const worker = new Worker(join(__dirname, 'worker.mjs'));
25+
worker.on('error', resolve);
26+
worker.on('exit', resolve);
27+
});
2728

28-
throw new Error('This is a test error');
29+
throw new Error('This is a test error');
30+
})();

dev-packages/node-integration-tests/suites/child-process/fork.mjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import * as Sentry from '@sentry/node';
2+
import { loggingTransport } from '@sentry-internal/node-integration-tests';
13
import { fork } from 'child_process';
24
import * as path from 'path';
3-
import { loggingTransport } from '@sentry-internal/node-integration-tests';
4-
import * as Sentry from '@sentry/node';
55

66
const __dirname = new URL('.', import.meta.url).pathname;
77

@@ -12,6 +12,7 @@ Sentry.init({
1212
transport: loggingTransport,
1313
});
1414

15+
// eslint-disable-next-line no-unused-vars
1516
const _child = fork(path.join(__dirname, 'child.mjs'));
1617

1718
setTimeout(() => {

dev-packages/node-integration-tests/suites/child-process/worker.mjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import * as path from 'path';
2-
import { loggingTransport } from '@sentry-internal/node-integration-tests';
31
import * as Sentry from '@sentry/node';
2+
import { loggingTransport } from '@sentry-internal/node-integration-tests';
3+
import * as path from 'path';
44
import { Worker } from 'worker_threads';
55

66
const __dirname = new URL('.', import.meta.url).pathname;
@@ -12,6 +12,7 @@ Sentry.init({
1212
transport: loggingTransport,
1313
});
1414

15+
// eslint-disable-next-line no-unused-vars
1516
const _worker = new Worker(path.join(__dirname, 'child.mjs'));
1617

1718
setTimeout(() => {

dev-packages/node-integration-tests/suites/contextLines/filename-with-spaces/instrument.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { loggingTransport } from '@sentry-internal/node-integration-tests';
21
import * as Sentry from '@sentry/node';
2+
import { loggingTransport } from '@sentry-internal/node-integration-tests';
33

44
Sentry.init({
55
dsn: 'https://[email protected]/1337',

dev-packages/node-integration-tests/suites/contextLines/filename-with-spaces/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe('ContextLines integration in ESM', () => {
88
const instrumentPath = join(__dirname, 'instrument.mjs');
99

1010
await createRunner(__dirname, 'scenario with space.mjs')
11-
.withFlags('--import', instrumentPath)
11+
.withInstrument(instrumentPath)
1212
.expect({
1313
event: {
1414
exception: {

dev-packages/node-integration-tests/suites/esm/import-in-the-middle/app.mjs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { loggingTransport } from '@sentry-internal/node-integration-tests';
21
import * as Sentry from '@sentry/node';
2+
import { loggingTransport } from '@sentry-internal/node-integration-tests';
33
import * as iitm from 'import-in-the-middle';
44

55
new iitm.Hook((_, name) => {
@@ -14,6 +14,8 @@ Sentry.init({
1414
transport: loggingTransport,
1515
});
1616

17-
await import('./sub-module.mjs');
18-
await import('http');
19-
await import('os');
17+
(async () => {
18+
await import('./sub-module.mjs');
19+
await import('http');
20+
await import('os');
21+
})();

dev-packages/node-integration-tests/suites/esm/modules-integration/app.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { loggingTransport } from '@sentry-internal/node-integration-tests';
21
import * as Sentry from '@sentry/node';
2+
import { loggingTransport } from '@sentry-internal/node-integration-tests';
33

44
Sentry.init({
55
dsn: 'https://[email protected]/1337',

dev-packages/node-integration-tests/suites/esm/warn-esm/server.mjs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import { loggingTransport } from '@sentry-internal/node-integration-tests';
21
import * as Sentry from '@sentry/node';
2+
import { loggingTransport, startExpressServerAndSendPortToRunner } from '@sentry-internal/node-integration-tests';
3+
import express from 'express';
34

45
Sentry.init({
56
dsn: 'https://[email protected]/1337',
67
release: '1.0',
78
transport: loggingTransport,
89
});
910

10-
import { startExpressServerAndSendPortToRunner } from '@sentry-internal/node-integration-tests';
11-
import express from 'express';
12-
1311
const app = express();
1412

1513
app.get('/test/success', (req, res) => {

dev-packages/node-integration-tests/suites/public-api/LocalVariables/deny-inspector.mjs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { register } from 'node:module';
22

3+
// eslint-disable-next-line no-unused-vars
34
const hookScript = Buffer.from(`
45
56
`);
@@ -16,6 +17,8 @@ export async function resolve(specifier, context, nextResolve) {
1617
import.meta.url,
1718
);
1819

19-
const Sentry = await import('@sentry/node');
20+
(async () => {
21+
const Sentry = await import('@sentry/node');
2022

21-
Sentry.init({});
23+
Sentry.init({});
24+
})();

dev-packages/node-integration-tests/suites/public-api/LocalVariables/local-variables-caught.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-unused-vars */
2-
import { loggingTransport } from '@sentry-internal/node-integration-tests';
32
import * as Sentry from '@sentry/node';
3+
import { loggingTransport } from '@sentry-internal/node-integration-tests';
44

55
Sentry.init({
66
dsn: 'https://[email protected]/1337',

dev-packages/node-integration-tests/suites/tracing/amqplib/constants.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

dev-packages/node-integration-tests/suites/tracing/amqplib/scenario-message.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import * as Sentry from '@sentry/node';
2+
import amqp from 'amqplib';
3+
4+
const queueName = 'queue1';
5+
const amqpUsername = 'sentry';
6+
const amqpPassword = 'sentry';
7+
8+
const AMQP_URL = `amqp://${amqpUsername}:${amqpPassword}@localhost:5672/`;
9+
const ACKNOWLEDGEMENT = { noAck: false };
10+
11+
const QUEUE_OPTIONS = {
12+
durable: true, // Make the queue durable
13+
exclusive: false, // Not exclusive
14+
autoDelete: false, // Don't auto-delete the queue
15+
arguments: {
16+
'x-message-ttl': 30000, // Message TTL of 30 seconds
17+
'x-max-length': 1000, // Maximum queue length of 1000 messages
18+
},
19+
};
20+
21+
(async () => {
22+
const { connection, channel } = await connectToRabbitMQ();
23+
await createQueue(queueName, channel);
24+
25+
const consumeMessagePromise = consumeMessageFromQueue(queueName, channel);
26+
27+
await Sentry.startSpan({ name: 'root span' }, async () => {
28+
sendMessageToQueue(queueName, channel, JSON.stringify({ foo: 'bar01' }));
29+
});
30+
31+
await consumeMessagePromise;
32+
33+
await channel.close();
34+
await connection.close();
35+
36+
// Stop the process from exiting before the transaction is sent
37+
setInterval(() => {}, 1000);
38+
})();
39+
40+
async function connectToRabbitMQ() {
41+
const connection = await amqp.connect(AMQP_URL);
42+
const channel = await connection.createChannel();
43+
return { connection, channel };
44+
}
45+
46+
async function createQueue(queueName, channel) {
47+
await channel.assertQueue(queueName, QUEUE_OPTIONS);
48+
}
49+
50+
function sendMessageToQueue(queueName, channel, message) {
51+
channel.sendToQueue(queueName, Buffer.from(message));
52+
}
53+
54+
async function consumer(queueName, channel) {
55+
return new Promise((resolve, reject) => {
56+
channel
57+
.consume(
58+
queueName,
59+
message => {
60+
if (message) {
61+
channel.ack(message);
62+
resolve();
63+
} else {
64+
reject(new Error('No message received'));
65+
}
66+
},
67+
ACKNOWLEDGEMENT,
68+
)
69+
.catch(reject);
70+
});
71+
}
72+
73+
async function consumeMessageFromQueue(queueName, channel) {
74+
await consumer(queueName, channel);
75+
}

0 commit comments

Comments
 (0)