Skip to content

Commit 3e02739

Browse files
JPeer264claude
andauthored
test(nextjs): Switch DEBUG_BUILD with a getter instead of re-importing the SDK (#25279)
`conflictingDebugOptions.test.ts` now mocks `DEBUG_BUILD` one time, with a getter over a `vi.hoisted` value, and it imports the client, server and edge entry points one time. Before, each test called `vi.doMock` and `vi.resetModules` and imported all three entry points again, and `beforeAll` "pre-warmed" them one more time. ## Why Under CI load, evaluating the three module graphs again took longer than the 10 s hook timeout. Now the two tests take a few milliseconds (25 ms and 6 ms locally, 1.68 s before). The "vi.unmock is not at the top level" warning is also gone. Failed on CI: https://github.com/getsentry/sentry-javascript/actions/runs/38034725173/job/114166137692 Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 5a724d8 commit 3e02739

1 file changed

Lines changed: 19 additions & 25 deletions

File tree

‎packages/nextjs/test/config/conflictingDebugOptions.test.ts‎

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
import { JSDOM } from 'jsdom';
22
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from 'vitest';
3+
import * as client from '../../src/client/index.js';
4+
import * as edge from '../../src/edge/index.js';
5+
import * as server from '../../src/server/index.js';
36

47
const TEST_DSN = 'https://public@dsn.ingest.sentry.io/1337';
58

9+
const debugBuild = vi.hoisted(() => ({ value: true }));
10+
11+
// The entry points read `DEBUG_BUILD` inside `init()`, so a getter lets each test switch it without
12+
// re-importing the SDK module graphs.
13+
vi.mock('../../src/common/debug-build.js', () => ({
14+
get DEBUG_BUILD() {
15+
return debugBuild.value;
16+
},
17+
}));
18+
619
function didWarnAboutDebugRemoved(warnSpy: ReturnType<typeof vi.spyOn>): boolean {
720
return warnSpy.mock.calls.some(call =>
821
call.some(
@@ -21,15 +34,7 @@ describe('debug: true + removeDebugLogging warning', () => {
2134
let originalAddEventListener: unknown;
2235
let originalRemoveEventListener: unknown;
2336

24-
beforeAll(async () => {
25-
// Pre-warm V8 compilation cache for the large SDK module graphs.
26-
// Without this, the first dynamic import after vi.resetModules() can hang
27-
// because vitest needs to compile the entire module graph from scratch.
28-
await import('../../src/client/index.js');
29-
await import('../../src/server/index.js');
30-
await import('../../src/edge/index.js');
31-
vi.resetModules();
32-
37+
beforeAll(() => {
3338
dom = new JSDOM('<!doctype html><html><head></head><body></body></html>', { url: 'https://example.com/' });
3439

3540
originalDocument = (globalThis as any).document;
@@ -52,41 +57,30 @@ describe('debug: true + removeDebugLogging warning', () => {
5257

5358
afterEach(() => {
5459
vi.restoreAllMocks();
55-
vi.resetModules();
56-
vi.unmock('../../src/common/debug-build.js');
60+
debugBuild.value = true;
5761
delete process.env.NEXT_OTEL_FETCH_DISABLED;
5862
delete process.env.NEXT_PHASE;
5963
});
6064

61-
it('warns on client/server/edge when debug is true but DEBUG_BUILD is false', async () => {
62-
vi.doMock('../../src/common/debug-build.js', () => ({ DEBUG_BUILD: false }));
65+
it('warns on client/server/edge when debug is true but DEBUG_BUILD is false', () => {
66+
debugBuild.value = false;
6367

6468
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
6569

66-
const client = await import('../../src/client/index.js');
6770
client.init({ dsn: TEST_DSN, debug: true } as any);
68-
69-
const server = await import('../../src/server/index.js');
7071
server.init({ dsn: TEST_DSN, debug: true } as any);
71-
72-
const edge = await import('../../src/edge/index.js');
7372
edge.init({ dsn: TEST_DSN, debug: true } as any);
7473

7574
expect(didWarnAboutDebugRemoved(warnSpy)).toBe(true);
7675
});
7776

78-
it('does not emit that warning when DEBUG_BUILD is true', async () => {
79-
vi.doMock('../../src/common/debug-build.js', () => ({ DEBUG_BUILD: true }));
77+
it('does not emit that warning when DEBUG_BUILD is true', () => {
78+
debugBuild.value = true;
8079

8180
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
8281

83-
const client = await import('../../src/client/index.js');
8482
client.init({ dsn: TEST_DSN, debug: true } as any);
85-
86-
const server = await import('../../src/server/index.js');
8783
server.init({ dsn: TEST_DSN, debug: true } as any);
88-
89-
const edge = await import('../../src/edge/index.js');
9084
edge.init({ dsn: TEST_DSN, debug: true } as any);
9185

9286
expect(didWarnAboutDebugRemoved(warnSpy)).toBe(false);

0 commit comments

Comments
 (0)