1

我的测试

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 标签,但我已经对其进行了初始化。所以我不知道出了什么问题。有人可以帮助我吗?

4

2 回答 2

0

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

global.document.createRange = () => ({
        setStart: () => {},
        setEnd: () => {},
        commonAncestorContainer: {
            nodeName: 'BODY',
            ownerDocument: document
        },
        createContextualFragment: jest.fn
    });
于 2020-09-04T18:59:03.907 回答
0
document.createRange = () => ({
    setStart: () => {},
    setEnd: () => {},
    //@ts-ignore
    commonAncestorContainer: {
      nodeName: "BODY",
      ownerDocument: document,
    },
  })
于 2020-05-09T09:27:57.190 回答