router.push could be a simple mock functions by default?
See original GitHub issueWhat problem is this solving
For a component doing a navigation with router.push({ name: 'home' }); on an action, our associated unit test usually does a simple assertion like the following:
const mockRouter = createRouter({ history: createWebHistory(), routes: [] });
jest.spyOn(mockRouter, 'push').mockResolvedValue();
const wrapper = mount(App, { global: { plugins: [mockRouter] } });
// do the action
expect(mockRouter.push).toHaveBeenCalledWith({ name: 'home' });
Using vue-router-mock, we can simplify the test a bit by using createRouterMock:
// do the action
expect(getRouter().push).toHaveBeenCalledWith({ name: 'home' });
but the mock router created tries to really resolve the navigation and throws:
Error: Uncaught [Error: No match for {"name":"home","params":{}}]
Proposed solution
Would it be possible to have a simpler router mock that just have mock functions by default? Most use cases would probably not need a real navigation to be resolved and triggered. Or maybe that could be an option?
Describe alternatives you’ve considered
It’s of course possible to spy on getRouter().push but I feel that this should be the default.
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
Vue test-utils how to test a router.push() - Stack Overflow
Now, thise makes testing easier, because you can pass a fake router to your component, when testing, via the mocks property, just as...
Read more >How to mock Next router with Jest - DEV Community
To demonstrate how to mock next/router I will use an example. The example files and tests are available on github and are build...
Read more >Testing Vue Router
You can use a mocked router to avoid caring about the implementation details of Vue Router in your unit tests. Instead of using...
Read more >Testing Vue 3 Apps — Testing with a Mocked Router - Medium
We can test apps that use the Vue Router with a mock router. ... And we check that the mockRouter.push method goes to...
Read more >Vue Router - Vue Testing Handbook
We will build a simple <App> , that has a /nested-child route. ... routes.js" Vue.use(VueRouter) export default new VueRouter({ routes }).
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 Free
Top 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

I followed your idea and opened #53
I tried a patched version in our projects, and it does simplify our tests quite a bit 👍
It shouldn’t be skipped because it normalizes the route location and also to simulate how the router changes the current route by affecting the whole route also, and this should be respected because it won’t trigger the same watchers, making a test fail while the actual code works