Karma run single test

I use karma to run tests. I have many tests and running all tests is a very slow process. I want to run only a single test in order to spend less time, because all tests are run about 10 minutes.

Is it possible?


Solution 1:

If you are using the Karma/Jasmine stack, use:

fdescribe("when ...", function () { // to [f]ocus on a single group of tests
  fit("should ...", function () {...}); // to [f]ocus on a single test case
});

... and:

xdescribe("when ...", function () { // to e[x]clude a group of tests
  xit("should ...", function () {...}); // to e[x]clude a test case
});

When you're on Karma/Mocha:

describe.only("when ...", function () { // to run [only] this group of tests
  it.only("should ...", function () {...}); // to run [only] this test case
});

... and:

describe.skip("when ...", function () { // to [skip] running this group of tests
  it.skip("should ...", function () {...}); // to [skip] running this test case
});

Solution 2:

Update: karma has changed.

Now use fit() and fdescribe()

f stands for focused!

Solution 3:

For Angular users!

I know two ways:

  1. Visual Studio Code Extension:

The easiest way is to use the vscode-test-explorer extension along with its child angular-karma-test-explorer and jasmine-test-adapter, you'll get a list of current test to run one by one if you want:

enter image description here

  1. Directly modify test.ts

For me, i wasn't able to use the extension way because of this bug, and so i ended up modifying the test.ts file (as stated here by Shashi), just to consolidate that answer here, by default context looks like this:

const context = require.context('./', true, /\.spec\.ts$/);

You should modify it's RegExp to match the files that you'r willing to test, for example if you want to test a single file named "my.single.file.custom.name.spec.ts" it'll look this way:

const context = require.context('./', true, /my\.single\.file\.custom\.name\.spec\.ts$/);

For more details about require parameters you may find it here at their wiki.

  1. Karma runner improvement

Currently there's an open issue to improve their current behaviour, you can follow their progress at their github page (https://github.com/karma-runner/karma/issues/1507).