From a19bdeda70de1c9ee1f38ed69e78b778a86f4805 Mon Sep 17 00:00:00 2001 From: Matthew Bauer Date: Wed, 27 Jan 2016 06:48:00 -0600 Subject: [PATCH] Implement a "grep" option in runner. 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). --- cli.js | 8 ++++++-- lib/runner.js | 8 ++++++-- test/runner.js | 26 ++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/cli.js b/cli.js index c86bdff739..09a638a61f 100755 --- a/cli.js +++ b/cli.js @@ -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', @@ -62,7 +63,8 @@ var cli = meow([ ], { string: [ '_', - 'require' + 'require', + 'grep' ], boolean: [ 'fail-fast', @@ -75,7 +77,8 @@ var cli = meow([ t: 'tap', v: 'verbose', r: 'require', - s: 'serial' + s: 'serial', + g: 'grep' } }); @@ -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 }); diff --git a/lib/runner.js b/lib/runner.js index 93c3266d26..296ad06bda 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -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); @@ -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, diff --git a/test/runner.js b/test/runner.js index 708eb1a970..80149d4fb0 100644 --- a/test/runner.js +++ b/test/runner.js @@ -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(); + }); +});