Skip to content

mocks only works for top component #1707

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

Closed
blake-newman opened this issue Oct 5, 2020 · 2 comments
Closed

mocks only works for top component #1707

blake-newman opened this issue Oct 5, 2020 · 2 comments

Comments

@blake-newman
Copy link
Member

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

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')
})

@lmiller1990
Copy link
Member

Pls lmk if my repro is wrong - this seems fine to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants