4

尝试运行测试时,Jest 会抛出遇到的意外令牌错误。我正在使用 ky http,这显然会导致问题并且没有被编译。

 Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    Z:\sepview.web\sepview-react\node_modules\ky\index.js:277
    export default createInstance();
    ^^^^^^

    SyntaxError: Unexpected token export

      1 | import * as React from 'react';
    > 2 | import ky from 'ky';
        | ^
      3 |
      4 | class HttpService extends React.Component {
      5 |

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
      at Object.<anonymous> (src/components/HttpService.tsx:2:1)

Test Suites: 1 failed, 1 total

这是我的 package.json

{
  "name": "test",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@fortawesome/fontawesome-svg-core": "^1.2.7",
    "@fortawesome/free-solid-svg-icons": "^5.4.2",
    "@fortawesome/react-fontawesome": "^0.1.3",
    "@types/d3": "^5.0.1",
    "bootstrap": "^4.1.3",
    "bootswatch": "^4.1.3",
    "d3": "^5.7.0",
    "d3-array": "^2.0.2",
    "jquery": "^3.3.1",
    "ky": "^0.5.1",
    "moment": "^2.22.2",
    "node-sass": "^4.9.4",
    "popper.js": "^1.14.4",
    "react": "^16.6.0",
    "react-dom": "^16.6.0",
    "react-router": "^4.3.1",
    "react-router-dom": "^4.3.1",
    "react-scripts": "2.1.1",
    "typescript": "^3.1.6"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "devDependencies": {
    "@types/enzyme": "^3.1.14",
    "@types/enzyme-adapter-react-16": "^1.0.3",
    "@types/jest": "^23.3.9",
    "@types/node": "^10.12.2",
    "@types/react": "^16.4.18",
    "@types/react-dom": "^16.0.9",
    "@types/react-router": "^4.4.0",
    "@types/react-router-dom": "^4.3.1",
    "enzyme": "^3.7.0",
    "enzyme-adapter-react-16": "^1.6.0"
  },
  "proxy": "http://localhost:43947"
}

像这样将 transform 或 transformIgnorePatterns 添加到 package.json 时:

"jest": {
        "transform": {
          "^.+\\.js$": "babel-jest"
      },
        "transformIgnorePatterns": [
          "/node_modules/(?!(@ky)/).*/"
        ]
      },

我收到此错误:

Out of the box, Create React App only supports overriding these Jest options:

  • collectCoverageFrom
  • coverageReporters
  • coverageThreshold
  • globalSetup
  • globalTeardown
  • resetMocks
  • resetModules
  • snapshotSerializers
  • watchPathIgnorePatterns.

These options in your package.json Jest configuration are not currently supported by Create React App:

  • transform
  • transformIgnorePatterns

If you wish to override other Jest options, you need to eject from the default setup. You can do so by running npm run eject but remember that this is a one-way operation. You may also file an issue with Create React App to discuss supporting more options out of the box.

我还有一个空 atm 的 babel.config.js:

module.exports = function () {
    const presets = [];
    const plugins = [];

    return {
      presets,
      plugins
    };
  }

弹出不是一种选择。我尝试解决这个问题 + 一周。非常感谢任何帮助。

4

2 回答 2

3

material-ui-next-pickers我在 dist/ 中使用 es6 导入时遇到了类似的问题

这是因为material-ui-next-pickers在 Jest 中运行之前没有被转译。create-react-app排除一切./node_modules

我们可以通过更改来解决此问题,但如问题中所述,transformIgnorePatterns它不受支持。create-react-app

但是,我们仍然可以将它作为命令 arg 传递给react-script test --transformIgnorePatterns \"node_modules/(?!my-module)/\"

例子

  "scripts": {
    "test": "react-scripts test --transformIgnorePatterns \"node_modules/(?!material-ui-next-pickers)/\" --env=jsdom",
     ...

  }
于 2019-02-20T20:20:08.643 回答
0

添加

moduleNameMapper: {
  '^ky$': require.resolve('ky').replace('index.js', 'umd.js'),
},

给你的jest.config.js

于 2020-07-08T08:28:13.373 回答