I'm writing a jest unit test for my Vuejs 2 (Vuetify) component. Setup is pretty simple.
Inside the component I have a prop, which is an object with nested fields:
props: ['testObject']
And I have a computed property that returns a field from that prop:
computed: {
myField() {
return this.testObject.testField;
}
}
And I write a Jest test:
wrapper = mount(MyComponent, {
mocks: {
propsData: {
testObject: { testField: 'My Test Value' }
}
},
});
it("should return a test field", () => {
const field = wrapper.vm.$options.computed.myField();
expect(field).toBe('My Test Value');
});
When I run the test I get the following:
TypeError: Cannot read property 'testField' of undefined
So it calls computed propperty, but testObject is undefined.
Any idea what's going on?
@vue/test-utils: 1.3.0 @vue/vue2-jest: 28.0.1 @types/jest: 28.1.3 jest: 28.1.1