fix(test): restore shared-worker environment and settle imports

This commit is contained in:
Jinjing
2026-11-08 20:07:37 -08:00
parent 4fc4d792be
commit ec1fc29f4b
2 changed files with 90 additions and 2 deletions
@@ -7,7 +7,7 @@ import { afterAll, vi } from 'vitest'
const VITEST_INTERNAL = /\/vitest\/dist\/|\/@vitest\/|vitest-virtual-/
// Registries their owners create on first use and look up again on every access.
const LAZY_GLOBAL_REGISTRY = /^(?:Symbol\(orca\.|__zod_global)/
const PROCESS_KEYS = ['platform', 'arch', 'execPath', 'argv', 'exitCode']
const PROCESS_KEYS = ['platform', 'arch', 'execPath', 'argv', 'exitCode', 'env']
function resetNode(node) {
node.evaluated = false
@@ -180,7 +180,9 @@ function restoreProcessState() {
return leaks
}
afterAll(() => {
afterAll(async () => {
// Finish background imports while their file's environment and module promises still exist.
await vi.dynamicImportSettled()
// Runs Vitest's end-of-file spy restore now: a spy restored by hand would otherwise be re-applied after this hook.
vi.restoreAllMocks()
vi.useRealTimers()
@@ -0,0 +1,86 @@
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import { join, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { runProcess, runProcessSync } from '@orca/process-host'
import { expect, it } from 'vitest'
import { resolvePinnedBunExecutable } from '../../tool-invocation/pinned-bun-executable.mjs'
const root = fileURLToPath(new URL('../../../..', import.meta.url))
it('restores environment stubs and finishes background imports before the next shared file', async () => {
const cache = join(root, 'node_modules/.cache')
mkdirSync(cache, { recursive: true })
const directory = mkdtempSync(join(cache, 'shared-worker-reset-'))
const write = (name, source) => writeFileSync(join(directory, name), source)
try {
write(
'vitest.config.mjs',
`import { defineConfig } from 'vitest/config'
import { BaseSequencer } from 'vitest/node'
class Ordered extends BaseSequencer {
async sort(specs) { return specs.sort((a, b) => a.moduleId.localeCompare(b.moduleId)) }
}
export default defineConfig({ test: {
root: ${JSON.stringify(directory)},
include: ['*.spec.mjs'], isolate: false, maxWorkers: 1,
setupFiles: ['./assert-import-finished.mjs', ${JSON.stringify(resolve(root, 'config/scripts/test-runtime/environment/vitest-shared-worker-file-reset.mjs'))}],
sequence: { sequencer: Ordered }
} })`
)
write(
'assert-import-finished.mjs',
`import { afterAll, expect } from 'vitest'
import { readFileSync } from 'node:fs'
afterAll(() => expect(readFileSync(new URL('./result', import.meta.url), 'utf8')).toBe('loaded'))`
)
write(
'import-started.mjs',
`export let started
export const ready = new Promise(resolve => { started = resolve })`
)
write(
'background.mjs',
`import { setImmediate } from 'node:timers/promises'
import { writeFileSync } from 'node:fs'
import { started } from './import-started.mjs'
started()
await setImmediate()
writeFileSync(new URL('./result', import.meta.url), 'loaded')`
)
write(
'01-replace-env.spec.mjs',
`import { it } from 'vitest'
import { ready } from './import-started.mjs'
it('replaces env and starts a background import', async () => {
process.env = { ...process.env }
void import('./background.mjs')
await ready
})`
)
write(
'02-stub-env.spec.mjs',
`import { expect, it, vi } from 'vitest'
it('removes an environment override after a previous file replaced env', () => {
vi.stubEnv('ORCA_SHARED_WORKER_OVERRIDE', 'asia-east2')
vi.unstubAllEnvs()
expect(process.env.ORCA_SHARED_WORKER_OVERRIDE).toBeUndefined()
})`
)
const result = await runProcess({
program: resolvePinnedBunExecutable(root, runProcessSync) ?? 'bun',
args: [
'--bun',
resolve(root, 'node_modules/vitest/vitest.mjs'),
'run',
'--config',
join(directory, 'vitest.config.mjs')
],
cwd: root,
env: { ...process.env, ORCA_BACKGROUND_LAUNCH: '1' },
timeoutMs: 30_000
})
expect(result.code, result.stdout + result.stderr).toBe(0)
} finally {
rmSync(directory, { recursive: true, force: true })
}
})