Discovering Mocha tests on VS Code

Published on Sat, 02 Oct 2021.

While working on a TypeScript project and doing TDD, obviously, I needed to debug a test. Sounds easy right? The thing is I wanted to used VSCode's debugger, since it came in handy some times in the past.

My testing rig had the following:

  • Mocha, for testing infrastructure
  • Chai, for assertions
  • TS-Node, NodeJS's TypeScript execution engine

Doing a quick Google search I found this beautiful extension, Mocha Test Explorer, to do what I though would be easy: discovery mocha tests.

However, there's a tiny bit of magic one has to do to enable the discovery of TypeScript-based tests, since there's a compiler in the middle to get to the finally executable JavaScript files. The steps to the everything working goes like this:

  1. In my case I had all tests under a tests/ folder, but this is not necessary, just to give some context.
  2. Install the extension mentioned above in VSCode.
  3. Open VSCode User Settings. Commonly through the command palette, and with the Preferences: Open Settings (JSON) option.
  4. Add this two options in the settings file:
{
    "mochaExplorer.require": "ts-node/register", // This first one enables the TS compiler in the middle
    "mochaExplorer.files": "tests/**/*.ts" // This let's the extension know which are your test files
}
  1. Now run the Mocha Test Explorer: Enable for a workspace folder option from the command palette, and you are done. Happy testing!
Working on my Masters thesis at LAFHIS. Find me on or