来自 create-react-app 的玩笑不在 Windows 上运行

Dim*_*los 7 reactjs jestjs babeljs babel-jest create-react-app

我在从create-react-app在 Windows 8.1 上创建的干净安装的应用程序运行 Jest 时遇到问题。安装后,运行命令npm run test,我收到错误:

No tests found related to files changed since last commit.

Running jest --watchAll,因此绕过 react-scripts,在同一目录中,虽然,显示:

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:

SyntaxError: E:\demo\src\App.test.js: Unexpected token (7:18)

  5 | it('renders without crashing', () => {
  6 |   const div = document.createElement('div');
> 7 |   ReactDOM.render(<App />, div);
    |                   ^
  8 |   ReactDOM.unmountComponentAtNode(div);
  9 | });
Run Code Online (Sandbox Code Playgroud)

谷歌搜索了一段时间并遵循人们对类似问题的建议,我开始在我的 package.json 中添加 Babel devDependencies。它变成了这样:

"devDependencies": {
    "@babel/core": "^7.2.2",
    "@babel/plugin-proposal-class-properties": "^7.2.3",
    "@babel/plugin-transform-runtime": "^7.2.0",
    "@babel/preset-env": "^7.2.3",
    "@babel/preset-react": "^7.0.0",
    "@babel/register": "^7.0.0",
    "@babel/runtime": "^7.2.0",
    "babel-eslint": "^10.0.1",
    "babel-jest": "^23.6.0",
    "babel-loader": "^8.0.5",
    "babel-plugin-root-import": "^6.1.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react-optimize": "^1.0.1",
    "jest-transform-stub": "^1.0.0"
  }
Run Code Online (Sandbox Code Playgroud)

我还将 .babelrc 文件添加为:

{
    "presets": [
        "@babel/react" ,
        "@babel/env"
    ],
    "plugins": [
        "@babel/plugin-proposal-class-properties"
    ]
 }
Run Code Online (Sandbox Code Playgroud)

无论我尝试哪种组合,我总是会遇到不同的错误,这不可能是正确的。看看互联网上的人们,所有人都可以通过 create-react-app 开箱即用地使用 Jest 命令。

最后一次尝试所有这些依赖项会导致以下错误:

Test suite failed to run

    E:\dev\demo\src\App.test.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import React from 'react';
                                                                                                    ^^^^^

    SyntaxError: Unexpected identifier

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?!我很困惑。

Est*_*ask 16

create-react-app是生成react-scripts预配置设置的CLI,其中包括 Webpack 和 Jest 配置等。

当 Jest 直接用作 时jest,它会忽略生成的配置,需要从头开始配置。

npm testreact-scripts test在 CRA 设置中默认为)应该被用来代替jest使用生成的 Jest 配置。

如果问题 npm test是交互模式:

未发现与自上次提交以来更改的文件相关的测试。按a运行所有测试,或运行 Jest--watchAll

它可以用 解决npm test a


Oma*_*gdy 6

只是为了澄清@estus-flask 的答案。
2种解决方案。

第一个解决方案:

package.json

"scripts": {
  "test": "react-scripts test --watchAll",
  ...
},
Run Code Online (Sandbox Code Playgroud)

然后运行

npm test
Run Code Online (Sandbox Code Playgroud)

第二种解决方案:

或者当您运行测试时,像这样运行它们:

npm test .
Run Code Online (Sandbox Code Playgroud)