Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selective test execution in Karma Jasmine using pattern matching

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"
  }
}
like image 553
Aristarkh Artemiy Avatar asked Jun 03 '15 19:06

Aristarkh Artemiy


1 Answers

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.

like image 88
Nikos Paraskevopoulos Avatar answered Oct 01 '22 19:10

Nikos Paraskevopoulos