9

I'm having trouble invoking the command line option on karma-jasmine which allows for the execution of only those tests which match a given pattern. My spec reads as follows:

/path/to/single-test/main.spec.js

describe('my first test suite', function() {
  it('always passes', function() {
    expect(true).toBe(true);
  });

  it('still always passes', function() {
    expect(true).toBe(true);
  });
});

I'm assuming the description (for example "still always passes") is the item against which the pattern specified by the grep command line option is matched. When I attempt to run the second example based on the fact that its description is the only example containing the word "still", both examples are executed instead of just the one:

$ karma start -- --grep=still
INFO [karma]: Karma v0.12.35 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.8 (Linux 0.0.0)]: Connected on socket 7Dn7Ez1Reap7ch0Uzsb0 with id 44623726
PhantomJS 1.9.8 (Linux 0.0.0): Executed 2 of 2 SUCCESS (0.002 secs / 0.001 secs)

How do I execute just this one example based on a pattern? The official documentation doesn't give a sample of the usage of the pattern matching option.

I read in the discussion of a pull request, that the grep option can be used in conjunction with "fit" and "fdescribe." This works when tested. However, in the case of using grep with "fit", what's the purpose of the pattern argument to the grep option? (It would be nice to be able to execute tests selectively without the need to augment source code!)

Here is the remainder of the files in my project for reference:

/path/to/single-test/karma.conf.js

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine'],
    files: ['*.spec.js'],
    exclude: [],
    preprocessors: {},
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: false,
    browsers: ['PhantomJS'],
    singleRun: true
  });
};

/path/to/single-test/package.json

{
  "name": "single-test",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "MIT",
  "devDependencies": {
    "jasmine-core": "^2.3.4",
    "karma": "^0.12.35",
    "karma-jasmine": "^0.3.5",
    "karma-phantomjs-launcher": "^0.2.0",
    "phantomjs": "^1.9.17"
  }
}
Aristarkh Artemiy
  • 347
  • 1
  • 4
  • 10
  • Related: [use `fdescribe`, `fit`](http://stackoverflow.com/a/23793631/) instead of `describe`, `it` – jakub.g Dec 09 '15 at 12:41

2 Answers2

5

You have to start a Karma server, then specify the --grep option in a Karma runner. I.e. something along the lines of:

karma start path/to/karma.conf.js

Then in another terminal:

karma run path/to/karma.conf.js -- --grep=still

It is important that you set singleRun: false in the configuration options.

Nikos Paraskevopoulos
  • 39,514
  • 12
  • 85
  • 90
  • Additional note: when you run the commands above, the second terminal command will run only once and exit, and the first terminal window will continue watching for changes and running only the filtered subset of tests. – jakub.g Dec 08 '15 at 13:53
  • 1
    If you want to grep multiple tags in no particular order, you can use the method from [this answer](https://stackoverflow.com/questions/469913/regular-expressions-is-there-an-and-operator#answer-470602), e.g. for tags `#tag1` and `#tag2`: `--grep='(?=.*#tag1)(?=.*#tag2)'`. – Will Oct 05 '17 at 22:29
0

There is karma-jasmine-spec-tags plugin which helps to filter running tests by tags in their names.

Example usage:

$ karma start --tags smoke
$ karma start --skip-tags slow,bench
$ karma start --tags bench --skip-tags slow
$ karma start --tag-prefix 'scope:' --tags critical

Where a spec is following:

describe('Example test', () => {
    it('should be a #smoke test', () => {
        // ...
    });

    it('#slow test', () => {
        // ...
    });
})

describe('Performance test suite #bench', () => {
    it('#fast #smoke test', () => {
        // ...
    });

    it('#slow test', () => {
        // ...
    });
})

describe('Custom tag prefix', () => {
    it('test scope:critical', () => {
        // ...
    });
})
Mikhail Nasyrov
  • 900
  • 1
  • 8
  • 14