Skip to content

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

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.

Activity

  1. changed the title [-]`unit-test` builder does not support `vi.mock()`[/-] [+]`unit-test` builder with `vitest` runner does not support `vi.mock()`[/+] on Oct 27, 2025
  2. added
    featureLabel used to distinguish feature request from other issues
    on Oct 31, 2025
  3. angular-robot commented on Oct 31, 2025

    @angular-robot
    Contributor

    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.

  4. angular-robot commented on Dec 10, 2025

    @angular-robot
    Contributor

    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.

  5. angular-robot commented on Dec 29, 2025

    @angular-robot
    Contributor

    Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage.

    We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.

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

  6. added
    feature: insufficient votesLabel to add when the not a sufficient number of votes or comments from unique authors
    and removed
    feature: votes requiredFeature request which is currently still in the voting phase
    on Dec 29, 2025
  7. Kludgy4 commented on Dec 31, 2025

    @Kludgy4

    Does an issue get reopened if it gets 20 votes after the 20 day polling process? We seems to have missed the deadline by less than 2 days

  8. added a commit that references this issue on Feb 9, 2026
    fd5cb28
  9. angular-automatic-lock-bot commented on Mar 12, 2026

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  10. locked and limited conversation to collaborators on Mar 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions