The Wayback Machine - https://web.archive.org/web/20201212162941/https://github.com/vuejs/vue-test-utils/issues/1707
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

`mocks` only works for top component #1707

Open
blake-newman opened this issue Oct 5, 2020 · 1 comment
Open

`mocks` only works for top component #1707

blake-newman opened this issue Oct 5, 2020 · 1 comment

Comments

@blake-newman
Copy link
Member

@blake-newman blake-newman commented Oct 5, 2020

I never realized mocks was only for the component in question - I thought it would be available to all the children, too 🤔

It sounds like this should be the default behavior.

Originally posted by @lmiller1990 in #1701 (comment)


The mocks object is only set for the first component; not it's children. This behaviour means that properties such as $route / $router are not easily mockable via integration style testing.

There is ways around this, by using localVue and creating a router instance and setting on the mount options.

@lmiller1990
Copy link
Member

@lmiller1990 lmiller1990 commented Oct 8, 2020

I was not able to reproduce this (or maybe I misunderstood?) eg, I could mock $store.state in a child component like this:

import { mount, config } from '@vue/test-utils'

const Foo = {
  template: `
    <div>
      Count: {{ $store.state.count }}
    </div>
  `
}

const App = {
  components: { Foo },
  template: `
    <div>
      <Foo /> 
    </div>
  `
}

const $store = {
  state: {
    count: 1
  }
}

test('with vuex', () => {
  const wrapper = mount(App, {
    mocks: {
      $store
    }
  })

  expect(wrapper.html()).toContain('Count: 1')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.