I'd like to write a Jest test for a Vue component, which renders a subcomponent. The component looks like this:
<template>
<div class="add-to-cart-position">
<a :href="item.url">
<picture-comp
class="add-to-cart-position__image"
:srcset-mobile="item.imageUrlMobile"
:srcset-desktop="item.imageUrl"
/>
</a>
</div>
</template>
<script lang="ts">
import {Vue, Component, Prop} from 'vue-property-decorator';
import BasketItem from '../interfaces/BasketItem';
import PictureComp from '../pictureComp/pictureComp.vue';
@Component({
name: 'add-to-cart-position',
components: {
PictureComp
}
})
export default class AddToCartPosition extends Vue {
@Prop()
item: BasketItem;
}
</script>
According to the docs I can use shallowMount
to automatically stub all subcomponents, which is exactly what I want. So my test looks like this:
import {shallowMount} from '@vue/test-utils';
import AddToCartPosition from 'AddToCartPosition.vue';
import {mockBasketItem} from '/__mocks__/data/basketItem.mock';
describe('addToCartPosition', () => {
const wrapper = shallowMount(AddToCartPosition, {
propsData: {
basketItem: mockBasketItem()
}
});
it('matches the snapshot', () => {
expect(wrapper.html()).toMatchSnapshot();
});
});
But when I run this test I get an error and the error is in the PictureComp
subcomponent, which should not be rendered at all. Why does the automatic stubbing not work?
I also tried mount
and manual stubbing, but it does not change anything. The error message stays the same.
Error message:
Test suite failed to run
TypeError: Object prototype may only be an Object or null: undefined
at setPrototypeOf (<anonymous>)
34 | >
35 | <source
> 36 | :srcset="srcLg"
| ^
37 | media="(min-width: 767px)"
38 | >
39 | <img
at extendStatics (pictureComp/pictureComp.vue:36:12)
at Object.<anonymous>.__extends (pictureComp/pictureComp.vue:40:5)
at pictureComp/pictureComp.vue:62:1
at pictureComp/pictureComp.vue:105:1
at Object.<anonymous> (pictureComp/pictureComp.vue:137:3)
at addToCartPosition/addToCartPosition.vue:70:1
at Object.<anonymous> (addToCartPosition/addToCartPosition.vue:106:3)
at Object.<anonymous> (test/components/addToCartPosition.test.ts:2:1)