Skip to content

Commit a5149ea

Browse files
committed
test: added proof test for cjs with esm loader
1 parent d0fe277 commit a5149ea

File tree

3 files changed

+109
-1
lines changed

3 files changed

+109
-1
lines changed

packages/collector/src/announceCycle/agentready.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ function enter(_ctx) {
147147
}
148148

149149
logger.info('The Instana Node.js collector is now fully initialized and connected to the Instana host agent.');
150-
151150
// CASE: This is an IPC message only for a parent process.
152151
// TODO: Add an EventEmitter functionality for the current process
153152
// such as `instana.on('instana.collector.initialized')`.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* (c) Copyright IBM Corp. 2025
3+
*/
4+
5+
'use strict';
6+
7+
// NOTE: c8 bug https://github.com/bcoe/c8/issues/166
8+
process.on('SIGTERM', () => {
9+
process.disconnect();
10+
process.exit(0);
11+
});
12+
13+
const logPrefix = `CJS via ESM (${process.pid}):\t`;
14+
const port = require('../../../test_util/app-port')();
15+
const express = require('express');
16+
const bodyParser = require('body-parser');
17+
const pinoLogger = require('pino')();
18+
19+
const app = express();
20+
app.use(bodyParser.json());
21+
22+
app.get('/', (req, res) => {
23+
res.sendStatus(200);
24+
});
25+
26+
app.get('/trigger', async (req, res) => {
27+
log('Received request');
28+
await fetch(`http://localhost:${port}`);
29+
pinoLogger.error('wtf');
30+
res.json({ success: true });
31+
});
32+
33+
app.listen(port, () => {
34+
log(`Listening on port: ${port}`);
35+
});
36+
37+
function log() {
38+
/* eslint-disable no-console */
39+
const args = Array.prototype.slice.call(arguments);
40+
args[0] = logPrefix + args[0];
41+
console.log.apply(console, args);
42+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* (c) Copyright IBM Corp. 2025
3+
*/
4+
5+
'use strict';
6+
7+
const semver = require('semver');
8+
const path = require('path');
9+
const expect = require('chai').expect;
10+
const supportedVersion = require('@instana/core').tracing.supportedVersion;
11+
const ProcessControls = require('../../../test_util/ProcessControls');
12+
const globalAgent = require('../../../globalAgent');
13+
const { retry } = require('../../../../../core/test/test_util');
14+
15+
const mochaSuiteFn =
16+
supportedVersion(process.versions.node) && semver.satisfies(process.versions.node, '18') ? describe : describe.skip;
17+
18+
mochaSuiteFn('tracing/cjs-via-esm', function () {
19+
this.timeout(1000 * 60);
20+
globalAgent.setUpCleanUpHooks();
21+
22+
let controls;
23+
const agentControls = globalAgent.instance;
24+
25+
before(async () => {
26+
const rootFolder = path.join(__dirname, '..', '..', '..', '..');
27+
28+
controls = new ProcessControls({
29+
useGlobalAgent: true,
30+
dirname: __dirname,
31+
enableOtelIntegration: true,
32+
execArgv: ['--experimental-loader', `${rootFolder}/esm-loader.mjs`]
33+
});
34+
35+
await controls.startAndWaitForAgentConnection();
36+
});
37+
38+
beforeEach(async () => {
39+
await agentControls.clearReceivedTraceData();
40+
});
41+
42+
after(async () => {
43+
await controls.stop();
44+
});
45+
46+
it('must trace', async () => {
47+
await controls.sendRequest({
48+
path: '/trigger'
49+
});
50+
51+
return retry(async () => {
52+
const spans = await agentControls.getSpans();
53+
54+
expect(spans.length).to.equal(4);
55+
56+
const pinoSpan = spans.find(span => span.n === 'log.pino');
57+
expect(pinoSpan).to.exist;
58+
59+
const httpServerSpan = spans.find(span => span.n === 'node.http.server');
60+
expect(httpServerSpan).to.exist;
61+
62+
const httpClientSpan = spans.find(span => span.n === 'node.http.client');
63+
expect(httpClientSpan).to.exist;
64+
expect(httpClientSpan.data).to.exist;
65+
});
66+
});
67+
});

0 commit comments

Comments
 (0)