Skip to content

Commit 5d76a7f

Browse files
chargomes1gr1d
authored andcommitted
metatagtransformer test
1 parent 43b383c commit 5d76a7f

File tree

6 files changed

+111
-124
lines changed

6 files changed

+111
-124
lines changed

packages/nextjs/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@
8787
"@sentry/react": "10.21.0",
8888
"@sentry/vercel-edge": "10.21.0",
8989
"@sentry/webpack-plugin": "^4.3.0",
90-
"chalk": "3.0.0",
9190
"resolve": "1.22.8",
9291
"rollup": "^4.35.0",
9392
"stacktrace-parser": "^0.1.10"

packages/nextjs/src/config/loaders/wrappingLoader.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import commonjs from '@rollup/plugin-commonjs';
22
import { stringMatchesSomePattern } from '@sentry/core';
3-
import * as chalk from 'chalk';
43
import * as fs from 'fs';
54
import * as path from 'path';
65
import type { RollupBuild, RollupError } from 'rollup';
@@ -165,9 +164,7 @@ export default function wrappingLoader(
165164
if (!showedMissingAsyncStorageModuleWarning) {
166165
// eslint-disable-next-line no-console
167166
console.warn(
168-
`${chalk.yellow('warn')} - The Sentry SDK could not access the ${chalk.bold.cyan(
169-
'RequestAsyncStorage',
170-
)} module. Certain features may not work. There is nothing you can do to fix this yourself, but future SDK updates may resolve this.\n`,
167+
"[@sentry/nextjs] The Sentry SDK could not access the 'RequestAsyncStorage' module. Certain features may not work. There is nothing you can do to fix this yourself, but future SDK updates may resolve this.",
171168
);
172169
showedMissingAsyncStorageModuleWarning = true;
173170
}

packages/nextjs/src/config/webpack.ts

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
/* eslint-disable max-lines */
33

44
import { debug, escapeStringForRegex, loadModule, parseSemver } from '@sentry/core';
5-
import * as chalk from 'chalk';
65
import * as fs from 'fs';
76
import * as path from 'path';
87
import { sync as resolveSync } from 'resolve';
@@ -245,11 +244,7 @@ export function constructWebpackConfigFunction({
245244
vercelCronsConfig = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'vercel.json'), 'utf8')).crons;
246245
if (vercelCronsConfig) {
247246
debug.log(
248-
`${chalk.cyan(
249-
'info',
250-
)} - Creating Sentry cron monitors for your Vercel Cron Jobs. You can disable this feature by setting the ${chalk.bold.cyan(
251-
'automaticVercelMonitors',
252-
)} option to false in you Next.js config.`,
247+
"[@sentry/nextjs] Creating Sentry cron monitors for your Vercel Cron Jobs. You can disable this feature by setting the 'automaticVercelMonitors' option to false in you Next.js config.",
253248
);
254249
}
255250
}
@@ -259,9 +254,7 @@ export function constructWebpackConfigFunction({
259254
} else {
260255
// log but noop
261256
debug.error(
262-
`${chalk.red(
263-
'error',
264-
)} - Sentry failed to read vercel.json for automatic cron job monitoring instrumentation`,
257+
'[@sentry/nextjs] Failed to read vercel.json for automatic cron job monitoring instrumentation',
265258
e,
266259
);
267260
}
@@ -344,11 +337,7 @@ export function constructWebpackConfigFunction({
344337
) {
345338
// eslint-disable-next-line no-console
346339
console.log(
347-
`${chalk.yellow(
348-
'warn',
349-
)} - It seems like you don't have a global error handler set up. It is recommended that you add a ${chalk.cyan(
350-
'global-error.js',
351-
)} file with Sentry instrumentation so that React rendering errors are reported to Sentry. Read more: https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/#react-render-errors-in-app-router (you can suppress this warning by setting SENTRY_SUPPRESS_GLOBAL_ERROR_HANDLER_FILE_WARNING=1 as environment variable)`,
340+
"[@sentry/nextjs] It seems like you don't have a global error handler set up. It is recommended that you add a 'global-error.js' file with Sentry instrumentation so that React rendering errors are reported to Sentry. Read more: https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/#react-render-errors-in-app-router (you can suppress this warning by setting SENTRY_SUPPRESS_GLOBAL_ERROR_HANDLER_FILE_WARNING=1 as environment variable)",
352341
);
353342
showedMissingGlobalErrorWarningMsg = true;
354343
}
@@ -541,9 +530,7 @@ function warnAboutMissingOnRequestErrorHandler(instrumentationFile: string | nul
541530
if (!process.env.SENTRY_SUPPRESS_INSTRUMENTATION_FILE_WARNING) {
542531
// eslint-disable-next-line no-console
543532
console.warn(
544-
chalk.yellow(
545-
'[@sentry/nextjs] Could not find a Next.js instrumentation file. This indicates an incomplete configuration of the Sentry SDK. An instrumentation file is required for the Sentry SDK to be initialized on the server: https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/#create-initialization-config-files (you can suppress this warning by setting SENTRY_SUPPRESS_INSTRUMENTATION_FILE_WARNING=1 as environment variable)',
546-
),
533+
'[@sentry/nextjs] Could not find a Next.js instrumentation file. This indicates an incomplete configuration of the Sentry SDK. An instrumentation file is required for the Sentry SDK to be initialized on the server: https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/#create-initialization-config-files (you can suppress this warning by setting SENTRY_SUPPRESS_INSTRUMENTATION_FILE_WARNING=1 as environment variable)',
547534
);
548535
}
549536
return;
@@ -552,9 +539,7 @@ function warnAboutMissingOnRequestErrorHandler(instrumentationFile: string | nul
552539
if (!instrumentationFile.includes('onRequestError')) {
553540
// eslint-disable-next-line no-console
554541
console.warn(
555-
chalk.yellow(
556-
'[@sentry/nextjs] Could not find `onRequestError` hook in instrumentation file. This indicates outdated configuration of the Sentry SDK. Use `Sentry.captureRequestError` to instrument the `onRequestError` hook: https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/#errors-from-nested-react-server-components',
557-
),
542+
'[@sentry/nextjs] Could not find `onRequestError` hook in instrumentation file. This indicates outdated configuration of the Sentry SDK. Use `Sentry.captureRequestError` to instrument the `onRequestError` hook: https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/#errors-from-nested-react-server-components',
558543
);
559544
}
560545
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
resolve();
42+
} catch (e) {
43+
reject(e);
44+
}
45+
});
46+
47+
transformer.write('<html><head></head><body>Test</body></html>');
48+
transformer.end();
49+
}));
50+
51+
test('should not modify chunks without head closing tag', () =>
52+
new Promise<void>((resolve, reject) => {
53+
const bodyStream = new PassThrough();
54+
const transformer = getMetaTagTransformer(bodyStream);
55+
56+
let outputData = '';
57+
bodyStream.on('data', chunk => {
58+
outputData += chunk.toString();
59+
});
60+
61+
bodyStream.on('end', () => {
62+
try {
63+
expect(outputData).toContain('<meta name="sentry-trace" content="test-trace-id"></head>');
64+
expect(getTraceMetaTags).toHaveBeenCalled();
65+
resolve();
66+
} catch (e) {
67+
reject(e);
68+
}
69+
});
70+
71+
transformer.write('<html><head></head><body>Test</body></html>');
72+
transformer.end();
73+
}));
74+
75+
test('should handle buffer input', () =>
76+
new Promise<void>((resolve, reject) => {
77+
const bodyStream = new PassThrough();
78+
const transformer = getMetaTagTransformer(bodyStream);
79+
80+
let outputData = '';
81+
bodyStream.on('data', chunk => {
82+
outputData += chunk.toString();
83+
});
84+
85+
bodyStream.on('end', () => {
86+
try {
87+
expect(outputData).toContain('<meta name="sentry-trace" content="test-trace-id"></head>');
88+
resolve();
89+
} catch (e) {
90+
reject(e);
91+
}
92+
});
93+
94+
transformer.write(Buffer.from('<html><head></head><body>Test</body></html>'));
95+
transformer.end();
96+
}));
97+
});

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

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

yarn.lock

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12615,14 +12615,6 @@ [email protected], chalk@^2.0.0, chalk@^2.0.1, chalk@^2.1.0, chalk@^2.4.1, chalk@^2.4.
1261512615
escape-string-regexp "^1.0.5"
1261612616
supports-color "^5.3.0"
1261712617

12618-
[email protected], chalk@^3.0.0:
12619-
version "3.0.0"
12620-
resolved "https://registry.yarnpkg.com/chalk/-/chalk-3.0.0.tgz#3f73c2bf526591f574cc492c51e2456349f844e4"
12621-
integrity sha512-4D3B6Wf41KOYRFdszmDqMCGq5VV/uMAB273JILmO+3jAlh8X4qDtdtgCR3fxtbLEMzSx22QdhnDcJvu2u1fVwg==
12622-
dependencies:
12623-
ansi-styles "^4.1.0"
12624-
supports-color "^7.1.0"
12625-
1262612618
1262712619
version "4.1.0"
1262812620
resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.0.tgz#4e14870a618d9e2edd97dd8345fd9d9dc315646a"
@@ -12642,6 +12634,14 @@ chalk@^1.0.0:
1264212634
strip-ansi "^3.0.0"
1264312635
supports-color "^2.0.0"
1264412636

12637+
chalk@^3.0.0:
12638+
version "3.0.0"
12639+
resolved "https://registry.yarnpkg.com/chalk/-/chalk-3.0.0.tgz#3f73c2bf526591f574cc492c51e2456349f844e4"
12640+
integrity sha512-4D3B6Wf41KOYRFdszmDqMCGq5VV/uMAB273JILmO+3jAlh8X4qDtdtgCR3fxtbLEMzSx22QdhnDcJvu2u1fVwg==
12641+
dependencies:
12642+
ansi-styles "^4.1.0"
12643+
supports-color "^7.1.0"
12644+
1264512645
chalk@^4.0.0, chalk@^4.0.2, chalk@^4.1.0, chalk@^4.1.1, chalk@^4.1.2:
1264612646
version "4.1.2"
1264712647
resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.2.tgz#aac4e2b7734a740867aeb16bf02aad556a1e7a01"

0 commit comments

Comments
 (0)