I'm doing just simple unit test with vue test utils. but It's not working. I've no clue.... help me
I installed this things.....
> $ 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
I made jest.config.js file
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'
}
}
and tests/Parent.test.js
import { mount } from '@vue/test-utils'
import Parent from './Parent.vue'
test('Mount', () => {
const wrapper = mount(Parent)
expect(wrapper.html()).toBe('')
})
but npm run test:unit error like this
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.
The package is moving away from semantic versioning to support a variety of Vue versions (2.x, 3.x) and Jest versions (26.x, 27.x). So, if you're using Vue 3.x and Jest 27.x, you need to install vue3-jest, not vue-jest.
npm i vue3-jest -D
# or
yarn add vue3-jest --dev
Then, update your jest.config.js
as follows:
module.exports = {
moduleFileExtensions: ['vue', 'js', 'json', 'jsx'],
testEnvironment: 'jsdom',
transform: {
'^.+\\.vue$': 'vue3-jest',
'^.+\\js$': 'babel-jest',
},
}
By the time of writing this answer, the package naming & versioning was like so:
You can read more about the new naming & versioning strategy here.
If you're using version 27 of jest, try downgrading to version 26. Make sure to downgrade babel-jest and ts-jest as well to version 26. I was getting the following error and that did it for me:
Cannot destructure property 'config' of 'undefined' as it is undefined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With