Skip to content

Commit 93f733c

Browse files
lijunlevdemedes
authored andcommitted
add test.only(), closes #204
1 parent 2d36b51 commit 93f733c

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,4 @@ module.exports.after = runner.addAfterHook.bind(runner);
7979
module.exports.beforeEach = runner.addBeforeEachHook.bind(runner);
8080
module.exports.afterEach = runner.addAfterEachHook.bind(runner);
8181
module.exports.skip = runner.addSkippedTest.bind(runner);
82+
module.exports.only = runner.addOnlyTest.bind(runner);

lib/runner.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function Runner(opts) {
3232
this.tests = {
3333
concurrent: [],
3434
serial: [],
35+
only: [],
3536
before: [],
3637
after: [],
3738
beforeEach: [],
@@ -97,6 +98,11 @@ Runner.prototype.addSkippedTest = function (title, cb) {
9798
this.tests.concurrent.push(test);
9899
};
99100

101+
Runner.prototype.addOnlyTest = function (title, cb) {
102+
this.stats.testCount++;
103+
this.tests.only.push(new Test(title, cb));
104+
};
105+
100106
Runner.prototype._runTestWithHooks = function (test) {
101107
if (test.skip) {
102108
this._addTestResult(test);
@@ -203,16 +209,20 @@ Runner.prototype.run = function () {
203209
}
204210
})
205211
.then(function () {
206-
return self.serial(tests.serial);
212+
return self.concurrent(tests.only);
213+
})
214+
.then(function () {
215+
return tests.only.length ? [] : self.serial(tests.serial);
207216
})
208217
.then(function () {
209-
return self.concurrent(tests.concurrent);
218+
return tests.only.length ? [] : self.concurrent(tests.concurrent);
210219
})
211220
.then(function () {
212221
return eachSeries(tests.after, self._runTest.bind(self));
213222
})
214223
.catch(noop)
215224
.then(function () {
225+
stats.testCount = tests.only.length ? tests.only.length : stats.testCount;
216226
stats.passCount = stats.testCount - stats.failCount;
217227
});
218228
};

test/test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,30 @@ test('skip test', function (t) {
10341034
});
10351035
});
10361036

1037+
test('only test', function (t) {
1038+
t.plan(3);
1039+
1040+
var runner = new Runner();
1041+
var arr = [];
1042+
1043+
runner.addTest(function (a) {
1044+
arr.push('a');
1045+
a.end();
1046+
});
1047+
1048+
runner.addOnlyTest(function (a) {
1049+
arr.push('b');
1050+
a.end();
1051+
});
1052+
1053+
runner.run().then(function () {
1054+
t.is(runner.stats.testCount, 1);
1055+
t.is(runner.stats.passCount, 1);
1056+
t.same(arr, ['b']);
1057+
t.end();
1058+
});
1059+
});
1060+
10371061
test('ES2015 support', function (t) {
10381062
t.plan(1);
10391063

0 commit comments

Comments
 (0)