无法使用 Jest:SyntaxError:Runtime.createScriptFromCode 出现意外标识符

Sta*_*wed 1 reactjs jestjs babel-jest

我刚刚使用 npm 安装了 Jest,现在我正在尝试运行一些测试。事实证明我无法这样做,因为我收到以下错误:

\n\n
\xe2\x97\x8f Test suite failed to run\n\nC:\\Users\\aleja\\Documents\\FINAL PAW POSTA\\paw-2018b-10\\frontend\\src\\tests\\PublicationService.test.js:3\nimport PublicationService from \'../services/PublicationService\'; // import * as StatusCode from \'../util/StatusCode\'\n       ^^^^^^^^^^^^^^^^^^\n\nSyntaxError: Unexpected identifier\n\n  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1059:14)\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是我的 package.json。我读过有关 babel.rc 文件之类的其他问题,但我什至没有这些文件。

\n\n
{\n  "name": "frontend",\n  "version": "0.1.0",\n  "private": true,\n  "dependencies": {\n    "@babel/preset-react": "^7.7.4",\n    "@material-ui/core": "^4.8.2",\n    "@material-ui/icons": "^4.5.0",\n    "@material/react-linear-progress": "^0.15.0",\n    "axios": "^0.19.0",\n    "axios-mock-adapter": "^1.17.0",\n    "babel-jest": "^25.1.0",\n    "bootstrap": "^4.4.1",\n    "crypto-js": "^3.1.9-1",\n    "enzyme": "^3.11.0",\n    "formik": "^1.5.8",\n    "i18next": "^17.3.1",\n    "i18next-browser-languagedetector": "^3.1.1",\n    "i18next-xhr-backend": "^3.2.2",\n    "jest": "^25.1.0",\n    "mutex-promise": "^0.1.0",\n    "query-string": "^6.9.0",\n    "react": "^16.12.0",\n    "react-bootstrap": "^1.0.0-beta.16",\n    "react-bootstrap-switch": "^15.5.3",\n    "react-content-loader": "^4.3.4",\n    "react-dom": "^16.12.0",\n    "react-form-validator-core": "^0.6.4",\n    "react-geocode": "^0.2.1",\n    "react-google-maps": "^9.4.5",\n    "react-i18next": "^10.13.2",\n    "react-images-upload": "^1.2.7",\n    "react-images-viewer": "^1.6.2",\n    "react-material-ui-form-validator": "^2.0.9",\n    "react-notifications-component": "^2.2.4",\n    "react-paginate": "^6.3.2",\n    "react-router-dom": "^5.1.2",\n    "react-scripts": "^3.3.0",\n    "react-spring": "^8.0.27",\n    "react-toast-notifications": "^2.4.0",\n    "redux": "^4.0.5",\n    "toasted-notes": "^3.2.0",\n    "webfontloader": "^1.6.28",\n    "yup": "^0.27.0"\n  },\n  "scripts": {\n    "start": "react-scripts start",\n    "build": "react-scripts build",\n    "test": "jest",\n    "eject": "react-scripts eject"\n  },\n  "eslintConfig": {\n    "extends": "react-app"\n  },\n  "browserslist": {\n    "production": [\n      ">0.2%",\n      "not dead",\n      "not op_mini all"\n    ],\n    "development": [\n      "last 1 chrome version",\n      "last 1 firefox version",\n      "last 1 safari version"\n    ]\n  },\n  "devDependencies": {\n    "@babel/cli": "^7.1.2",\n    "@babel/core": "^7.1.2",\n    "@babel/preset-env": "^7.1.0",\n    "@babel/preset-react": "^7.0.0",\n    "@testing-library/jest-dom": "^5.0.2",\n    "@testing-library/react": "^9.4.0",\n    "babel-plugin-transform-es2015-modules-amd": "^6.24.1",\n    "grunt": "^1.0.4",\n    "grunt-babel": "^8.0.0",\n    "grunt-sass": "^3.1.0",\n    "grunt-war": "^0.5.1",\n    "node-sass": "^4.13.1",\n    "react-test-renderer": "^16.12.0",\n    "reactotron-react-js": "^3.3.7",\n    "sass": "^1.24.0"\n  },\n\n  "jest": {\n    "modulePaths": [\n      "node_modules"\n    ],\n    "moduleFileExtensions": [\n      "js",\n      "jsx"\n    ],\n    "moduleDirectories": [\n      "node_modules"\n    ]\n  },\n  "homepage": "http://mypage.com/",\n  "proxy": "http://localhost:8080"\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我添加了一些 babel 依赖项,但问题仍然没有得到解决。

\n\n

我还在我在另一个问题上读到的 package.json 中添加了“Jest”部分。还是没有运气。

\n

Sta*_*wed 7

好吧,看来我必须在根目录中创建一个 babel.config.js 文件,如下所示

module.exports = {
  presets: [
    [
      '@babel/preset-env',
      {
        targets: {
          node: 'current',
        },
      },
    ],
  ],
};
Run Code Online (Sandbox Code Playgroud)