[Vuejs]-Getting access to varaibles when testing Vue with Jest

0👍

It looks like you’re using the component options definition instead of the component instance in your tests.

You should be creating a wrapper by mounting the component, and then you could access the component method via wrapper.vm:

import { shallowMount } from '@vue/test-utils'
import FileToTest from '@/components/FileToTest.vue'

describe('FileToTest', () => {
  it('exampleOfFunction returns false by default', () => {
    const wrapper = shallowMount(FileToTest)
    expect(wrapper.vm.exampleOfFunction()).toBe(false)
  })

  it('exampleOfFunction returns true when data is not 100', () => {
    const wrapper = shallowMount(FileToTest)
    wrapper.setData({ exampleOfData2: 0 })
    expect(wrapper.vm.exampleOfFunction()).toBe(true)
  })
})

Leave a comment