7

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.

DerMike
  • 15,594
  • 13
  • 50
  • 63
deitch
  • 14,019
  • 14
  • 68
  • 96

6 Answers6

7

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.

deitch
  • 14,019
  • 14
  • 68
  • 96
  • That limits the execution to specific tests or suites though. I thought you wanted to execute them by keyword. Anyway, happy you've found a solution that fits your needs. – MarcoL Oct 29 '14 at 21:02
  • I was trying to limit to specific tests or suites on the fly. I thought the only way to do that was a "macro" approach, like the CLI, but somehow real-time injected into karma. I didn't think of going micro, from the bottom up. This worked really well. – deitch Oct 30 '14 at 09:36
4

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.

Community
  • 1
  • 1
MarcoL
  • 9,829
  • 3
  • 37
  • 50
  • thanks. I read it, but even that depends on the comment-line. When I run command-line tests, that is good; I just do `mocha -g 'some expression'`. But karma takes some time to start up and automatically reruns. I would like to be able to have some file that I can include where I just change it and it picks it up automatically. Any ideas? – deitch Oct 29 '14 at 13:34
0

I have same question and this is my workround by a little change on karma.conf.js. In fact, take an argument from command line and modify the pattern in "files". I use minimist to parse the argument list.

In config file:

/* Begin */
var minimist = require('minimist');
var argv = minimist(process.argv);
var testBase="test/unit";
var testExt=".spec.js";
var unitTestPattern = testBase+'/**/*'+testExt;
if ("test" in argv){
  unitTestPattern = testBase+"/"+argv["test"]+testExt;
}
/* End */
module.exports = function(config){
  config.set({
    //....

    files : [
    //....
      unitTestPattern,             //place here
//      'test/unit/**/*.spec.js',  //replace this
    //....
    ],
    //....

  });
};

run in command prompt:

karma start test/karma.conf.js --single-run  --test #TEST_CASE_FILE#
Ziong
  • 54
  • 1
0

a nice extension that can help here is karma-jasmine-html-reporter-livereload https://www.npmjs.com/package/karma-jasmine-html-reporter-livereload

or karma-jasmine-html-reporter https://www.npmjs.com/package/karma-jasmine-html-reporter?__hstc=72727564.86845f057bb4d741f59d578059e30644.1443860954685.1453095135802.1453138187458.37&__hssc=72727564.1.1453138187458&__hsfp=2285154675

It creates a debug page in which you can run each test individually. very useful for large projects!

Tomer Almog
  • 3,604
  • 3
  • 30
  • 36
0

1) In your karma.conf.js get the params from the terminal:

var files = (process.env.npm_config_single_file) ? process.env.npm_config_single_file : 'test/test_index.js';

2) In order to run a single test you will need to set an option object with all your configuration (Without files and preprocessors):

var option = {

  webpack: {
    // webpack configuration
  },

  // more configuration......
};

3) Set your files path and preprocessors:

  option.files = [
      {pattern: files, watch: false}
  ];

  option.preprocessors = {};

  option.preprocessors[files] = [ 'webpack', 'sourcemap' ];

  // call config.set function
  config.set(option);

4) Run in the terminal:

npm test --single_file=**/my-specific-file-spec.js

For more information check this PR: https://github.com/webpack/karma-webpack/pull/178

David
  • 1,284
  • 1
  • 11
  • 21
0

There are different ways to do it.

  1. Use --grep option. The disadvantage of this is that all the tests are preprocessed before running the specific test suite.

  2. Use .only method. Disadvantage same as no. 1. Using both 1 and 2 method my node process used to crash often saying out of memory.

  3. Limit the files options for processing. This is super fast.

Limit preprocessing to certain folder like Unit or Integration folder. For this I have used custom cli option --only and in the karma config

const modules = config.only;

and in the the files pattern

files: typeof modules === 'string ? '[`tests/**/${module}/**/*.(test|spec).js`]: 'tests/**/*.(test|spec).js'

Advantage: Developers can run only certain tests when they make a small change way faster by limiting in the preprocessing phase.

You can also use combination of no.3 and no.1 or 2.

bring2dip
  • 886
  • 2
  • 11
  • 22