vuetestutilsTypeError:无法解构“未定义”或“空”的属性“config”
我正在使用 vue test utils 进行简单的单元测试。但它不起作用。我不知道....帮帮我
我安装了这些东西.....
> $ npm i -D jest @vue/test-utils vue-jest jest-serializer-vue babel-jest babel-core@bridge
> $ npm i -D @babel/core @babel/preset-env
我做了jest.config.js文件
module.exports = {
moduleFileExtensions: [
'vue',
'js'
],
moduleNameMapper: {
'^~/(.*)$': '<rootDir>/src/$1'
},
modulePathIgnorePatterns: [
'<rootDir>/node_modules',
'<rootDir>/dist'
],
testURL: 'http://localhost',
transform: {
'^.+.vue$' : 'vue-jest',
'^.+.jsx?$' : 'babel-jest'
}
}
和测试/Parent.test.js
import { mount } from '@vue/test-utils'
import Parent from './Parent.vue'
test('Mount', () => {
const wrapper = mount(Parent)
expect(wrapper.html()).toBe('')
})
但是 npm run test:unit 错误是这样的
FAIL tests/Parent.test.js
? Test suite failed to run
TypeError: Cannot destructure property `config` of 'undefined' or 'null'.
at Object.getCacheKey (node_modules/vue-jest/lib/index.js:10:5)
at ScriptTransformer._getCacheKey (node_modules/@jest/transform/build/ScriptTransformer.js:280:41)
at ScriptTransformer._getFileCachePath (node_modules/@jest/transform/build/ScriptTransformer.js:351:27)
at ScriptTransformer.transformSource (node_modules/@jest/transform/build/ScriptTransformer.js:588:32)
at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:758:40)
at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:815:19)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.512 s
Ran all test suites.
在此处输入图片说明
回答
如果您使用的是 jest 的 27 版,请尝试降级到 26 版。确保将 babel-jest 和 ts-jest 也降级到 26 版。我收到以下错误,这对我来说是正确的:
无法解构 'undefined' 的属性 'config',因为它是未定义的。
- 这是一个很好的线索,对我来说,我使用的是 Jest 27,但我也需要将它的伴侣 `babel-jest` 更新为 27,因为它仍然是 24。更新后它开始工作。
THE END
二维码