I am using Vue 3 and Jest. I created a simple code to test as shown below. According to some posts on Stack Overflow, I modified the babel.config.js
and package.json
as shown below.
When I run npm test
I receive the error message posted below. Please let me know how to fix it.
sum.test.js
:
import sum from '../sum.js'
import pkg from '@jest/globals';
const jest = pkg;
import {expect} from '@jest/globals'
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
sum.js
export default function sum(a, b) {
return a + b;
}
Result of npm test
:
npm test
> test@0.1.0 test C:\Users\xxx\xx\xx\xx xx\xxx\0\test
> jest
FAIL unitTests/sum.test.js
● Test suite failed to run
You appear to be using a native ECMAScript module configuration file, which is only supported when running Babel asynchronously.
babel.config.js
:
module.exports = {
presets: [
[
"@babel/preset-env",
{
"targets": {
"node": "current"
}
}
],
'@vue/cli-plugin-babel/preset'
]
}
package.json
:
{
"name": "test",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint",
"test": "jest"
},
"dependencies": {
"@jest/globals": "^29.6.3",
"core-js": "^3.8.3",
"vue": "^3.2.13"
},
"devDependencies": {
"@babel/core": "^7.22.10",
"@babel/eslint-parser": "^7.12.16",
"@babel/preset-env": "^7.22.10",
"@types/jest": "^29.5.3",
"@vue/cli-plugin-babel": "~5.0.0",
"@vue/cli-plugin-eslint": "~5.0.0",
"@vue/cli-service": "~5.0.0",
"babel-jest": "^29.6.3",
"eslint": "^7.32.0",
"eslint-plugin-vue": "^8.0.3",
"jest": "^29.6.3",
"ts-jest": "^29.1.1"
},
"eslintConfig": {
"root": true,
"env": {
"node": true,
"jest": true,
"jest/globals": true
},
"extends": [
"plugin:vue/vue3-essential",
"eslint:recommended"
],
"parserOptions": {
"parser": "@babel/eslint-parser"
},
"rules": {}
},
"type": "module",
"browserslist": [
"> 1%",
"last 2 versions",
"not dead",
"not ie 11"
]
}