Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9c0397c

Browse files
authored
test(react-router): Fix getMetaTagTransformer tests for Vitest compatibility (#18013)
- Migrated from Jest `done` callback to [Promise-based async test pattern](https://vitest.dev/guide/migration.html#done-callback) - Fixed test assertion by adding missing `</head>` tag to trigger `getTraceMetaTags` call (the test did not work before as `getTraceMetaTags` is only called when there is a closing `head` tag - Corrected stream piping logic to properly test transformer functionality The getMetaTagTransformer function internally pipes the transformer TO the bodyStream (`htmlMetaTagTransformer.pipe(body)`). So the correct flow is: 1. Write data to the transformer 2. Transformer processes it and pipes to bodyStream 3. Read the output from bodyStream
1 parent 0bac0ea commit 9c0397c

File tree

2 files changed

+127
-91
lines changed

2 files changed

+127
-91
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import { getTraceMetaTags } from '@sentry/core';
2+
import { PassThrough } from 'stream';
3+
import { beforeEach, describe, expect, test, vi } from 'vitest';
4+
import { getMetaTagTransformer } from '../../src/server/getMetaTagTransformer';
5+
6+
vi.mock('@opentelemetry/core', () => ({
7+
RPCType: { HTTP: 'http' },
8+
getRPCMetadata: vi.fn(),
9+
}));
10+
11+
vi.mock('@sentry/core', () => ({
12+
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE: 'sentry.source',
13+
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN: 'sentry.origin',
14+
getActiveSpan: vi.fn(),
15+
getRootSpan: vi.fn(),
16+
getTraceMetaTags: vi.fn(),
17+
}));
18+
19+
describe('getMetaTagTransformer', () => {
20+
beforeEach(() => {
21+
vi.clearAllMocks();
22+
(getTraceMetaTags as unknown as ReturnType<typeof vi.fn>).mockReturnValue(
23+
'<meta name="sentry-trace" content="test-trace-id">',
24+
);
25+
});
26+
27+
test('should inject meta tags before closing head tag', () =>
28+
new Promise<void>((resolve, reject) => {
29+
const bodyStream = new PassThrough();
30+
const transformer = getMetaTagTransformer(bodyStream);
31+
32+
let outputData = '';
33+
bodyStream.on('data', chunk => {
34+
outputData += chunk.toString();
35+
});
36+
37+
bodyStream.on('end', () => {
38+
try {
39+
expect(outputData).toContain('<meta name="sentry-trace" content="test-trace-id"></head>');
40+
expect(outputData).not.toContain('</head></head>');
41+
expect(getTraceMetaTags).toHaveBeenCalledTimes(1);
42+
resolve();
43+
} catch (e) {
44+
reject(e);
45+
}
46+
});
47+
48+
transformer.write('<html><head></head><body>Test</body></html>');
49+
transformer.end();
50+
}));
51+
52+
test('should not modify chunks without head closing tag', () =>
53+
new Promise<void>((resolve, reject) => {
54+
const bodyStream = new PassThrough();
55+
const transformer = getMetaTagTransformer(bodyStream);
56+
57+
let outputData = '';
58+
bodyStream.on('data', chunk => {
59+
outputData += chunk.toString();
60+
});
61+
62+
bodyStream.on('end', () => {
63+
try {
64+
expect(outputData).toBe('<html><body>Test</body></html>');
65+
expect(outputData).not.toContain('sentry-trace');
66+
expect(getTraceMetaTags).not.toHaveBeenCalled();
67+
resolve();
68+
} catch (e) {
69+
reject(e);
70+
}
71+
});
72+
73+
transformer.write('<html><body>Test</body></html>');
74+
transformer.end();
75+
}));
76+
77+
test('should handle buffer input', () =>
78+
new Promise<void>((resolve, reject) => {
79+
const bodyStream = new PassThrough();
80+
const transformer = getMetaTagTransformer(bodyStream);
81+
82+
let outputData = '';
83+
bodyStream.on('data', chunk => {
84+
outputData += chunk.toString();
85+
});
86+
87+
bodyStream.on('end', () => {
88+
try {
89+
expect(outputData).toContain('<meta name="sentry-trace" content="test-trace-id"></head>');
90+
expect(getTraceMetaTags).toHaveBeenCalledTimes(1);
91+
resolve();
92+
} catch (e) {
93+
reject(e);
94+
}
95+
});
96+
97+
transformer.write(Buffer.from('<html><head></head><body>Test</body></html>'));
98+
transformer.end();
99+
}));
100+
101+
test('should handle multiple chunks', () =>
102+
new Promise<void>((resolve, reject) => {
103+
const bodyStream = new PassThrough();
104+
const transformer = getMetaTagTransformer(bodyStream);
105+
106+
let outputData = '';
107+
bodyStream.on('data', chunk => {
108+
outputData += chunk.toString();
109+
});
110+
111+
bodyStream.on('end', () => {
112+
try {
113+
expect(outputData).toContain('<meta name="sentry-trace" content="test-trace-id"></head>');
114+
expect(outputData).toContain('<body>Test content</body>');
115+
expect(getTraceMetaTags).toHaveBeenCalledTimes(1);
116+
resolve();
117+
} catch (e) {
118+
reject(e);
119+
}
120+
});
121+
122+
transformer.write('<html><head>');
123+
transformer.write('</head><body>Test content</body>');
124+
transformer.write('</html>');
125+
transformer.end();
126+
}));
127+
});

‎packages/react-router/test/server/getMetaTagTransformer.ts‎

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /