Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the only modifier. #204

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable testCount is maintained by the addTest, addOnlyTest, addSerialTest function, which I think that is not suitable. A better way is to return such information (testCount, failCount) from the promise, then calculate the final stats from at the end of the chain.

However, it involves more code change if doing so. Any ideas?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's perfectly fine, I wouldn't call it a hack.

stats.passCount = stats.testCount - stats.failCount;
});
};
24 changes: 24 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
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