Skip to content

Commit

Permalink
add test.only(), closes #204
Browse files Browse the repository at this point in the history
  • Loading branch information
lijunle authored and vdemedes committed Nov 17, 2015
1 parent 2d36b51 commit 93f733c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -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);
14 changes: 12 additions & 2 deletions lib/runner.js
Expand Up @@ -32,6 +32,7 @@ function Runner(opts) {
this.tests = {
concurrent: [],
serial: [],
only: [],
before: [],
after: [],
beforeEach: [],
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
});
};
24 changes: 24 additions & 0 deletions test/test.js
Expand Up @@ -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);

Expand Down

0 comments on commit 93f733c

Please sign in to comment.