I'm having a problem configuring Jest to run my tests. When running npm test -- --watchAll --no-cache
I get the message
babel.config.js: Error while loading config - You appear to be using a native ECMAScript module configuration file, which is only supported when running Babel asynchronously.
I've already checked other issues here on the OS, notably this one, but when I change the extension from babel.config to .cjs
, the following message appears
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Details:
SyntaxError: <project_root>/src/filmes/gerenciador.js: Unexpected reserved word 'await'. (13:24)
Below is my configuration files
package.json
{
"name": "myfilms",
"version": "1.0.0",
"type": "module",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.15.0",
"@babel/preset-env": "^7.15.0",
"babel-jest": "^27.0.6",
"jest": "^27.0.6",
"json-server": "^0.16.3"
},
"dependencies": {
"axios": "^0.21.1",
}
}
babel.config.js
module.exports = {
presets: [
[
"@babel/preset-env",
{
targets: {
node: "current",
},
},
],
],
};
Jest's and Babel's documentation is pretty confusing about these cases, and each of the solutions I try alternates between these two types of errors. Is there any standard way to configure Jest to support imports/exports
and await/async
?