How do I run only certain tests in karma?

10,984

Solution 1

So now I feel foolish. mocha supports a very narrow version of regexp matching.

This runs all tests

describe('all tests',function(){
   describe('first tests',function(){
   });
   describe('second tests',function(){
   });
});

This runs just 'first tests'

describe('all tests',function(){
   describe.only('first tests',function(){
   });
   describe('second tests',function(){
   });
});

You can also do it.only()

I should have noticed that. Sigh.

Solution 2

You can do that at karma startup time unfortunately, not at runtime. If you want to change it dynamically you have to put some more effort.

Say you want to focus on a specific set/suite of tests from the beginning, on the karma-mocha plugin page there's this snippet of code to do what you want:

module.exports = function(config) {
  config.set({
    // karma configuration here
    ...

    // this is a mocha configuration object
    client: {
      // The pattern string will be passed to mocha
      args: ['--grep', '<pattern>'],
      ...
    }
  });
};

In order to make the <pattern> parametric you have to wrap the configuration file in a Configurator that will listen CLI and customize the karma configuration for you.

Have a look to this SO answer to know how to setup a very simple Configurator.

Share:
10,984
deitch
Author by

deitch

Technology operations, product and development consultant. 20+ years of Open source Cloud scaling Technology operations Software architecture, design and development

Updated on June 18, 2022

Comments

  • deitch
    deitch about 2 years

    I have karma config set up correctly, config file, running in the background, just great. As soon as I change and save a file, it reruns the tests.... all 750 of the unit tests. I want to be able to run just a few. Short of manually hacking the config file or commenting out hundreds of tests across many files, is there any easy way to do it?

    E.g. when running command line server tests using say mocha, I just use regexp: mocha -g 'only tests that I want'. Makes it much easier to debug and quickly check.