Skip to content

Vitest module mocking doesn't work in Angular 21 #32212

Closed as duplicate
Closed as duplicate

Description

@Kludgy4

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

No

Description

Module mocking doesn't appear to work with vitest in ng21. I have used vi.mock and vi.spyOn and neither works. This did work however with jest (CommonJS?) and jest.mock.

Please provide a link to a minimal reproduction of the bug

Clone this and run ng test
https://github.com/Kludgy4/ng21-broken-module-mock-repro

See broken tests here
https://github.com/Kludgy4/ng21-broken-module-mock-repro/blob/main/src/app/app.spec.ts#L4-L26

Please provide the exception or error you saw

TypeError: [Function downloadDocument] is not a spy or a call to a spy!

or with spyOn

TypeError: Cannot redefine property: downloadDocument

Please provide the environment you discovered this bug in (run ng version)

Angular CLI       : 21.0.4
Angular           : 21.0.6
Node.js           : 20.19.2
Package Manager   : npm 10.8.2
Operating System  : linux x64

┌───────────────────────────┬───────────────────┬───────────────────┐
│ Package                   │ Installed Version │ Requested Version │
├───────────────────────────┼───────────────────┼───────────────────┤
│ @angular/build            │ 21.0.4            │ ^21.0.4           │
│ @angular/cli              │ 21.0.4            │ ^21.0.4           │
│ @angular/common           │ 21.0.6            │ ^21.0.0           │
│ @angular/compiler         │ 21.0.6            │ ^21.0.0           │
│ @angular/compiler-cli     │ 21.0.6            │ ^21.0.0           │
│ @angular/core             │ 21.0.6            │ ^21.0.0           │
│ @angular/forms            │ 21.0.6            │ ^21.0.0           │
│ @angular/platform-browser │ 21.0.6            │ ^21.0.0           │
│ @angular/router           │ 21.0.6            │ ^21.0.0           │
│ rxjs                      │ 7.8.2             │ ~7.8.0            │
│ typescript                │ 5.9.3             │ ~5.9.2            │
│ vitest                    │ 4.0.16            │ ^4.0.8            │
└───────────────────────────┴───────────────────┴───────────────────┘

Anything else?

No response

Activity

  1. transferred this issue fromangular/angularon Dec 31, 2025
  2. angular-automatic-lock-bot commented on Jan 31, 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.

  3. locked and limited conversation to collaborators on Jan 31, 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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions