【问题标题】:JetBrains WebStorm Jest encountered an unexpected tokenJetBrains WebStorm Jest 遇到了意外的令牌
【发布时间】:2021-12-07 15:49:43
【问题描述】:

我在 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


【解决方案1】:

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

它是如何解决的:

  1. 请检查是否安装了 nodejs 和 npm - 在 IDE:文件\设置,部分:语言和框架 > nodejs 和 npm 部分 > 在字段:“节点解释器”中,您会发现类似:“节点 /usr/bin/节点”和字段:“包管理器”值“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$"
  }
}

所以,希望对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 2019-08-24
    相关资源
    最近更新 更多