Search code examples
typescriptmocha.jswebstormchai

WebStorm can not find ts-node/module and describe definition when setup with Mocha, Chai and TypeScript


I am having problems setting up WebStorm with Mocha/Chai and TypeScript.

I installed mocha and chai setup a test script in my package.json like so

"test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha -r ts-node/register 'tests/**/*.ts'"

When I run npm test the test runs and everything is fine.

However WebStorm does not recognize describe and it. Even though I am running WebStorm 2020.3 I followed the solution in this post without any success.

Furthermore when I use the runner in WebStorm I get the following error:

ERROR: Error: Cannot find module 'ts-node/register'

In the Runner configuration in WebStorm I added the following:

  • Environmental Variables: env TS_NODE_COMPILER_OPTIONS='{"module": "commonjs" }'
  • Extra mocha options: mocha -r ts-node/register

enter image description here

However this also did not help.

What am I missing?

[Edit]

Thanks to the comment by lena I found out that the Mocha package was pointing to the wrong project so I fixed it. Unfortunately I now get this error:

enter image description here


Solution

  • Try changing the way your environment variable is defined - remove the single quotes around the value, this is what mocha is complaining about. Just set it to TS_NODE_COMPILER_OPTIONS={"module":"commonjs"}:

    enter image description here

    it works fine for me