Browse Source

Merge pull request #612 from sindresorhus/match-without-match

Don't run tests if --match has no matches
browser-support
Mark Wubben 9 years ago
parent
commit
45f96bd516
  1. 71
      api.js
  2. 6
      lib/runner.js
  3. 15
      test/api.js
  4. 13
      test/fixture/match-no-match.js
  5. 2
      test/fixture/pkg-conf/precedence/c.js

71
api.js

@ -28,6 +28,7 @@ function Api(options) {
this.options = options || {}; this.options = options || {};
this.options.require = (this.options.require || []).map(resolveCwd); this.options.require = (this.options.require || []).map(resolveCwd);
this.options.match = this.options.match || [];
this.excludePatterns = [ this.excludePatterns = [
'!**/node_modules/**', '!**/node_modules/**',
@ -194,35 +195,57 @@ Api.prototype.run = function (files) {
var tests = files.map(self._runFile); var tests = files.map(self._runFile);
// receive test count from all files and then run the tests // receive test count from all files and then run the tests
var statsCount = 0; var unreportedFiles = self.fileCount;
return new Promise(function (resolve) { return new Promise(function (resolve) {
tests.forEach(function (test) { function run() {
function tryRun() { if (self.options.match.length > 0 && !self.hasExclusive) {
if (++statsCount === self.fileCount) { self._handleExceptions({
self.emit('ready'); exception: new AvaError('Couldn\'t find any matching tests'),
file: undefined
});
tests.forEach(function (test) {
// No tests will be run so tear down the child processes.
test.send('teardown');
});
resolve([]);
return;
}
var method = self.options.serial ? 'mapSeries' : 'map'; self.emit('ready');
var options = {
runOnlyExclusive: self.hasExclusive var method = self.options.serial ? 'mapSeries' : 'map';
var options = {
runOnlyExclusive: self.hasExclusive
};
resolve(Promise[method](files, function (file, index) {
return tests[index].run(options).catch(function (err) {
// The test failed catastrophically. Flag it up as an
// exception, then return an empty result. Other tests may
// continue to run.
self._handleExceptions({
exception: err,
file: file
});
return {
stats: {passCount: 0, skipCount: 0, todoCount: 0, failCount: 0},
tests: []
}; };
});
}));
}
resolve(Promise[method](files, function (file, index) { tests.forEach(function (test) {
return tests[index].run(options).catch(function (err) { var tried = false;
// The test failed catastrophically. Flag it up as an function tryRun() {
// exception, then return an empty result. Other tests may if (!tried) {
// continue to run. unreportedFiles--;
self._handleExceptions({ if (unreportedFiles === 0) {
exception: err, run();
file: file }
});
return {
stats: {passCount: 0, skipCount: 0, todoCount: 0, failCount: 0},
tests: []
};
});
}));
} }
} }

6
lib/runner.js

@ -43,7 +43,7 @@ function Runner(options) {
this.tests = new TestCollection(); this.tests = new TestCollection();
this._bail = options.bail; this._bail = options.bail;
this._serial = options.serial; this._serial = options.serial;
this._match = options.match; this._match = options.match || [];
this._addTestResult = this._addTestResult.bind(this); this._addTestResult = this._addTestResult.bind(this);
} }
@ -71,8 +71,8 @@ optionChain(chainableMethods, function (opts, title, fn) {
opts.serial = true; opts.serial = true;
} }
if (opts.type === 'test' && title !== null && this._match && this._match.length > 0) { if (opts.type === 'test' && this._match.length > 0) {
opts.exclusive = matcher([title], this._match).length > 0; opts.exclusive = title !== null && matcher([title], this._match).length === 1;
} }
this.tests.add({ this.tests.add({

15
test/api.js

@ -715,3 +715,18 @@ test('Default babel config doesn\'t use .babelrc', function (t) {
t.is(api.passCount, 1); t.is(api.passCount, 1);
}); });
}); });
test('using --match with no matching tests causes an AvaError to be emitted', function (t) {
t.plan(2);
var api = new Api({
match: ['can\'t match this']
});
api.on('error', function (err) {
t.is(err.name, 'AvaError');
t.match(err.message, /Couldn't find any matching tests/);
});
return api.run([path.join(__dirname, 'fixture/match-no-match.js')]);
});

13
test/fixture/match-no-match.js

@ -0,0 +1,13 @@
import test from 'ava';
test('foo', t => {
t.pass();
});
test.only('bar', t => {
t.pass();
});
test.only(t => {
t.pass();
});

2
test/fixture/pkg-conf/precedence/c.js

@ -3,7 +3,7 @@ import path from 'path';
const opts = JSON.parse(process.argv[2]); const opts = JSON.parse(process.argv[2]);
test(t => { test('foo', t => {
t.is(opts.failFast, false); t.is(opts.failFast, false);
t.is(opts.serial, false); t.is(opts.serial, false);
t.is(opts.cacheEnabled, true); t.is(opts.cacheEnabled, true);

Loading…
Cancel
Save