Skip to content

@angular/build:unit-test: "vitest-mock-patch" re-patches for each test file run in same vitest worker context causing vi.mock to fail #32871

@m-yst-ery

Description

@m-yst-ery

Command

test

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

I encountered this issue while trying to mock a statically imported npm package using vi.mock

Apparently vitest does not clear a workers context between test file executions. Because of that, the patch applied by the Angular builder ("vitest-mock-patch") is applied multiple times on the already patched functions for each test file a worker runs.

This causes calls to vi.mock to fail with TypeError: Cannot read properties of undefined (reading 'trim') on every but the first file run by a worker.

Through debugging I could trace it back to the fact that vitest uses stack trace information when enqueuing mocks. Due to the nested patches the stack trace is altered in a way that makes this logic break due to having additional stack entries containing " at Object.mock "

The exact point of failure is in the getImporter function call in vi.mock (https://github.com/vitest-dev/vitest/blob/main/packages/vitest/src/integrations/vi.ts#L852)
Due to the additional stack entries, importerStackIndex resolves to the last entry in stackArray and subsequently parseSingleStack recieves undefined instead of a string (parseSingleStack then calls .trim() on that value, causing the error)

Minimal Reproduction

Repository with minimal repro: https://github.com/m-yst-ery/angular-cli-vi-mock-repro

On ng run app:test the first spec file the worker decides to run will complete with no issue. Then the second file will encounter the error described above.

Notes:

Normally with only two test files they would of course run in seperate workers. To force them to instead run in the same worker context i have set maxWorkers to 1 in the vitest config.

I have set this up to have the vi.mock call in a vitest setup file because this is the scenario in the project where I encountered this issue (mock the import for all tests in the project).
However the same issue would also occur if only the second file run by the worker contains the vi.mock call.

Exception or Error


Your Environment

_                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/


Angular CLI       : 21.2.5
Angular           : 21.2.6
Node.js           : 22.22.1
Package Manager   : npm 10.9.4
Operating System  : win32 x64

┌───────────────────────────┬───────────────────┬───────────────────┐
│ Package                   │ Installed Version │ Requested Version │
├───────────────────────────┼───────────────────┼───────────────────┤
│ @angular/build            │ 21.2.5            │ ^21.2.5           │
│ @angular/cli              │ 21.2.5            │ ^21.2.1           │
│ @angular/common           │ 21.2.6            │ ^21.2.0           │
│ @angular/compiler         │ 21.2.6            │ ^21.2.0           │
│ @angular/compiler-cli     │ 21.2.6            │ ^21.2.0           │
│ @angular/core             │ 21.2.6            │ ^21.2.0           │
│ @angular/forms            │ 21.2.6            │ ^21.2.0           │
│ @angular/platform-browser │ 21.2.6            │ ^21.2.0           │
│ @angular/router           │ 21.2.6            │ ^21.2.0           │
│ ng-packagr                │ 21.2.1            │ ^21.2.0           │
│ rxjs                      │ 7.8.2             │ ~7.8.0            │
│ typescript                │ 5.9.3             │ ~5.9.2            │
│ vitest                    │ 4.1.2             │ ^4.0.8            │
└───────────────────────────┴───────────────────┴───────────────────┘

Anything else relevant?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions