Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/collector/test/tracing/misc/node-fetch/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`));
}
Expand Down
121 changes: 66 additions & 55 deletions packages/collector/test/tracing/misc/node-fetch/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
]
});
})
)
));
});
});