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.

Mocking useRouter and useRoute

See original GitHub issue

currently you can get the router and current route with useRouter and useRoute functions and are not accessible with $router and $route functions. The docs are therefor outdated with Vue 3 (https://vue-test-utils.vuejs.org/v2/guide/vue-router.html#using-a-mock-router). Is there a way to mock the router?

<script lang="ts">
import { defineComponent, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';

export default defineComponent({
  setup() {
    const router = useRouter();
    const route = useRoute();
  },
});

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
Stijn98scommented, Nov 13, 2020

Thanks for the Example!

i am using this for now.

const routerPushMock = jest.fn();

jest.mock('vue-router', () => ({
  useRouter: () => ({
    push: routerPushMock,
  }),
}));

describe('test', () => {
  beforeEach(() => {
    jest.resetAllMocks();
  });

  it('test router pushes, async () => {
    const wrapper = mount(Test);

    expect(routerPushMock).toBeCalledWith({ name: 'dashboard' });
  });
});
1reaction
lmiller1990commented, Nov 12, 2020

I am also using the mock strategy, either making a simple router myself with jest.fn or jest.mock('vue-router', .....). Here is an example: https://github.com/lmiller1990/vuejs-composition-course/blob/412a46d48bf0624ed523567b05424aa16c48d67e/src/NewPost.spec.ts#L8

We are working on a mock router, but it is not ready for usage yet.

Is there any action to be taken for this issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mocking useRouter and useRoute · Issue #242 · vuejs/test-utils
currently you can get the router and current route with useRouter and useRoute functions and are not accessible with $router and $route ...
Read more >
Mocking vue-router's useRoute() in Jest tests in Vue 3
The computed property is using useRoute() , and it is used in the template. When I make a Jest test on this component,...
Read more >
Testing Vue Router
Instead of using a real Vue Router instance, we can create a mock version which ... import { useRouter, useRoute } from 'vue-router'...
Read more >
mocking useRouter (just basics) with react testing library (4/6)
In this episode , I create the navbar & product component while writing 3 tests and :1. creating advanced nav links ,2. displaying...
Read more >
Read more - back - GitHub Pages
The useRouter hook is basically a shortcut for accessing values from the RouterContext . Therefore in order to test a component with useRouter...
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