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

Allow custom assert modules #49

Closed
wants to merge 5 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 @@ -96,3 +96,4 @@ module.exports = runner.addTest.bind(runner);
module.exports.serial = runner.addSerialTest.bind(runner);
module.exports.before = runner.addBeforeHook.bind(runner);
module.exports.after = runner.addAfterHook.bind(runner);
module.exports.setAssertModule = runner.setAssertModule.bind(runner);
15 changes: 11 additions & 4 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,29 @@ function Runner(opts) {
before: [],
after: []
};

this._assertModule = null;
}

util.inherits(Runner, EventEmitter);
module.exports = Runner;

Runner.prototype.addTest = function (title, cb) {
this.stats.testCount++;
this.tests.concurrent.push(new Test(title, cb));
this.tests.concurrent.push(new Test(title, this._assertModule, cb));
};

Runner.prototype.addSerialTest = function (title, cb) {
this.stats.testCount++;
this.tests.serial.push(new Test(title, cb));
this.tests.serial.push(new Test(title, this._assertModule, cb));
};

Runner.prototype.addBeforeHook = function (title, cb) {
this.tests.before.push(new Test(title, cb));
this.tests.before.push(new Test(title, this._assertModule, cb));
};

Runner.prototype.addAfterHook = function (title, cb) {
this.tests.after.push(new Test(title, cb));
this.tests.after.push(new Test(title, this._assertModule, cb));
};

Runner.prototype.concurrent = function (tests) {
Expand Down Expand Up @@ -118,3 +120,8 @@ Runner.prototype.run = function () {
stats.passCount = stats.testCount - stats.failCount;
});
};

// Set custom assert module
Runner.prototype.setAssertModule = function (assertModule) {
this._assertModule = assertModule;
};
49 changes: 34 additions & 15 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ var setImmediate = require('set-immediate-shim');
var fnName = require('fn-name');
var assert = require('./assert');

function Test(title, fn) {
function Test(title, assertModule, fn) {
Copy link
Contributor

Choose a reason for hiding this comment

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

title and fn are more common properties, while assertModule is absolutely optional. I think it needs to be the last one in the argument list.

if (!(this instanceof Test)) {
return new Test(title, fn);
return new Test(title, assertModule, fn);
}

if (typeof title !== 'string') {
fn = title;
title = null;
if (fn === undefined) {
if (typeof title === 'function') {
fn = title;
title = null;
} else if (typeof assertModule === 'function') {
fn = assertModule;
assertModule = null;
}
}

this.title = title || fnName(fn) || '[anonymous]';
Expand All @@ -23,6 +28,9 @@ function Test(title, fn) {
// store the time point before test execution
// to calculate the total time spent in test
this._timeStart = null;

// Set assert module
this._setAssertModule(assertModule);
}

module.exports = Test;
Expand All @@ -35,17 +43,28 @@ Test.prototype._assert = function () {
}
};

Object.keys(assert).forEach(function (el) {
Test.prototype[el] = function () {
this._assert();
Test.prototype._setAssertModule = function (assertModule) {
var lib = assertModule || assert;

try {
assert[el].apply(assert, arguments);
} catch (err) {
this.assertError = err;
}
};
});
if (typeof lib === 'function') {
setPrototype(lib.name, lib);
} else if (typeof lib === 'object') {
Object.keys(lib).forEach(function (el) {
setPrototype(el, lib[el]);
});
}

function setPrototype(el, method) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Functions inside functions is a really no-go, please separate it out.

Object.defineProperty(Test.prototype, el, {
get: function () {
this._assert();

return method;
},
configurable: true
});
}
};

Test.prototype.plan = function (count) {
if (typeof count !== 'number') {
Expand Down