diff --git a/index.js b/index.js index e5639f2a9..753ef2b66 100644 --- a/index.js +++ b/index.js @@ -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); diff --git a/lib/runner.js b/lib/runner.js index 84d8d8976..1b64623a8 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -24,6 +24,8 @@ function Runner(opts) { before: [], after: [] }; + + this._assertModule = null; } util.inherits(Runner, EventEmitter); @@ -31,20 +33,20 @@ 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) { @@ -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; +}; diff --git a/lib/test.js b/lib/test.js index 23dea0bbd..94d440904 100644 --- a/lib/test.js +++ b/lib/test.js @@ -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) { 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]'; @@ -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; @@ -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) { + Object.defineProperty(Test.prototype, el, { + get: function () { + this._assert(); + + return method; + }, + configurable: true + }); + } +}; Test.prototype.plan = function (count) { if (typeof count !== 'number') {