diff --git a/packages/collector/test/tracing/misc/node-fetch/app.js b/packages/collector/test/tracing/misc/node-fetch/app.js index 1931459054..d0234e4472 100644 --- a/packages/collector/test/tracing/misc/node-fetch/app.js +++ b/packages/collector/test/tracing/misc/node-fetch/app.js @@ -25,8 +25,10 @@ const app = express(); const logPrefix = `fetch App (${process.pid}):\t`; const agentPort = process.env.INSTANA_AGENT_PORT; +// From v3 onwards, node-fetch is a pure ESM module and does not support require(). +const fetchVersion = process.env.NODE_FETCH_VERSION || 'v2'; +const fetch = fetchVersion === 'latest' ? require('node-fetch') : require(`node-fetch-${fetchVersion}`); -const fetch = require('node-fetch-v2'); if (process.env.WITH_STDOUT) { app.use(morgan(`${logPrefix}:method :url :status`)); } diff --git a/packages/collector/test/tracing/misc/node-fetch/test.js b/packages/collector/test/tracing/misc/node-fetch/test.js index 41e9f8a6e2..5ef6e58c9b 100644 --- a/packages/collector/test/tracing/misc/node-fetch/test.js +++ b/packages/collector/test/tracing/misc/node-fetch/test.js @@ -14,68 +14,79 @@ const globalAgent = require('../../../globalAgent'); const mochaSuiteFn = supportedVersion(process.versions.node) ? describe : describe.skip; -mochaSuiteFn('tracing/node-fetch', function () { - this.timeout(config.getTestTimeout()); +['latest', 'v2'].forEach(version => { + mochaSuiteFn(`tracing/node-fetch @${version}`, function () { + // Skip testing the latest version in CommonJS mode, as it is ESM-only. + // From v3 onwards, node-fetch is a pure ESM module and does not support require(). + if (!process.env.RUN_ESM && version === 'latest') return; - globalAgent.setUpCleanUpHooks(); - const agentControls = globalAgent.instance; + // Curently we do not run ESM tests for all versions, so skip the ESM app for non-latest versions. + // TODO: Support for mocking `import` in ESM apps is planned under INSTA-788. + if (process.env.RUN_ESM && version !== 'latest') return; - let controls; + this.timeout(config.getTestTimeout()); - before(async () => { - controls = new ProcessControls({ - dirname: __dirname, - useGlobalAgent: true + globalAgent.setUpCleanUpHooks(); + const agentControls = globalAgent.instance; + + let controls; + + before(async () => { + controls = new ProcessControls({ + dirname: __dirname, + useGlobalAgent: true, + env: { NODE_FETCH_VERSION: version } + }); + + await controls.startAndWaitForAgentConnection(); }); - await controls.startAndWaitForAgentConnection(); - }); + beforeEach(async () => { + await agentControls.clearReceivedTraceData(); + }); - beforeEach(async () => { - await agentControls.clearReceivedTraceData(); - }); + after(async () => { + await controls.stop(); + }); - after(async () => { - await controls.stop(); - }); + afterEach(async () => { + await controls.clearIpcMessages(); + }); - afterEach(async () => { - await controls.clearIpcMessages(); - }); + it('GET request', () => + controls + .sendRequest({ + method: 'GET', + path: '/request' + }) + .then(() => + retry(() => + agentControls.getSpans().then(spans => { + expect(spans.length).to.equal(2); + + const httpEntry = verifyHttpRootEntry({ + spans, + apiPath: '/request', + pid: String(controls.getPid()) + }); - it('GET request', () => - controls - .sendRequest({ - method: 'GET', - path: '/request' - }) - .then(() => - retry(() => - agentControls.getSpans().then(spans => { - expect(spans.length).to.equal(2); - - const httpEntry = verifyHttpRootEntry({ - spans, - apiPath: '/request', - pid: String(controls.getPid()) - }); - - verifyExitSpan({ - spanName: 'node.http.client', - spans, - parent: httpEntry, - withError: false, - pid: String(controls.getPid()), - dataProperty: 'http', - extraTests: [ - span => { - expect(span.data.http.method).to.equal('GET'); - expect(span.data.http.url).to.equal(`http://127.0.0.1:${agentControls.agentPort}/ping`); - expect(span.data.http.status).to.equal(200); - } - ] - }); - }) - ) - )); + verifyExitSpan({ + spanName: 'node.http.client', + spans, + parent: httpEntry, + withError: false, + pid: String(controls.getPid()), + dataProperty: 'http', + extraTests: [ + span => { + expect(span.data.http.method).to.equal('GET'); + expect(span.data.http.url).to.equal(`http://127.0.0.1:${agentControls.agentPort}/ping`); + expect(span.data.http.status).to.equal(200); + } + ] + }); + }) + ) + )); + }); });