Skip to content

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

@yjaaidi

Description

@yjaaidi

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions