Skip to content

About

Angular + vitest 5 repro for evanw/esbuild#4551: lowered async arrows make specs CommonJS and break vi.mock

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

esbuild-angular-vitest-mock-repro

Minimal Angular app showing evanw/esbuild#4551: with zone.js in the polyfills, vi.mock fails in any spec that has an async test.

Reproduce

npm ci
npx ng test --watch=false
  • src/repro/fails.spec.ts fails:

    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.ts is the same spec plus export {}, and it passes.

Why

  1. zone.js is in the polyfills in angular.json, so @angular/build lowers async/await to generators. Zone.js can't patch native async/await.
  2. When esbuild lowers a top-level async () => {}, it records a use of top-level this, even though the generated code passes null instead. With no export in the file, esbuild then treats the spec as CommonJS. It wraps the file in __commonJS(...) and adds an export default.
  3. 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.

Versions

  • @angular/build 22.2.2 (bundles esbuild 0.28.2)
  • vitest 5.0.3
  • zone.js 0.16.3

About

Angular + vitest 5 repro for evanw/esbuild#4551: lowered async arrows make specs CommonJS and break vi.mock

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages