Skip to content

Commit b7c095b

Browse files
authored
test(node): Remove axios in favor of using fetch (#16172)
This removes usage of `axios` in our node-integration-test runner, in favor of just using `fetch`.
1 parent 2f2b66b commit b7c095b

File tree

8 files changed

+77
-48
lines changed

8 files changed

+77
-48
lines changed

dev-packages/node-integration-tests/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
"ai": "^4.0.6",
4040
"amqplib": "^0.10.7",
4141
"apollo-server": "^3.11.1",
42-
"axios": "^1.7.7",
4342
"body-parser": "^1.20.3",
4443
"connect": "^3.7.0",
4544
"cors": "^2.8.5",

dev-packages/node-integration-tests/suites/express-v5/tracing/test.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ describe('express tracing', () => {
7979

8080
test.each([['array1'], ['array5']])(
8181
'should set a correct transaction name for routes consisting of arrays of routes for %p',
82-
((segment: string, done: () => void) => {
83-
createRunner(__dirname, 'server.js')
82+
async (segment: string) => {
83+
const runner = await createRunner(__dirname, 'server.js')
8484
.expect({
8585
transaction: {
8686
transaction: 'GET /test/array1,/\\/test\\/array[2-9]/',
@@ -101,9 +101,10 @@ describe('express tracing', () => {
101101
},
102102
},
103103
})
104-
.start(done)
105-
.makeRequest('get', `/test/${segment}`);
106-
}) as any,
104+
.start();
105+
await runner.makeRequest('get', `/test/${segment}`);
106+
await runner.completed();
107+
},
107108
);
108109

109110
test.each([
@@ -113,8 +114,8 @@ describe('express tracing', () => {
113114
['arr/requiredPath'],
114115
['arr/required/lastParam'],
115116
['arr55/required/lastParam'],
116-
])('should handle more complex regexes in route arrays correctly for %p', ((segment: string, done: () => void) => {
117-
createRunner(__dirname, 'server.js')
117+
])('should handle more complex regexes in route arrays correctly for %p', async (segment: string) => {
118+
const runner = await createRunner(__dirname, 'server.js')
118119
.expect({
119120
transaction: {
120121
transaction: 'GET /test/arr/:id,/\\/test\\/arr[0-9]*\\/required(path)?(\\/optionalPath)?\\/(lastParam)?/',
@@ -135,9 +136,10 @@ describe('express tracing', () => {
135136
},
136137
},
137138
})
138-
.start(done)
139-
.makeRequest('get', `/test/${segment}`);
140-
}) as any);
139+
.start();
140+
await runner.makeRequest('get', `/test/${segment}`);
141+
await runner.completed();
142+
});
141143

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

164-
runner.makeRequest('post', '/test-post', { data: { foo: 'bar', other: 1 } });
166+
runner.makeRequest('post', '/test-post', {
167+
data: JSON.stringify({ foo: 'bar', other: 1 }),
168+
headers: {
169+
'Content-Type': 'application/json',
170+
},
171+
});
165172
await runner.completed();
166173
});
167174

dev-packages/node-integration-tests/suites/express-v5/without-tracing/test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ describe('express without tracing', () => {
5454
})
5555
.start();
5656

57-
runner.makeRequest('post', '/test-post', { data: { foo: 'bar', other: 1 } });
57+
runner.makeRequest('post', '/test-post', {
58+
headers: {
59+
'Content-Type': 'application/json',
60+
},
61+
data: JSON.stringify({ foo: 'bar', other: 1 }),
62+
});
5863
await runner.completed();
5964
});
6065

dev-packages/node-integration-tests/suites/express/tracing/test.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ describe('express tracing', () => {
8080

8181
test.each([['array1'], ['array5']])(
8282
'should set a correct transaction name for routes consisting of arrays of routes for %p',
83-
((segment: string, done: () => void) => {
84-
createRunner(__dirname, 'server.js')
83+
async (segment: string) => {
84+
const runner = await createRunner(__dirname, 'server.js')
8585
.expect({
8686
transaction: {
8787
transaction: 'GET /test/array1,/\\/test\\/array[2-9]/',
@@ -102,9 +102,10 @@ describe('express tracing', () => {
102102
},
103103
},
104104
})
105-
.start(done)
106-
.makeRequest('get', `/test/${segment}`);
107-
}) as any,
105+
.start();
106+
await runner.makeRequest('get', `/test/${segment}`);
107+
await runner.completed();
108+
},
108109
);
109110

110111
test.each([
@@ -116,8 +117,8 @@ describe('express tracing', () => {
116117
['arr55/required/lastParam'],
117118
['arr/requiredPath/optionalPath/'],
118119
['arr/requiredPath/optionalPath/lastParam'],
119-
])('should handle more complex regexes in route arrays correctly for %p', ((segment: string, done: () => void) => {
120-
createRunner(__dirname, 'server.js')
120+
])('should handle more complex regexes in route arrays correctly for %p', async (segment: string) => {
121+
const runner = await createRunner(__dirname, 'server.js')
121122
.expect({
122123
transaction: {
123124
transaction: 'GET /test/arr/:id,/\\/test\\/arr[0-9]*\\/required(path)?(\\/optionalPath)?\\/(lastParam)?/',
@@ -138,9 +139,10 @@ describe('express tracing', () => {
138139
},
139140
},
140141
})
141-
.start(done)
142-
.makeRequest('get', `/test/${segment}`);
143-
}) as any);
142+
.start();
143+
await runner.makeRequest('get', `/test/${segment}`);
144+
await runner.completed();
145+
});
144146

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

167-
runner.makeRequest('post', '/test-post', { data: { foo: 'bar', other: 1 } });
169+
runner.makeRequest('post', '/test-post', {
170+
headers: {
171+
'Content-Type': 'application/json',
172+
},
173+
data: JSON.stringify({ foo: 'bar', other: 1 }),
174+
});
168175
await runner.completed();
169176
});
170177

dev-packages/node-integration-tests/suites/express/without-tracing/test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ describe('express without tracing', () => {
5454
})
5555
.start();
5656

57-
runner.makeRequest('post', '/test-post', { data: { foo: 'bar', other: 1 } });
57+
runner.makeRequest('post', '/test-post', {
58+
headers: {
59+
'Content-Type': 'application/json',
60+
},
61+
data: JSON.stringify({ foo: 'bar', other: 1 }),
62+
});
5863
await runner.completed();
5964
});
6065

dev-packages/node-integration-tests/suites/tracing/meta-tags/test.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@ describe('getTraceMetaTags', () => {
1212

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

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

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

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

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

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

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

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

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

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

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

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

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

66-
const response = await runner.makeRequest('get', '/test', {
63+
const response = await runner.makeRequest<{ response: string }>('get', '/test', {
6764
headers: {
6865
'sentry-trace': `${traceId}-${parentSpanId}-1`,
6966
baggage: 'sentry-environment=production',
7067
},
7168
});
7269

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

7672
expect(html).not.toContain('"sentry-trace"');
7773
expect(html).not.toContain('"baggage"');

dev-packages/node-integration-tests/utils/runner.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import type {
1212
TransactionEvent,
1313
} from '@sentry/core';
1414
import { normalize } from '@sentry/core';
15-
import axios from 'axios';
1615
import { execSync, spawn, spawnSync } from 'child_process';
1716
import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'fs';
1817
import { join } from 'path';
@@ -161,7 +160,7 @@ type StartResult = {
161160
makeRequest<T>(
162161
method: 'get' | 'post',
163162
path: string,
164-
options?: { headers?: Record<string, string>; data?: unknown; expectError?: boolean },
163+
options?: { headers?: Record<string, string>; data?: BodyInit; expectError?: boolean },
165164
): Promise<T | undefined>;
166165
};
167166

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

544543
const url = `http://localhost:${scenarioServerPort}${path}`;
545-
const data = options.data;
544+
const body = options.data;
546545
const headers = options.headers || {};
547546
const expectError = options.expectError || false;
548547

549-
if (process.env.DEBUG) log('making request', method, url, headers, data);
548+
if (process.env.DEBUG) log('making request', method, url, headers, body);
550549

551550
try {
552-
const res =
553-
method === 'post' ? await axios.post(url, data, { headers }) : await axios.get(url, { headers });
551+
const res = await fetch(url, { headers, method, body });
552+
553+
if (!res.ok) {
554+
if (!expectError) {
555+
complete(new Error(`Expected request to "${path}" to succeed, but got a ${res.status} response`));
556+
}
557+
558+
return;
559+
}
554560

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

560-
return res.data;
566+
if (res.headers.get('content-type')?.includes('application/json')) {
567+
return await res.json();
568+
}
569+
570+
return (await res.text()) as T;
561571
} catch (e) {
562572
if (expectError) {
563573
return;

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10168,7 +10168,7 @@ aws-ssl-profiles@^1.1.1:
1016810168
resolved "https://registry.yarnpkg.com/aws-ssl-profiles/-/aws-ssl-profiles-1.1.2.tgz#157dd77e9f19b1d123678e93f120e6f193022641"
1016910169
integrity sha512-NZKeq9AfyQvEeNlN0zSYAaWrmBffJh3IELMZfRpJVWgrpEbtEpnjvzqBPf+mxoI287JohRDoa+/nsfqqiZmF6g==
1017010170

10171-
[email protected], axios@^1.0.0, axios@^1.7.7:
10171+
[email protected], axios@^1.0.0:
1017210172
version "1.8.2"
1017310173
resolved "https://registry.yarnpkg.com/axios/-/axios-1.8.2.tgz#fabe06e241dfe83071d4edfbcaa7b1c3a40f7979"
1017410174
integrity sha512-ls4GYBm5aig9vWx8AWDSGLpnpDQRtWAfrjU+EuytuODrFBkqesN2RkOQCBzrA1RQNHw1SmRMSDDDSwzNAYQ6Rg==

0 commit comments

Comments
 (0)