From 412d5dbd86c20bd921bb00454d095363b6723cc6 Mon Sep 17 00:00:00 2001 From: JPeer264 Date: Sat, 10 Oct 2026 10:10:45 +0200 Subject: [PATCH] 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 --- .../config/conflictingDebugOptions.test.ts | 44 ++++++++----------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/packages/nextjs/test/config/conflictingDebugOptions.test.ts b/packages/nextjs/test/config/conflictingDebugOptions.test.ts index 6356fcc9bfdc..0a877af8958a 100644 --- a/packages/nextjs/test/config/conflictingDebugOptions.test.ts +++ b/packages/nextjs/test/config/conflictingDebugOptions.test.ts @@ -1,8 +1,21 @@ import { JSDOM } from 'jsdom'; import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from 'vitest'; +import * as client from '../../src/client/index.js'; +import * as edge from '../../src/edge/index.js'; +import * as server from '../../src/server/index.js'; const TEST_DSN = 'https://public@dsn.ingest.sentry.io/1337'; +const debugBuild = vi.hoisted(() => ({ value: true })); + +// The entry points read `DEBUG_BUILD` inside `init()`, so a getter lets each test switch it without +// re-importing the SDK module graphs. +vi.mock('../../src/common/debug-build.js', () => ({ + get DEBUG_BUILD() { + return debugBuild.value; + }, +})); + function didWarnAboutDebugRemoved(warnSpy: ReturnType): boolean { return warnSpy.mock.calls.some(call => call.some( @@ -21,15 +34,7 @@ describe('debug: true + removeDebugLogging warning', () => { let originalAddEventListener: unknown; let originalRemoveEventListener: unknown; - beforeAll(async () => { - // Pre-warm V8 compilation cache for the large SDK module graphs. - // Without this, the first dynamic import after vi.resetModules() can hang - // because vitest needs to compile the entire module graph from scratch. - await import('../../src/client/index.js'); - await import('../../src/server/index.js'); - await import('../../src/edge/index.js'); - vi.resetModules(); - + beforeAll(() => { dom = new JSDOM('', { url: 'https://example.com/' }); originalDocument = (globalThis as any).document; @@ -52,41 +57,30 @@ describe('debug: true + removeDebugLogging warning', () => { afterEach(() => { vi.restoreAllMocks(); - vi.resetModules(); - vi.unmock('../../src/common/debug-build.js'); + debugBuild.value = true; delete process.env.NEXT_OTEL_FETCH_DISABLED; delete process.env.NEXT_PHASE; }); - it('warns on client/server/edge when debug is true but DEBUG_BUILD is false', async () => { - vi.doMock('../../src/common/debug-build.js', () => ({ DEBUG_BUILD: false })); + it('warns on client/server/edge when debug is true but DEBUG_BUILD is false', () => { + debugBuild.value = false; const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); - const client = await import('../../src/client/index.js'); client.init({ dsn: TEST_DSN, debug: true } as any); - - const server = await import('../../src/server/index.js'); server.init({ dsn: TEST_DSN, debug: true } as any); - - const edge = await import('../../src/edge/index.js'); edge.init({ dsn: TEST_DSN, debug: true } as any); expect(didWarnAboutDebugRemoved(warnSpy)).toBe(true); }); - it('does not emit that warning when DEBUG_BUILD is true', async () => { - vi.doMock('../../src/common/debug-build.js', () => ({ DEBUG_BUILD: true })); + it('does not emit that warning when DEBUG_BUILD is true', () => { + debugBuild.value = true; const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); - const client = await import('../../src/client/index.js'); client.init({ dsn: TEST_DSN, debug: true } as any); - - const server = await import('../../src/server/index.js'); server.init({ dsn: TEST_DSN, debug: true } as any); - - const edge = await import('../../src/edge/index.js'); edge.init({ dsn: TEST_DSN, debug: true } as any); expect(didWarnAboutDebugRemoved(warnSpy)).toBe(false);