Skip to content

Commit 3032a21

Browse files
committed
test(node): Remove axios in favor of using fetch
1 parent 2e164e1 commit 3032a21

File tree

8 files changed

+53
-28
lines changed

8 files changed

+53
-28
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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,12 @@ describe('express tracing', () => {
161161
})
162162
.start();
163163

164-
runner.makeRequest('post', '/test-post', { data: { foo: 'bar', other: 1 } });
164+
runner.makeRequest('post', '/test-post', {
165+
data: JSON.stringify({ foo: 'bar', other: 1 }),
166+
headers: {
167+
'Content-Type': 'application/json',
168+
},
169+
});
165170
await runner.completed();
166171
});
167172

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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,12 @@ describe('express tracing', () => {
164164
})
165165
.start();
166166

167-
runner.makeRequest('post', '/test-post', { data: { foo: 'bar', other: 1 } });
167+
runner.makeRequest('post', '/test-post', {
168+
headers: {
169+
'Content-Type': 'application/json',
170+
},
171+
data: JSON.stringify({ foo: 'bar', other: 1 }),
172+
});
168173
await runner.completed();
169174
});
170175

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)