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 0e6db13 commit d55975e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 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 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
13 changes: 13 additions & 0 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ function Runner(opts) {
EventEmitter.call(this);

this.options = opts || {};

if (this.options.grep instanceof RegExp) {
this.grep = this.options.grep;
} else if (typeof this.options.grep === 'string') {
this.grep = new RegExp(this.options.grep);
} else {
this.grep = new RegExp('');
}

this.results = [];
this.tests = [];
this.testsByType = {};
Expand All @@ -57,6 +66,10 @@ util.inherits(Runner, EventEmitter);
module.exports = Runner;

optionChain(chainableMethods, function (opts, title, fn) {
if (!opts.skipped && !this.grep.test(title)) {
console.log(title);
}
opts.skipped = opts.skipped || !this.grep.test(title);
var Constructor = (opts && /Each/.test(opts.type)) ? Hook : Test;
var test = new Constructor(title, fn);
test.metadata = objectAssign({}, opts);
Expand Down
52 changes: 52 additions & 0 deletions test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,55 @@ 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();
});
});

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

var runner = new Runner({
grep: new RegExp('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 d55975e

Please sign in to comment.