Tomek Nowak
Tomek Nowak

Reputation: 123

JetBrains WebStorm Jest encountered an unexpected token

I have a problem with running a simple project in WebStorm IDE. This is what I get when I hit run:

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)

At this moment, my project contains just 1 html file. I tried reinstalling nodejs and npm, but that didn't work

Upvotes: 2

Views: 2750

Answers (2)

Alex C
Alex C

Reputation: 11

In case it helps someone:

I ran into the same issue running jest test. The problem in my case was with a wrong Run Configuration, created by default. Jest package was selected as /node_modules/react-scripts

When I changed it to /node_modules/jest everything started working.

Upvotes: 0

Alexey L
Alexey L

Reputation: 9

Good time of day, I've encoutered the same problem with one Edu project in WebStorm IDE - when i pressed the "check" button for task - i've got issue: "test suite failed to run Jest encountered an unexpected token ..."

How it was solved:

  1. Please check if nodejs and npm installed - At IDE: File\Settings, section: Languages & frameworks > nodejs and npm section > at field: "Node interpreter" you'll find something like : "node /usr/bin/node" and at field: "Package manager" the value "npm /usr/share/npm"
  2. In IDE - you need to expand "Project pane" and to choose view mode "Project Files" - then you need open the file "package.json", initially this file contains the code:
{
  "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"
  }
 }

You need to add the section for "jest" - please found the complete package.json below

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

So,i hope it help you.

Upvotes: 0

Related Questions