angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

`unit-test` builder with `vitest` runner does not support `vi.mock()`

Open yjaaidi opened this issue 2 months ago • 1 comments

Command

test

Description

When using the new unit-test builder with Vitest runner, some Vitest features such as the module graph or module "mocking" with vi.mock() (https://vitest.dev/guide/mocking/modules.html) do not work.

This is due to the fact that the builder is building the tests and then providing the built chunks to Vitest so Vitest is not aware of the real module graph.

Luckily, Angular developers have the luxury of dependency injection that avoids relying on test framework for replacing things with test doubles. That said, there are some use cases, specially when relying on 3rd party libraries that might require module "mocking". For instance, AFAIK, the main approach to achieve Shallow testing in AOT without altering production code is to use this feature (cf. https://marmicode.io/blog/angular-template-code-coverage-and-future-proof-testing#:~:text=Shallow%20Testing%20is%20More%20Challenging)

Describe the solution you'd like

Let vitest resolve modules 😇

Describe alternatives you've considered

In the mean time, it would be nice to monkey patch vi.mock and make it throw an error indicating that the feature is not supported.

yjaaidi avatar Oct 27 '25 13:10 yjaaidi

This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list.

You can find more details about the feature request process in our documentation.

angular-robot[bot] avatar Oct 31 '25 17:10 angular-robot[bot]

Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.

Find more details about Angular's feature request process in our documentation.

angular-robot[bot] avatar Dec 10 '25 13:12 angular-robot[bot]