Jest Test-TypeError:document.createRange不是函数



我的测试

import VueI18n from 'vue-i18n'
import Vuex from "vuex"
import iView from 'view-design'
import {mount,createLocalVue} from '@vue/test-utils'
// @ts-ignore
import FormAccountName from '@/views/forms/FormAccountName/FormAccountName'
const localVue = createLocalVue()
localVue.use(Vuex)
localVue.use(iView)
localVue.use(VueI18n)
describe('a',()=>{
test('b',async ()=>{
const wrapper = mount(FormAccountName,{
localVue,
mocks: {
$t: () => 'this is a label',
formItems: {
name: '<a>'
}
},
})
expect(wrapper).toMatchSnapshot()
})
})

错误

快照正常生成,但出现错误

[Vue warn]:下一次点击时出错:"TypeError:document.createRange不是函数">

found in
---> <Tooltip>
<ErrorTooltipTs>
<ValidationProvider>
<FormRow>
<ValidationObserver>
<FormWrapper>
<FormAccountNameUpdateTs>
<Root>

作为iView标记,但我必须初始化它。所以我不知道出了什么问题。有人能帮我吗?

document.createRange = () => ({
setStart: () => {},
setEnd: () => {},
//@ts-ignore
commonAncestorContainer: {
nodeName: "BODY",
ownerDocument: document,
},
})

在pollyfill.js或testSetup 中添加以下配置模拟代码

global.document.createRange = () => ({
setStart: () => {},
setEnd: () => {},
commonAncestorContainer: {
nodeName: 'BODY',
ownerDocument: document
},
createContextualFragment: jest.fn
});

最新更新