简体   繁体   English

Jest(React) 语法错误,jest coomand 运行后的配置问题

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

I have use these packages in the package.json file我在 package.json 文件中使用了这些包

"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 Setup.tests.js 文件位于包含内容的根文件夹中

** **

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当我运行jest --runInBand --ci --verbose true我得到了

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您将在文档中找到这些配置选项的更多详细信息和示例: https ://jestjs.io/docs/configuration 有关自定义转换的信息,请参阅: 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)

I found the issue, I have create .babelrc file in the root of folder and add我发现了问题,我在文件夹的根目录中创建了 .babelrc 文件并添加

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM