Jest(React) syntax error, configuration problem after the run of jest coomand

Viewed 23

I have use these packages in the package.json file

"devDependencies": {
    "@babel/core": "^7.18.9",
    "@babel/plugin-transform-runtime": "^7.18.9",
    "@babel/preset-env": "^7.18.9",
    "@babel/preset-react": "^7.18.6",
    "@types/jest": "^28.1.6",
    "@vitejs/plugin-react": "^1.2.0",
    "babel-jest": "^28.1.3",
    "enzyme": "^3.11.0",
    "enzyme-adapter-react-16": "^1.15.6",
    "jest": "^28.1.3",
    "jest-enzyme": "^7.1.2",
    "jest-extended": "^3.0.1",
    "jest-junit": "^14.0.0",
  }

Also have configuration for jest like this

"jest": {
    "testEnvironment": "jsdom",
    "setupFilesAfterEnv": [
      "<rootDir>/setupTests.js",
      "./node_modules/jest-enzyme/lib/index.js",
      "jest-extended"
    ],
    "testResultsProcessor": "jest-junit"
  }

Setup.tests.js file is in the root folder with content

**

import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });

**

When I run jest --runInBand --ci --verbose true I have got

You'll find more details and examples of these config options in the docs: https://jestjs.io/docs/configuration For information about custom transformations, see: https://jestjs.io/docs/code-transformation

Details:

/setupTests.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { configure } from 'enzyme';
                                                                                  ^^^^^^

SyntaxError: Cannot use import statement outside a module

  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1796:14)
1 Answers

I found the issue, I have create .babelrc file in the root of folder and add

{
  "env": {
    "test": {
      "presets": [
        [
          "@babel/preset-env"
        ]
      ]
    }
  }
}
Related