Skip to content

test(node): Remove axios in favor of using fetch #16172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 30, 2025
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
1 change: 0 additions & 1 deletion dev-packages/node-integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"ai": "^4.0.6",
"amqplib": "^0.10.7",
"apollo-server": "^3.11.1",
"axios": "^1.7.7",
"body-parser": "^1.20.3",
"connect": "^3.7.0",
"cors": "^2.8.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ describe('express tracing', () => {

test.each([['array1'], ['array5']])(
'should set a correct transaction name for routes consisting of arrays of routes for %p',
((segment: string, done: () => void) => {
createRunner(__dirname, 'server.js')
async (segment: string) => {
const runner = await createRunner(__dirname, 'server.js')
.expect({
transaction: {
transaction: 'GET /test/array1,/\\/test\\/array[2-9]/',
Expand All @@ -101,9 +101,10 @@ describe('express tracing', () => {
},
},
})
.start(done)
.makeRequest('get', `/test/${segment}`);
}) as any,
.start();
await runner.makeRequest('get', `/test/${segment}`);
await runner.completed();
},
);

test.each([
Expand All @@ -113,8 +114,8 @@ describe('express tracing', () => {
['arr/requiredPath'],
['arr/required/lastParam'],
['arr55/required/lastParam'],
])('should handle more complex regexes in route arrays correctly for %p', ((segment: string, done: () => void) => {
createRunner(__dirname, 'server.js')
])('should handle more complex regexes in route arrays correctly for %p', async (segment: string) => {
const runner = await createRunner(__dirname, 'server.js')
.expect({
transaction: {
transaction: 'GET /test/arr/:id,/\\/test\\/arr[0-9]*\\/required(path)?(\\/optionalPath)?\\/(lastParam)?/',
Expand All @@ -135,9 +136,10 @@ describe('express tracing', () => {
},
},
})
.start(done)
.makeRequest('get', `/test/${segment}`);
}) as any);
.start();
await runner.makeRequest('get', `/test/${segment}`);
await runner.completed();
});

describe('request data', () => {
test('correctly captures JSON request data', async () => {
Expand All @@ -161,7 +163,12 @@ describe('express tracing', () => {
})
.start();

runner.makeRequest('post', '/test-post', { data: { foo: 'bar', other: 1 } });
runner.makeRequest('post', '/test-post', {
data: JSON.stringify({ foo: 'bar', other: 1 }),
headers: {
'Content-Type': 'application/json',
},
});
await runner.completed();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ describe('express without tracing', () => {
})
.start();

runner.makeRequest('post', '/test-post', { data: { foo: 'bar', other: 1 } });
runner.makeRequest('post', '/test-post', {
headers: {
'Content-Type': 'application/json',
},
data: JSON.stringify({ foo: 'bar', other: 1 }),
});
await runner.completed();
});

Expand Down
29 changes: 18 additions & 11 deletions dev-packages/node-integration-tests/suites/express/tracing/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ describe('express tracing', () => {

test.each([['array1'], ['array5']])(
'should set a correct transaction name for routes consisting of arrays of routes for %p',
((segment: string, done: () => void) => {
createRunner(__dirname, 'server.js')
async (segment: string) => {
const runner = await createRunner(__dirname, 'server.js')
.expect({
transaction: {
transaction: 'GET /test/array1,/\\/test\\/array[2-9]/',
Expand All @@ -102,9 +102,10 @@ describe('express tracing', () => {
},
},
})
.start(done)
.makeRequest('get', `/test/${segment}`);
}) as any,
.start();
await runner.makeRequest('get', `/test/${segment}`);
await runner.completed();
},
);

test.each([
Expand All @@ -116,8 +117,8 @@ describe('express tracing', () => {
['arr55/required/lastParam'],
['arr/requiredPath/optionalPath/'],
['arr/requiredPath/optionalPath/lastParam'],
])('should handle more complex regexes in route arrays correctly for %p', ((segment: string, done: () => void) => {
createRunner(__dirname, 'server.js')
])('should handle more complex regexes in route arrays correctly for %p', async (segment: string) => {
const runner = await createRunner(__dirname, 'server.js')
.expect({
transaction: {
transaction: 'GET /test/arr/:id,/\\/test\\/arr[0-9]*\\/required(path)?(\\/optionalPath)?\\/(lastParam)?/',
Expand All @@ -138,9 +139,10 @@ describe('express tracing', () => {
},
},
})
.start(done)
.makeRequest('get', `/test/${segment}`);
}) as any);
.start();
await runner.makeRequest('get', `/test/${segment}`);
await runner.completed();
});

describe('request data', () => {
test('correctly captures JSON request data', async () => {
Expand All @@ -164,7 +166,12 @@ describe('express tracing', () => {
})
.start();

runner.makeRequest('post', '/test-post', { data: { foo: 'bar', other: 1 } });
runner.makeRequest('post', '/test-post', {
headers: {
'Content-Type': 'application/json',
},
data: JSON.stringify({ foo: 'bar', other: 1 }),
});
await runner.completed();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ describe('express without tracing', () => {
})
.start();

runner.makeRequest('post', '/test-post', { data: { foo: 'bar', other: 1 } });
runner.makeRequest('post', '/test-post', {
headers: {
'Content-Type': 'application/json',
},
data: JSON.stringify({ foo: 'bar', other: 1 }),
});
await runner.completed();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ describe('getTraceMetaTags', () => {

const runner = createRunner(__dirname, 'server.js').start();

const response = await runner.makeRequest('get', '/test', {
const response = await runner.makeRequest<{ response: string }>('get', '/test', {
headers: {
'sentry-trace': `${traceId}-${parentSpanId}-1`,
baggage: 'sentry-environment=production,sentry-sample_rand=0.42',
},
});

// @ts-ignore - response is defined, types just don't reflect it
const html = response?.response as unknown as string;
const html = response?.response;

expect(html).toMatch(/<meta name="sentry-trace" content="cd7ee7a6fe3ebe7ab9c3271559bc203c-[a-z0-9]{16}-1"\/>/);
expect(html).toContain('<meta name="baggage" content="sentry-environment=production,sentry-sample_rand=0.42"/>');
Expand All @@ -29,12 +28,11 @@ describe('getTraceMetaTags', () => {
test('injects <meta> tags with new trace if no incoming headers', async () => {
const runner = createRunner(__dirname, 'server.js').start();

const response = await runner.makeRequest('get', '/test');
const response = await runner.makeRequest<{ response: string }>('get', '/test');

// @ts-ignore - response is defined, types just don't reflect it
const html = response?.response as unknown as string;
const html = response?.response;

const traceId = html.match(/<meta name="sentry-trace" content="([a-z0-9]{32})-[a-z0-9]{16}-1"\/>/)?.[1];
const traceId = html?.match(/<meta name="sentry-trace" content="([a-z0-9]{32})-[a-z0-9]{16}-1"\/>/)?.[1];
expect(traceId).not.toBeUndefined();

expect(html).toContain('<meta name="baggage"');
Expand All @@ -44,12 +42,11 @@ describe('getTraceMetaTags', () => {
test('injects <meta> tags with negative sampling decision if tracesSampleRate is 0', async () => {
const runner = createRunner(__dirname, 'server-tracesSampleRate-zero.js').start();

const response = await runner.makeRequest('get', '/test');
const response = await runner.makeRequest<{ response: string }>('get', '/test');

// @ts-ignore - response is defined, types just don't reflect it
const html = response?.response as unknown as string;
const html = response?.response;

const traceId = html.match(/<meta name="sentry-trace" content="([a-z0-9]{32})-[a-z0-9]{16}-0"\/>/)?.[1];
const traceId = html?.match(/<meta name="sentry-trace" content="([a-z0-9]{32})-[a-z0-9]{16}-0"\/>/)?.[1];
expect(traceId).not.toBeUndefined();

expect(html).toContain('<meta name="baggage"');
Expand All @@ -63,15 +60,14 @@ describe('getTraceMetaTags', () => {

const runner = createRunner(__dirname, 'server-sdk-disabled.js').start();

const response = await runner.makeRequest('get', '/test', {
const response = await runner.makeRequest<{ response: string }>('get', '/test', {
headers: {
'sentry-trace': `${traceId}-${parentSpanId}-1`,
baggage: 'sentry-environment=production',
},
});

// @ts-ignore - response is defined, types just don't reflect it
const html = response?.response as unknown as string;
const html = response?.response;

expect(html).not.toContain('"sentry-trace"');
expect(html).not.toContain('"baggage"');
Expand Down
26 changes: 18 additions & 8 deletions dev-packages/node-integration-tests/utils/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import type {
TransactionEvent,
} from '@sentry/core';
import { normalize } from '@sentry/core';
import axios from 'axios';
import { execSync, spawn, spawnSync } from 'child_process';
import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'fs';
import { join } from 'path';
Expand Down Expand Up @@ -161,7 +160,7 @@ type StartResult = {
makeRequest<T>(
method: 'get' | 'post',
path: string,
options?: { headers?: Record<string, string>; data?: unknown; expectError?: boolean },
options?: { headers?: Record<string, string>; data?: BodyInit; expectError?: boolean },
): Promise<T | undefined>;
};

Expand Down Expand Up @@ -532,7 +531,7 @@ export function createRunner(...paths: string[]) {
makeRequest: async function <T>(
method: 'get' | 'post',
path: string,
options: { headers?: Record<string, string>; data?: unknown; expectError?: boolean } = {},
options: { headers?: Record<string, string>; data?: BodyInit; expectError?: boolean } = {},
): Promise<T | undefined> {
try {
await waitFor(() => scenarioServerPort !== undefined, 10_000, 'Timed out waiting for server port');
Expand All @@ -542,22 +541,33 @@ export function createRunner(...paths: string[]) {
}

const url = `http://localhost:${scenarioServerPort}${path}`;
const data = options.data;
const body = options.data;
const headers = options.headers || {};
const expectError = options.expectError || false;

if (process.env.DEBUG) log('making request', method, url, headers, data);
if (process.env.DEBUG) log('making request', method, url, headers, body);

try {
const res =
method === 'post' ? await axios.post(url, data, { headers }) : await axios.get(url, { headers });
const res = await fetch(url, { headers, method, body });

if (!res.ok) {
if (!expectError) {
complete(new Error(`Expected request to "${path}" to succeed, but got a ${res.status} response`));
}

return;
}

if (expectError) {
complete(new Error(`Expected request to "${path}" to fail, but got a ${res.status} response`));
return;
}

return res.data;
if (res.headers.get('content-type')?.includes('application/json')) {
return await res.json();
}

return (await res.text()) as T;
} catch (e) {
if (expectError) {
return;
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10168,7 +10168,7 @@ aws-ssl-profiles@^1.1.1:
resolved "https://registry.yarnpkg.com/aws-ssl-profiles/-/aws-ssl-profiles-1.1.2.tgz#157dd77e9f19b1d123678e93f120e6f193022641"
integrity sha512-NZKeq9AfyQvEeNlN0zSYAaWrmBffJh3IELMZfRpJVWgrpEbtEpnjvzqBPf+mxoI287JohRDoa+/nsfqqiZmF6g==

axios@1.8.2, axios@^1.0.0, axios@^1.7.7:
axios@1.8.2, axios@^1.0.0:
version "1.8.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.8.2.tgz#fabe06e241dfe83071d4edfbcaa7b1c3a40f7979"
integrity sha512-ls4GYBm5aig9vWx8AWDSGLpnpDQRtWAfrjU+EuytuODrFBkqesN2RkOQCBzrA1RQNHw1SmRMSDDDSwzNAYQ6Rg==
Expand Down
Loading