document.execCommand is not a function in vue test utils

Viewed 3296

Vue testing with vue-test-utils with Jest, gives errors when exec copy command

document.execCommand is not a function. How can I resolve it?

This is code snippet in component source.

   copyToClipboard(){
     document.execCommand('copy');
    }

And here I had writen test like this;

import Vue from 'vue';
import Vuex from 'vuex';
import {shallowMount} from '@vue/test-utils'

describe('*****.vue', () => {
let wrapper;
beforeAll(()=>{
wrapper = shallowMount(***, {attachToDocument:true, store, localVue});
})

it('should render correct content', () => {
wrapper.vm.copyToClipboard();
);
}
1 Answers

Presumably you're using jsdom for the tests? In which case execCommand is not supported.

Jest doesn't support environments such as headless Chrome, but you may have some success using Puppeteer

Related