TypeError: Cannot read property 'getters' of undefined

Programmingjoe picture Programmingjoe · Feb 5, 2019 · Viewed 13.6k times · Source

I'm trying to test a basic Vue Component that makes reference to a Vuex store. I thought I followed Vue's example (https://vue-test-utils.vuejs.org/guides/using-with-vuex.html#mocking-getters) to a T but it doesn't appear to be working.

I get the error that is mentioned in the title.

const localVue = createLocalVue()
localVue.use(Vuex)

describe('Navbar.vue', () => {
  let store: any
  let getters: any

  beforeEach(() => {
    getters: {
      isLoggedIn: () => false
    }

    store = new Vuex.Store({
      getters
    })
  })

  it('renders props.title when passed', () => {
    const title = 'Smart Filing'
    const wrapper = shallowMount(Navbar, {
      propsData: { title },
      i18n,
      store,
      localVue,
      stubs: ['router-link']
    })

    expect(wrapper.text()).to.include(title)
  })
})

I'm using class components so maybe that has something to do with it?

@Component({
  props: {
    title: String
  },
  computed: mapGetters(['isLoggedIn'])
})
export default class Navbar extends mixins(Utils) {}

Thanks in advance.

Answer

Jeremi G picture Jeremi G · Feb 5, 2019

The "getters" here is not properly assigned:

  beforeEach(() => {
    getters: {
      isLoggedIn: () => false
    }

    store = new Vuex.Store({
      getters
    })
  })

It should be getters = {... rather than getters: {... because your argument to beforeEach is a function and not an object.

I can confirm that it is indeed correctly written in the documentation.

Good luck!