Jest 测试 - TypeError:document.createRange 不是函数 [英] Jest Test - TypeError : document.createRange is not a function

查看:46
本文介绍了Jest 测试 - 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()
    })
})

错误

快照正常生成,但是报错

The snapshot was generated normally, but an error was obtained

[Vue 警告]:nextTick 中的错误:TypeError:document.createRange 不是函数"

[Vue warn]: Error in nextTick: "TypeError: document.createRange is not a function"

found in

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

作为 iView 标签,但我已经初始化它.所以我不知道出了什么问题.有人可以帮我吗?

as iView tag ,but i had initialize it. so i don't know what went wrong. could someone help me?

推荐答案

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

Add below config mock code in pollyfill.js or testSetup

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

这篇关于Jest 测试 - TypeError:document.createRange 不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆