Struggle to properly mock child component with third party dependency in it #2215
-
Hey there, continue to learn unit testing, currently got an issues with testing a component, which have child component + library usage onMount hook. I try both mount / shallowMount, for some reason shallowMount not stubbing child component and i fail to manage make simple wrapper exist test run. Here's the fork: Error i got:
Any advice is highly appreciated |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
If you use shallow mount, then the child component is not used, but the import to your third party lib is still executed, and fails with So you can either patch the window object to add matchMedia in your test (maybe look on the issue tracker of jsdom), or mock the third dependency completely if you use shallowMount: vi.mock('@kolirt/vue-web3-auth', () => {
return {};
});
describe('LoginView.vue', () => {
const wrapper = shallowMount(LoginView);
it('should render properly', () => {
expect(wrapper.exists()).toBeTruthy();
});
}); |
Beta Was this translation helpful? Give feedback.
If you use shallow mount, then the child component is not used, but the import to your third party lib is still executed, and fails with
TypeError: window.matchMedia is not a function
, because jsdom probably does not support this function.So you can either patch the window object to add matchMedia in your test (maybe look on the issue tracker of jsdom), or mock the third dependency completely if you use shallowMount: