JetBrains WebStorm Jest 遇到意外令牌

问题描述 投票:0回答:2

我在 WebStorm IDE 中运行简单项目时遇到问题。这就是我运行时得到的结果:

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:

    /home/patryk/WebstormProjects/Case Converter/node_modules/hs-test-web/hstest/stage/stageTest.js:12
        runner = new PureJsApplicationRunner();
               ^

    SyntaxError: Unexpected token =

      at ScriptTransformer._transformAndBuildScript (node_modules/jest/node_modules/jest-runtime/build/script_transformer.js:403:17)
      at Object.<anonymous> (node_modules/hs-test-web/hstest/index.js:1:110)

目前,我的项目仅包含 1 个 html 文件。我尝试重新安装nodejs和npm,但这不起作用

html node.js npm frontend webstorm
2个回答
0
投票

一天中的美好时光, 我在 WebStorm IDE 中的一个 Edu 项目中遇到了同样的问题 - 当我按下任务的“检查”按钮时 - 我遇到了问题: “测试套件运行失败 Jest 遇到了意外的令牌...”

如何解决:

  1. 请检查是否安装了nodejs和npm - 在IDE:文件\设置,部分:语言和框架> nodejs和npm部分>在字段:“节点解释器”中,您会发现类似:“node / usr / bin / node”并在字段:“包管理器”中输入值“npm /usr/share/npm”
  2. 在 IDE 中 - 您需要展开“项目窗格”并选择查看模式“项目文件” - 然后您需要打开文件“package.json”, 最初该文件包含代码:
{
  "devDependencies": {
    "@types/jest": "^23.3.12",
    "hs-test-web": "https://github.com/hyperskill/hs-test-web/archive/release.tar.gz",
    "jest": "^27.3.1",
    "puppeteer": ">=8.0.0"
  },
  "scripts": {
    "test": "jest"
  }
 }

您需要添加“jest”部分 - 请在下面找到完整的 package.json

{
  "devDependencies": {
    "@types/jest": "^23.3.12",
    "hs-test-web": "https://github.com/hyperskill/hs-test-web/archive/release.tar.gz",
    "jest": "^27.3.1",
    "puppeteer": ">=8.0.0"
  },
  "scripts": {
    "test": "jest"
  },
  "jest": {
    "verbose": true,
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json"
    ],
    "transform": {
      "^.+\\.jsx?$": "babel-jest",
      "^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
    },
    "transformIgnorePatterns": ["/node_modules/(?!lodash-es)"],
    "testRegex": "test/.*\\.spec\\.ts$"
  }
}

所以,希望对你有帮助。


0
投票

如果它对某人有帮助:

我在运行笑话测试时遇到了同样的问题。我的例子中的问题是默认创建的错误运行配置。 Jest 包被选择为 /node_modules/react-scripts

当我将其更改为 /node_modules/jest 时,一切都开始工作了。

© www.soinside.com 2019 - 2024. All rights reserved.