Skip to content

Commit 7af3e01

Browse files
committed
fix tests
1 parent 14a46fc commit 7af3e01

File tree

7 files changed

+111
-15
lines changed

7 files changed

+111
-15
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as Sentry from '@sentry/browser';
2-
import { ContextLines } from '@sentry/integrations';
2+
import { contextLinesIntegration } from '@sentry/integrations';
33

44
window.Sentry = Sentry;
55

66
Sentry.init({
77
dsn: 'https://[email protected]/1337',
8-
integrations: [new ContextLines()],
8+
integrations: [contextLinesIntegration()],
99
});
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import * as Sentry from '@sentry/browser';
2-
import { HttpClient } from '@sentry/integrations';
2+
import { httpClientIntegration } from '@sentry/integrations';
33

44
window.Sentry = Sentry;
55

66
Sentry.init({
77
dsn: 'https://[email protected]/1337',
8-
integrations: [new HttpClient()],
8+
integrations: [httpClientIntegration()],
99
tracesSampleRate: 1,
1010
sendDefaultPii: true,
1111
});
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import * as Sentry from '@sentry/browser';
2-
import { HttpClient } from '@sentry/integrations';
2+
import { httpClientIntegration } from '@sentry/integrations';
33

44
window.Sentry = Sentry;
55

66
Sentry.init({
77
dsn: 'https://[email protected]/1337',
8-
integrations: [new HttpClient()],
8+
integrations: [httpClientIntegration()],
99
tracesSampleRate: 1,
1010
sendDefaultPii: true,
1111
});

dev-packages/e2e-tests/test-applications/node-express-app/src/app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as Integrations from '@sentry/integrations';
1+
import { httpClientIntegration } from '@sentry/integrations';
22
import * as Sentry from '@sentry/node';
33
import '@sentry/tracing';
44
import express from 'express';
@@ -13,7 +13,7 @@ Sentry.init({
1313
environment: 'qa', // dynamic sampling bias to keep transactions
1414
dsn: process.env.E2E_TEST_DSN,
1515
includeLocalVariables: true,
16-
integrations: [new Integrations.HttpClient()],
16+
integrations: [httpClientIntegration()],
1717
debug: true,
1818
tunnel: `http://localhost:3031/`, // proxy server
1919
tracesSampleRate: 1,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { defineIntegration } from '@sentry/core';
2+
import { rewriteFramesIntegration as originalRewriteFramesIntegration } from '@sentry/integrations';
3+
import type { IntegrationFn, StackFrame } from '@sentry/types';
4+
import { GLOBAL_OBJ, escapeStringForRegex } from '@sentry/utils';
5+
6+
const globalWithInjectedValues = GLOBAL_OBJ as typeof GLOBAL_OBJ & {
7+
__rewriteFramesDistDir__?: string;
8+
};
9+
10+
type StackFrameIteratee = (frame: StackFrame) => StackFrame;
11+
interface RewriteFramesOptions {
12+
root?: string;
13+
prefix?: string;
14+
iteratee?: StackFrameIteratee;
15+
}
16+
17+
export const customRewriteFramesIntegration = ((options?: RewriteFramesOptions) => {
18+
// This value is injected at build time, based on the output directory specified in the build config. Though a default
19+
// is set there, we set it here as well, just in case something has gone wrong with the injection.
20+
const distDirName = globalWithInjectedValues.__rewriteFramesDistDir__;
21+
22+
if (distDirName) {
23+
const distDirAbsPath = distDirName.replace(/(\/|\\)$/, ''); // We strip trailing slashes because "app:///_next" also doesn't have one
24+
25+
// Normally we would use `path.resolve` to obtain the absolute path we will strip from the stack frame to align with
26+
// the uploaded artifacts, however we don't have access to that API in edge so we need to be a bit more lax.
27+
// eslint-disable-next-line @sentry-internal/sdk/no-regexp-constructor -- user input is escaped
28+
const SOURCEMAP_FILENAME_REGEX = new RegExp(`.*${escapeStringForRegex(distDirAbsPath)}`);
29+
30+
return originalRewriteFramesIntegration({
31+
iteratee: frame => {
32+
frame.filename = frame.filename?.replace(SOURCEMAP_FILENAME_REGEX, 'app:///_next');
33+
return frame;
34+
},
35+
...options,
36+
});
37+
}
38+
39+
// Do nothing if we can't find a distDirName
40+
return {
41+
// eslint-disable-next-line deprecation/deprecation
42+
name: 'RewriteFrames',
43+
// eslint-disable-next-line @typescript-eslint/no-empty-function
44+
setupOnce: () => {},
45+
processEvent: event => event,
46+
};
47+
}) satisfies IntegrationFn;
48+
49+
export const rewriteFramesIntegration = defineIntegration(customRewriteFramesIntegration);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import * as path from 'path';
2+
import { defineIntegration } from '@sentry/core';
3+
import { rewriteFramesIntegration as originalRewriteFramesIntegration } from '@sentry/integrations';
4+
import type { IntegrationFn, StackFrame } from '@sentry/types';
5+
import { escapeStringForRegex } from '@sentry/utils';
6+
7+
const globalWithInjectedValues = global as typeof global & {
8+
__rewriteFramesDistDir__?: string;
9+
};
10+
11+
type StackFrameIteratee = (frame: StackFrame) => StackFrame;
12+
interface RewriteFramesOptions {
13+
root?: string;
14+
prefix?: string;
15+
iteratee?: StackFrameIteratee;
16+
}
17+
18+
export const customRewriteFramesIntegration = ((options?: RewriteFramesOptions) => {
19+
// This value is injected at build time, based on the output directory specified in the build config. Though a default
20+
// is set there, we set it here as well, just in case something has gone wrong with the injection.
21+
const distDirName = globalWithInjectedValues.__rewriteFramesDistDir__;
22+
23+
if (distDirName) {
24+
// nextjs always puts the build directory at the project root level, which is also where you run `next start` from, so
25+
// we can read in the project directory from the currently running process
26+
const distDirAbsPath = path.resolve(distDirName).replace(/(\/|\\)$/, ''); // We strip trailing slashes because "app:///_next" also doesn't have one
27+
// eslint-disable-next-line @sentry-internal/sdk/no-regexp-constructor -- user input is escaped
28+
const SOURCEMAP_FILENAME_REGEX = new RegExp(escapeStringForRegex(distDirAbsPath));
29+
30+
return originalRewriteFramesIntegration({
31+
iteratee: frame => {
32+
frame.filename = frame.filename?.replace(SOURCEMAP_FILENAME_REGEX, 'app:///_next');
33+
return frame;
34+
},
35+
...options,
36+
});
37+
}
38+
39+
// Do nothing if we can't find a distDirName
40+
return {
41+
// eslint-disable-next-line deprecation/deprecation
42+
name: 'RewriteFrames',
43+
// eslint-disable-next-line @typescript-eslint/no-empty-function
44+
setupOnce: () => {},
45+
processEvent: event => event,
46+
};
47+
}) satisfies IntegrationFn;
48+
49+
export const rewriteFramesIntegration = defineIntegration(customRewriteFramesIntegration);

packages/sveltekit/test/server/rewriteFramesIntegration.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RewriteFrames } from '@sentry/integrations';
1+
import { rewriteFramesIntegration } from '@sentry/integrations';
22
import type { Event, StackFrame } from '@sentry/types';
33
import { basename } from '@sentry/utils';
44

@@ -25,10 +25,8 @@ describe('rewriteFramesIteratee', () => {
2525
module: '3-ab34d22f.js',
2626
};
2727

28-
// eslint-disable-next-line deprecation/deprecation
29-
const originalRewriteFrames = new RewriteFrames();
30-
// eslint-disable-next-line deprecation/deprecation
31-
const rewriteFrames = new RewriteFrames({ iteratee: rewriteFramesIteratee });
28+
const originalRewriteFrames = rewriteFramesIntegration();
29+
const rewriteFrames = rewriteFramesIntegration({ iteratee: rewriteFramesIteratee });
3230

3331
const event: Event = {
3432
exception: {
@@ -42,8 +40,8 @@ describe('rewriteFramesIteratee', () => {
4240
},
4341
};
4442

45-
const originalResult = originalRewriteFrames.processEvent(event);
46-
const result = rewriteFrames.processEvent(event);
43+
const originalResult = originalRewriteFrames.processEvent?.(event, {}, {} as any);
44+
const result = rewriteFrames.processEvent?.(event, {}, {} as any) as Event;
4745

4846
expect(result.exception?.values?.[0]?.stacktrace?.frames?.[0]).toEqual({
4947
filename: 'app:///3-ab34d22f.js',

0 commit comments

Comments
 (0)