根本配置网上都有,这里不再详述,说一下踩过的坑

  1. 目前jest只能用26+的版本,不能用最新的27+,我一开始间接用的npm install jest --save-dev装置,各种配置配好后运行报了一个莫名其妙的谬误Cannot destructure property 'config' of 'undefined',查看源码发现是vue-jestgetCacheKey的第4个参数解构失败报错,再搜寻了一下发现是jest调用vue-jestgetCacheKey办法的时候只传了3个参数,由此判断是jest版本有问题,再看了一下element-plus的配置发现用的是jest26版本,豁然开朗,马上换26版本运行,发现还是报错不过是另一个谬误了。钻研了下,应该是jest配套的包的版本问题,于是全副换成26+运行,胜利。上面是我胜利运行的的package.jsonjest相干的包配置:

     "@types/jest": "^26.0.23", "babel-jest": "^26.3.0", "jest": "^26.6.3", "ts-jest": "^26.0.0",
  2. 运行胜利后,我本人写的一个dialog组件测试用例执行失败,谬误:Cannot call text on an empty DOMWrapper,具体代码:

    const TESTSTR = 'risk everywhere risk everywhere risk everywhere';describe('Dialog vue',() => {  test('dialog should have content when content has been given', async () => { const wrapper = mount(Dialog,{   props:{     content: TESTSTR,     modelValue: true   } }); await nextTick(); expect(wrapper.find('.modal-body').text()).toEqual(TESTSTR);  });});

    其它组件测试没报错,就这个dialog组件报错,通过了一番思考终于找到起因:

    <template> <teleport to="body"> </teleport></template>

    teleport的问题,元素都搬走了,DOMWrapper必定变成empty了,应该要加一个配置属性能让teleport生效

    <template> <teleport to="body" :disabled="!appendToBody"> </teleport></template>

    OK,功败垂成