Skip to content

Commit 412d5db

Browse files
JPeer264claude
andcommitted
test(nextjs): Switch DEBUG_BUILD with a getter instead of re-importing the SDK
The test mocked DEBUG_BUILD with vi.doMock, then called vi.resetModules and imported the client, server and edge entry points again in each test. Vitest had to evaluate three large module graphs every time, and beforeAll "pre-warmed" them once more. Under CI load this took longer than the 10s hook timeout. The entry points read DEBUG_BUILD inside init(), so one top-level vi.mock with a getter over a vi.hoisted value lets each test switch it. The entry points are imported once, and the tests take milliseconds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1d8c6bd commit 412d5db

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)