未转译节点模块时发生的玩笑错误

时间:2018-11-30 09:47:30

标签: reactjs jestjs babel create-react-app unexpected-token

尝试运行测试时,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"
}

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

"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.

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

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

    return {
      presets,
      plugins
    };
  }

不能弹出。我尝试解决这个问题一周以上。非常感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

我使用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",
     ...

  }

答案 1 :(得分:0)

添加

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

到您的jest.config.js