Skip to content

vue-router is shared between tests #1681

Closed
@Mikilll94

Description

@Mikilll94

Subject of the issue

I have the following test:

import { createLocalVue, mount } from '@vue/test-utils'
import HelloWorld from '@/components/HelloWorld.vue'
import VueRouter, { RouteConfig } from 'vue-router'

const routes: RouteConfig[] = [
  {
    path: '/',
    name: 'Home',
  },
  {
    path: '/about',
    name: 'About',
  }
];

describe('HelloWorld.vue', () => {
  it('test one', () => {
    const localVue = createLocalVue()
    localVue.use(VueRouter)
    const router = new VueRouter({routes})

    const msg = 'new message'
    const wrapper = mount(HelloWorld, {
      localVue,
      router,
      propsData: { msg }
    })

    console.log("TEST ONE", wrapper.vm.$route.path);
    wrapper.vm.$router.replace("/whatever");
    console.log("TEST ONE", wrapper.vm.$route.path);

    expect(wrapper.text()).toMatch(msg)
  })

  it('test two', () => {
    const localVue = createLocalVue()
    localVue.use(VueRouter)
    const router = new VueRouter({routes})

    const msg = 'new message'
    const wrapper = mount(HelloWorld, {
      localVue,
      router,
      propsData: { msg }
    })

    console.log("TEST TWO", wrapper.vm.$route.path);

    expect(wrapper.text()).toMatch(msg)
  })
})

If you run it, you will see that it prints this to the console:

 PASS  tests/unit/example.spec.ts
  HelloWorld.vue
    √ test one (76ms)
    √ test two (11ms)

  console.log tests/unit/example.spec.ts:29
    TEST ONE /

  console.log tests/unit/example.spec.ts:31
    TEST ONE /whatever

  console.log tests/unit/example.spec.ts:48
    TEST TWO /whatever

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        5.646s, estimated 7s
Ran all test suites.

This means that the router instance is shared between tests, even though I am using separate localVue and router for both tests.

IMO tests should be independent -> they should have their own instance of vue-router. Is it possible with library? If you follow the examples in the docs, then you will end up with shared vue-router instance between tests.

Steps to reproduce

Just run unit tests in this project (npm run test:unit)

testing-vue-router.zip

Expected behaviour

All tests should have their own instance of vue-router

Actual behaviour

vue-router instance is shared between tests

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions