Skip to content

Commit

Permalink
Implement a "grep" option in runner.
Browse files Browse the repository at this point in the history
This aims to resolve issue #476. --grep can be passed to
ava which means to skip every test that doesn't match the
supplied pattern search for the test title. The pattern
can contain regexp strings and the skipped flag will be
set based on the output of "test" (so anywhere in the
title of the test will work).
  • Loading branch information
matthewbauer committed Jan 27, 2016
1 parent ffef6d6 commit a19bded
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
8 changes: 6 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var cli = meow([
' --tap, -t Generate TAP output',
' --verbose, -v Enable verbose output',
' --no-cache Disable the transpiler cache',
' --grep, -g Only run tests matching a pattern',
'',
'Examples',
' ava',
Expand All @@ -62,7 +63,8 @@ var cli = meow([
], {
string: [
'_',
'require'
'require',
'grep'
],
boolean: [
'fail-fast',
Expand All @@ -75,7 +77,8 @@ var cli = meow([
t: 'tap',
v: 'verbose',
r: 'require',
s: 'serial'
s: 'serial',
g: 'grep'
}
});

Expand All @@ -90,6 +93,7 @@ var api = new Api(cli.input.length ? cli.input : arrify(conf.files), {
failFast: cli.flags.failFast,
serial: cli.flags.serial,
require: arrify(cli.flags.require),
grep: cli.flags.grep,
cacheEnabled: cli.flags.cache !== false
});

Expand Down
8 changes: 6 additions & 2 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ var chainableMethods = {
}
};

function Runner() {
function Runner(opts) {
if (!(this instanceof Runner)) {
return new Runner();
return new Runner(opts);
}

EventEmitter.call(this);

this.grep = (opts && opts.grep) ? opts.grep : '';

this.results = [];
this.tests = new TestCollection();
this._addTestResult = this._addTestResult.bind(this);
Expand All @@ -48,6 +50,8 @@ optionChain(chainableMethods, function (opts, title, fn) {
title = null;
}

opts.skipped = opts.skipped || (title && title.indexOf(this.grep) === -1);

this.tests.add({
metadata: opts,
fn: fn,
Expand Down
26 changes: 26 additions & 0 deletions test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,29 @@ test('only test', function (t) {
t.end();
});
});

test('skip test based on grep option with string', function (t) {
t.plan(3);

var runner = new Runner({
grep: 'dont skip me'
});

runner.test('skip me1', function () {
t.fail();
});

runner.skip('skip me2', function () {
t.fail();
});

runner.test('dont skip me', function () {
t.pass();
});

runner.run().then(function () {
t.is(runner.stats.passCount, 1);
t.is(runner.stats.failCount, 0);
t.end();
});
});

0 comments on commit a19bded

Please sign in to comment.