'Configure Mocha sidebar for typescript test file

How can I configure Mocha sidebar for my project?

My test files are located in this path: /src/Gamma.Tests/*.ts.

Also I set these in settings.json file:

"mocha.files.glob": "src/Gamma.Tests/**/*.ts",
    "mocha.requires": [
        "ts-node/register"
    ]

I have installed these packages already:

npm install typescript --save-dev
npm install ts-node --save-dev
npm install -g ts-node
npm install -g typescript

The error in VSCode:

Failed to run Mocha due to error message:
" Cannot find module 'ts-node/register' Require stack:

  • c:\Users\rouhi.vscode\extensions\maty.vscode-mocha-sidebar-0.22.2\lib\worker\findtests.js " error trace can be found in the ouput channel . for more help:

How can I solve this problem?


Notice:

in package.json file:

"scripts": {
    "test": "mocha -r ts-node/register src/Gamma.Tests/**/*.ts",
    "start": "tsc && node ./build/server.js"
  },

There is no problem, when I run npm run test

vscode environment



Solution 1:[1]

Solution for Mocha Test Explorer

I had the same problem using mocha sidebar. Intrigued by this demo, I installed the Mocha Test Explorer extension and faced the same problem (among others).

It didn't work for me since I had the whole "backend" project in a subfolder of the project root. The solution for me was to set up a multi-root project in VSCode:

  • Click on File -> Add Folder to Workspace
  • Choose your subfolder, e.g. backend
  • Click on File -> Save Workspace As and save the workspace.code-workspace file to the .vscode folder in your project root.
  • In your subfolder (e.g. backend), create a new folder .vscode and place a new settings.json file with the following content:
{
    "mochaExplorer.files": "test/**/*.ts",
    "mochaExplorer.require": "ts-node/register"
}

If you still encounter problems with ts-node/register, this post on StackOverflow might be useful. For tips using a multi-root workspace in VSCode, see this great blog post.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Splines