question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

router.push could be a simple mock functions by default?

See original GitHub issue

What 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:closed
  • Created 3 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
cexbrayatcommented, Jan 27, 2021

I followed your idea and opened #53

I tried a patched version in our projects, and it does simplify our tests quite a bit 👍

0reactions
posvacommented, Jan 7, 2021

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found