-2

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"
  ]
}
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • 2
    The title of the question seems to have ***nothing to do*** with what you're asking. You'd get the same error regardless of which numbers you use. Or what test you'd run - if you were testing a `subtract` function or anything else, even non-maths code, the result would be the same. – VLAZ Aug 22 '23 at 05:24
  • @VLAZ would you please tell me why that will happen? – Amrmsmb Aug 22 '23 at 05:25
  • 1
    Because the configuration is wrong for the test. Googling the error message gives me [Error while loading config - You appear to be using a native ECMAScript module configuration file (Jest)](https://stackoverflow.com/q/61146112). Things that *don't* help me figure out what the problem is: the title of the question here. – VLAZ Aug 22 '23 at 05:29
  • @VLAZ google gave me the same resutls. and i applied it as shown above in section `babel.config.js` – Amrmsmb Aug 22 '23 at 05:32

1 Answers1

1

the problem is that babel.config.js is using commonjs and your whole project is using js modules, you can switch the file to babel.config.json and use a json structure

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "current"
        }
      }
    ],
    "@vue/cli-plugin-babel/preset"
  ]
}
Yuu
  • 337
  • 1
  • 4