diff --git a/packages/utils/src/env.ts b/packages/utils/src/env.ts index 2a2b39969..363f8ae5c 100644 --- a/packages/utils/src/env.ts +++ b/packages/utils/src/env.ts @@ -258,7 +258,7 @@ export function $envpos(name: string, defaultValue: number): number { return parsed; } -const BUN_TEST_ENTRY_PATTERN = /\.(?:test|spec)\.[cm]?[jt]sx?$/; +const BUN_TEST_ENTRY_PATTERN = /[._](?:test|spec)\.[cm]?[jt]sx?$/; /** True when the process is an explicitly marked test child or Bun is running a test entrypoint. */ export function isBunTestRuntime(): boolean { diff --git a/packages/utils/test/env.test.ts b/packages/utils/test/env.test.ts index 55147d5fe..8afe5c108 100644 --- a/packages/utils/test/env.test.ts +++ b/packages/utils/test/env.test.ts @@ -40,9 +40,12 @@ describe("getDbBusyTimeoutMs", () => { } }); }); -async function runRuntimeProbe(env: Record): Promise { +async function runRuntimeProbe( + env: Record, + probePath = runtimeProbePath, +): Promise { const cwd = path.dirname(writeTempEnv("")); - const proc = Bun.spawn([process.execPath, runtimeProbePath], { + const proc = Bun.spawn([process.execPath, probePath], { cwd, env: { ...process.env, ...env }, stdout: "pipe", @@ -168,4 +171,20 @@ describe("isBunTestRuntime", () => { it("honors the private test runner signal", async () => { expect(await runRuntimeProbe({ NODE_ENV: undefined, BUN_ENV: undefined, PI_TEST_RUNTIME: "1" })).toBe(true); }); + + it("recognizes Bun's underscore test entrypoints", async () => { + const dir = path.dirname(writeTempEnv("")); + const underscoreProbePath = path.join(dir, "runtime_test.ts"); + const envModulePath = path.join(import.meta.dir, "..", "src", "env.ts"); + fs.writeFileSync( + underscoreProbePath, + `import { isBunTestRuntime } from ${JSON.stringify(envModulePath)};\nprocess.stdout.write(JSON.stringify(isBunTestRuntime()));\n`, + ); + expect( + await runRuntimeProbe( + { NODE_ENV: "test", BUN_ENV: undefined, PI_TEST_RUNTIME: undefined }, + underscoreProbePath, + ), + ).toBe(true); + }); });