【问题标题】:Jest Test - TypeError : document.createRange is not a function开玩笑测试 - TypeError:document.createRange 不是函数
【发布时间】:2020-08-24 17:33:40
【问题描述】:

我的测试

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 is not a function”

found in

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

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

【问题讨论】:

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
    document.createRange = () => ({
        setStart: () => {},
        setEnd: () => {},
        //@ts-ignore
        commonAncestorContainer: {
          nodeName: "BODY",
          ownerDocument: document,
        },
      })
    

    【讨论】:

      猜你喜欢
      • 2017-02-11
      • 2017-10-20
      • 2021-09-13
      • 2021-02-26
      • 2020-02-24
      • 1970-01-01
      • 2019-01-04
      • 2021-01-01
      • 1970-01-01
      相关资源
      最近更新 更多