spectator icon indicating copy to clipboard operation
spectator copied to clipboard

Test failure when using mock ActivatedRoute and spy on Router navigate

Open loriepisicchio opened this issue 4 years ago • 1 comments

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request
[ ] Other... Please describe:

Current behavior

If you run the tests in the following test repo : https://github.com/loriepisicchio/activated-route-test-issue you will have test failures. If you comment out this line : https://github.com/loriepisicchio/activated-route-test-issue/blob/main/src/app/app.component.spec.ts#L56, it works fine

Expected behavior

There seems to be some kind of overlap between mocking ActivatedRoute and spying on Route methods.

Minimal reproduction of the problem with instructions

For bug reports please provide the STEPS TO REPRODUCE and if possible a MINIMAL DEMO of the problem via https://stackblitz.com or similar (you can use this template as a starting point: https://bit.ly/2zme3bj).

  • Clone the following repo : https://github.com/loriepisicchio/activated-route-test-issue you will have test failures.
  • Run npm test :arrow_right: Tests will fail
  • Comment out https://github.com/loriepisicchio/activated-route-test-issue/blob/main/src/app/app.component.spec.ts#L56
  • Run npm test again :arrow_right: Tests will pass

What is the motivation / use case for changing the behavior?

I need to redirect the user on a different page in my app depending on the activated route. I would like to be able to test this by spying on router.navigate and check that it has been called with the right value.

Environment


Angular version: 10.1.2
Jest: 26.6.3
Spectator: 6.1.0,



Browser:
- [ ] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [x] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Node version: v14.10.1
- Platform:  Ubuntu 20.10

Others:

loriepisicchio avatar Nov 09 '20 16:11 loriepisicchio

Not sure if this will help, but you can use https://github.com/ngneat/spectator#integration-testing-with-routertestingmodule

krzyhan avatar Jan 19 '21 11:01 krzyhan