Search code examples
javascripttypescriptunit-testingintellij-ideakarma-mocha

IntelliJ IDEA: Can I run "green arrow" unit tests directly with Mocha, when both Karma and Mocha are installed?


I have unit tests set up with Karma and Mocha. Karma is important here because some of the functionality I'm testing needs a web browser (even if a fake headless one). But much of the code can run either in a browser or Node.js. For debugging tests it would be much easier to skip launching Karma and use Mocha directly most of the time.

I can do that easily enough if running the whole test suite, but I'd like to be able to use the convenience of the little green play-button style arrows for individual tests. Unfortunately, even for a single unit test, these always launch Karma now.

Disabling the Karma plugin doesn't help. Instead, that makes all of the green arrows go away, with no easy access to either Karma or Mocha.

Unit test screen shot

Is there a way to configure IDEA so that these convenience arrows ignore Karma, and directly run Mocha tests instead?


Solution

  • The logic used for determining what test runner is available for a given test file is based on dependencies declarations in package.json nearest to the current file.

    Note that if you have a single package.json, with both karma and mocha included, and there is a karma config in your project, karma is preferred - see https://youtrack.jetbrains.com/issue/WEB-26070#comment=27-2088951. To force using Mocha test runner for files in a certain directory, create a Mocha run configuration with Test directory: set to this directory - when running tests from gutter in this folder, mocha will be used.