代码之家  ›  专栏  ›  技术社区  ›  Dimitris Damilos

来自未在Windows上运行的create react应用程序的笑话

  •  0
  • Dimitris Damilos  · 技术社区  · 6 年前

    从使用创建的干净安装的应用程序运行Jest时出现问题 create-react-app 在Windows8.1上。安装完成后,运行命令 npm run test ,我得到一个错误:

    No tests found related to files changed since last commit.

    跑步 jest --watchAll

    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 | });
    

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

    我还将.babelrc文件添加为:

    {
        "presets": [
            "@babel/react" ,
            "@babel/env"
        ],
        "plugins": [
            "@babel/plugin-proposal-class-properties"
        ]
     }
    

    无论我尝试什么组合,我总是会得到不同的错误,这不可能是正确的。看看互联网上的人们,所有人都可以通过createreact应用程序使用开箱即用的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)
    

    有什么想法吗?!我很困惑。

    1 回复  |  直到 6 年前
        1
  •  17
  •   Estus Flask    6 年前

    create-react-app 是生成 react-scripts 预配置的设置,包括网页包和Jest配置等。

    当笑话被直接用作 jest ,它将忽略生成的配置,并要求从头开始配置。

    npm test (默认为 react-scripts test 开玩笑 使用生成的Jest配置。

    npm试验 是互动模式:

    a 运行所有测试,或使用 --watchAll

    这可以通过以下方式解决: npm test a .

        2
  •  1
  •   Omar Magdy    3 年前

    只是为了澄清@estus flask的答案。
    解决。

    第一个解决方案:

    package.json

    "scripts": {
      "test": "react-scripts test --watchAll",
      ...
    },
    

    然后跑

    npm test
    

    第二种解决方案:

    运行测试时,请按如下方式运行:

    npm test a
    
        3
  •  0
  •   VivekDev    3 年前

    在我的例子中,我正在运行一个Azure DevOps管道,以构建和部署一个react应用程序到Azure应用程序服务。因此,我使用下面的脚本任务进行npm安装、npm构建,然后进行如下测试。

    - script: |
        echo Working dir and file list are as follows.
        echo -----------------------------------------
        pwd
        ls -la
        echo Changing directory to reactonazure
        ehco ----------------------------------
        cd reactonazure
        echo Working dir and file list now after chaning directory are as follows.
        echo ---------------------------------------------------------------------
        pwd
        ls -la
        echo Running npm install
        echo -------------------
        npm install
        echo Running npm build --if-present
        echo ------------------------------
        npm run build --if-present
        echo Running CI=true npm test
        echo ------------------------             
        CI=true npm test
      displayName: 'npm install, build and test'
    

    您可以忽略echo、ls、pwd和cd语句。焦点是最后一个

    CI=true npm test
    

    这终于开始起作用了。早些时候我

    npm run test --if-present
    

    这给了我错误。

    No tests found related to files changed since last commit.