8

I'm trying to create a basic test for a function in Vue component and failing miserably.

main.ts

new Vue({
  render: (h) => h(App),
  router,
  store,
}).$mount('#app')

App.ts

const App = Vue.extend({
    components: { MainContainer },
    data() {
        return {
            msg: 'Test',
        }
    },
    name: 'App',
    template: `<MainContainer />`,
})

export default App

MainContainer.vue

const MainContainer = Vue.extend({
    components: {
        Content,
    },
    methods: {
        sum: (a: number, b: number): number => a + b,
    },
})

export default MainContainer

MainContainer.spec.ts

import { createLocalVue, shallowMount } from '@vue/test-utils'

describe('MainComponent.vue', () => {
    const localVue = createLocalVue()

    test('sum method should add number together', () => {
    const wrapper = shallowMount(MainContainer, { localVue })
    expect(wrapper.vm.sum(1, 2)).toEqual(3)
})

Tests fails with the error

Property 'sum' does not exist on type 'CombinedVueInstance<Vue, object, object, object, Record<never, any>>'.

The same behavior I see if I try to test data.msg from App.ts. I'd appreciate any help. Thanks

1 Answer 1

15

You must specify type of wrapper.vm

A solution that works is to specify type to any :

expect((wrapper.vm as any).sum(1, 2)).toEqual(3)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.