Problem with mocking of RouterModule (Angular 14 + Jest)
See original GitHub issueIn the process of updating my project to angular 14, in which I am using Jest and Shallow-render for unit tests, I noticed a problem with mocking of RouterModule. Unit tests failed with the following error message.
AppComponent
✕ should match snapshot (11 ms)
● AppComponent › should match snapshot
MockOfRouterOutlet does not have a module def (ɵmod property)
11 |
12 | it('should match snapshot', async () => {
> 13 | const { fixture } = await shallow.render();
| ^
14 |
15 | expect(fixture).toMatchSnapshot();
16 | });
at transitiveScopesFor (node_modules/@angular/core/fesm2020/core.mjs:24497:11)
at Array.forEach (<anonymous>)
at Array.forEach (<anonymous>)
at Array.forEach (<anonymous>)
at src/app/app.component.spec.ts:13:39
at src/app/app.component.spec.ts:12:42
● AppComponent › should match snapshot
MockOfRouterOutlet does not have a module def (ɵmod property)
at transitiveScopesFor (node_modules/@angular/core/fesm2020/core.mjs:24497:11)
at Array.forEach (<anonymous>)
at Array.forEach (<anonymous>)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 4.101 s, estimated 13 s
Minimal reproduction of the problem
You can reproduce the issue by running ng test
for the project in my repo where you find a simple Angular 14 project with Jest 28 (added with @angular-builders/jest) and Shallow-render 14.
npm i
ng test
Issue Analytics
- State:
- Created a year ago
- Reactions:10
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Problem with mocking of RouterModule (Angular 14 + Jest)
In the process of updating my project to angular 14, in which I am using Jest and Shallow-render for unit tests, I noticed...
Read more >How can I unit test a component that uses the Router in ...
Here we are just mocking the router. Since we are just unit testing, we may not want the real routing facility. We just...
Read more >Angular mocking troubleshooting FAQ
Frequently Asked Questions about Angular mocks troubleshooting.
Read more >Issues · getsaf/shallow-render - GitHub
Angular testing made easy with shallow rendering and easy mocking. ... Problem with mocking of RouterModule (Angular 14 + Jest).
Read more >RouterTestingModule - Angular
The modules sets up the router to be used for testing. It provides spy implementations of Location and LocationStrategy . Further information is...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I had the same issue and saw in the example test that they used RouterTestingModule instead of RouterModule. Adding a global replacement fixed it for me:
Shallow.alwaysReplaceModule(RouterModule, RouterTestingModule);
For us it didn’t work to provide a mock for
Router
usingshallow.provide({ provide: Router, useValue: mockRouter })
as suggested by @floisloading. Because when addingdontMock(RouterModule)
e.g. theRouterLinkWithHref
directive seemed to need some of the other methods onRouter
(TypeError: Cannot read properties of undefined (reading 'subscribe') at new RouterLinkWithHref
). If we don’t adddontMock(RouterModule)
we get errors like described above:MockOfRouterOutlet does not have a module def (ɵmod property)
. Similar problems for usingreplaceModule(RouterModule, RouterTestingModule)
.But we could fix the problem by only mocking a single method on the
Router
like this: