开玩笑测试-TypeError:document.createRange不是函数

时间:2020-05-09 03:22:01

标签: typescript unit-testing vue.js jestjs iview

我的测试

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警告]:nextTick中出现错误:“ TypeError:document.createRange不是函数”

found in

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

作为iView标签,但是我已经对其进行了初始化。 所以我不知道出了什么问题。有人可以帮我吗?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

str_squish(format(date_time, "%e/%m/%Y %H:%M:%S"))