Minimal Angular app showing evanw/esbuild#4551: with zone.js in the polyfills, vi.mock fails in any spec that has an async test.
npm ci
npx ng test --watch=false-
src/repro/fails.spec.tsfails:Error: 1 call in "src/repro/fails.spec.ts" was defined outside of the module's top level scope: - vi.mock("date-fns") at src/repro/fails.spec.ts:7:1 -
src/repro/works-with-export.spec.tsis the same spec plusexport {}, and it passes.
zone.jsis in thepolyfillsinangular.json, so@angular/buildlowersasync/awaitto generators. Zone.js can't patch nativeasync/await.- When esbuild lowers a top-level
async () => {}, it records a use of top-levelthis, even though the generated code passesnullinstead. With noexportin the file, esbuild then treats the spec as CommonJS. It wraps the file in__commonJS(...)and adds anexport default. - That wrapper moves
vi.mock(...)out of module top-level scope, and vitest 5 rejects that. Vitest 4 let it through without an error.
Adding export {} makes esbuild treat the file as ESM, so there's no wrapper.
@angular/build22.2.2 (bundles esbuild 0.28.2)- vitest 5.0.3
- zone.js 0.16.3