diff --git a/index.js b/index.js index 204063db1..8484e43e4 100644 --- a/index.js +++ b/index.js @@ -79,3 +79,4 @@ module.exports.after = runner.addAfterHook.bind(runner); module.exports.beforeEach = runner.addBeforeEachHook.bind(runner); module.exports.afterEach = runner.addAfterEachHook.bind(runner); module.exports.skip = runner.addSkippedTest.bind(runner); +module.exports.only = runner.addOnlyTest.bind(runner); diff --git a/lib/runner.js b/lib/runner.js index 241a96fa3..10b669c73 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -32,6 +32,7 @@ function Runner(opts) { this.tests = { concurrent: [], serial: [], + only: [], before: [], after: [], beforeEach: [], @@ -97,6 +98,11 @@ Runner.prototype.addSkippedTest = function (title, cb) { this.tests.concurrent.push(test); }; +Runner.prototype.addOnlyTest = function (title, cb) { + this.stats.testCount++; + this.tests.only.push(new Test(title, cb)); +}; + Runner.prototype._runTestWithHooks = function (test) { if (test.skip) { this._addTestResult(test); @@ -203,16 +209,20 @@ Runner.prototype.run = function () { } }) .then(function () { - return self.serial(tests.serial); + return self.concurrent(tests.only); + }) + .then(function () { + return tests.only.length ? [] : self.serial(tests.serial); }) .then(function () { - return self.concurrent(tests.concurrent); + return tests.only.length ? [] : self.concurrent(tests.concurrent); }) .then(function () { return eachSeries(tests.after, self._runTest.bind(self)); }) .catch(noop) .then(function () { + stats.testCount = tests.only.length ? tests.only.length : stats.testCount; stats.passCount = stats.testCount - stats.failCount; }); }; diff --git a/test/test.js b/test/test.js index a0e1418b6..405e448e8 100644 --- a/test/test.js +++ b/test/test.js @@ -1034,6 +1034,30 @@ test('skip test', function (t) { }); }); +test('only test', function (t) { + t.plan(3); + + var runner = new Runner(); + var arr = []; + + runner.addTest(function (a) { + arr.push('a'); + a.end(); + }); + + runner.addOnlyTest(function (a) { + arr.push('b'); + a.end(); + }); + + runner.run().then(function () { + t.is(runner.stats.testCount, 1); + t.is(runner.stats.passCount, 1); + t.same(arr, ['b']); + t.end(); + }); +}); + test('ES2015 support', function (t) { t.plan(1);