From 7c91f546f17937748704709fa85bfa3b7b287e59 Mon Sep 17 00:00:00 2001 From: Jens Lind Date: Thu, 10 Sep 2015 20:44:16 +0200 Subject: [PATCH 1/5] Allow custom assert modules --- index.js | 1 + lib/runner.js | 10 ++++++++++ lib/test.js | 25 +++++++++++++++---------- 3 files changed, 26 insertions(+), 10 deletions(-) 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..87931fa27 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -92,6 +92,11 @@ Runner.prototype._addTestResult = function (test) { }; Runner.prototype.run = function () { + // Set default assert module if needed + if (!this._assertModule) { + Test._setAssertModule(); + } + var self = this; var tests = this.tests; var stats = this.stats; @@ -118,3 +123,8 @@ Runner.prototype.run = function () { stats.passCount = stats.testCount - stats.failCount; }); }; + +// Set custom assert module +Runner.prototype.setAssertModule = function (assertModule) { + this._assertModule = Test._setAssertModule(assertModule); +}; diff --git a/lib/test.js b/lib/test.js index 23dea0bbd..1ae3f8268 100644 --- a/lib/test.js +++ b/lib/test.js @@ -35,17 +35,22 @@ Test.prototype._assert = function () { } }; -Object.keys(assert).forEach(function (el) { - Test.prototype[el] = function () { - this._assert(); +Test._setAssertModule = function (assertModule) { + var lib = assertModule || assert; + Object.keys(lib).forEach(function (el) { + Test.prototype[el] = function () { + this._assert(); + + try { + lib[el].apply(lib, arguments); + } catch (err) { + this.assertError = err; + } + }; + }); - try { - assert[el].apply(assert, arguments); - } catch (err) { - this.assertError = err; - } - }; -}); + return (assertModule) ? 'custom' : 'default'; +}; Test.prototype.plan = function (count) { if (typeof count !== 'number') { From e1b827b7461f6ea38b40db5a522a37841c8d5063 Mon Sep 17 00:00:00 2001 From: Jens Lind Date: Thu, 10 Sep 2015 21:29:33 +0200 Subject: [PATCH 2/5] Always set default assertion lib This may need some rethinking :cat2: --- lib/runner.js | 7 +------ lib/test.js | 6 ++---- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/lib/runner.js b/lib/runner.js index 87931fa27..9eaa1396a 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -92,11 +92,6 @@ Runner.prototype._addTestResult = function (test) { }; Runner.prototype.run = function () { - // Set default assert module if needed - if (!this._assertModule) { - Test._setAssertModule(); - } - var self = this; var tests = this.tests; var stats = this.stats; @@ -126,5 +121,5 @@ Runner.prototype.run = function () { // Set custom assert module Runner.prototype.setAssertModule = function (assertModule) { - this._assertModule = Test._setAssertModule(assertModule); + Test._setAssertModule(assertModule); }; diff --git a/lib/test.js b/lib/test.js index 1ae3f8268..64659592a 100644 --- a/lib/test.js +++ b/lib/test.js @@ -35,7 +35,7 @@ Test.prototype._assert = function () { } }; -Test._setAssertModule = function (assertModule) { +(Test._setAssertModule = function (assertModule) { var lib = assertModule || assert; Object.keys(lib).forEach(function (el) { Test.prototype[el] = function () { @@ -48,9 +48,7 @@ Test._setAssertModule = function (assertModule) { } }; }); - - return (assertModule) ? 'custom' : 'default'; -}; +})(); Test.prototype.plan = function (count) { if (typeof count !== 'number') { From accbeda0565f3041c79cd361ede9afe71ee6223f Mon Sep 17 00:00:00 2001 From: Jens Lind Date: Sat, 12 Sep 2015 15:05:07 +0200 Subject: [PATCH 3/5] Added assertModule to Test --- lib/runner.js | 12 +++++++----- lib/test.js | 22 +++++++++++++++------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/lib/runner.js b/lib/runner.js index 9eaa1396a..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) { @@ -121,5 +123,5 @@ Runner.prototype.run = function () { // Set custom assert module Runner.prototype.setAssertModule = function (assertModule) { - Test._setAssertModule(assertModule); + this._assertModule = assertModule; }; diff --git a/lib/test.js b/lib/test.js index 64659592a..7b8372693 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 assertModule === 'function') { + fn = assertModule; + assertModule = null; + } else if (typeof title === 'function') { + fn = title; + title = 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,7 +43,7 @@ Test.prototype._assert = function () { } }; -(Test._setAssertModule = function (assertModule) { +Test.prototype._setAssertModule = function (assertModule) { var lib = assertModule || assert; Object.keys(lib).forEach(function (el) { Test.prototype[el] = function () { @@ -48,7 +56,7 @@ Test.prototype._assert = function () { } }; }); -})(); +}; Test.prototype.plan = function (count) { if (typeof count !== 'number') { From 80c537d4f10a6b67f12a4adddd864ab7c086c9a4 Mon Sep 17 00:00:00 2001 From: Jens Lind Date: Sat, 12 Sep 2015 15:08:08 +0200 Subject: [PATCH 4/5] Use `Object.defineProperty` --- lib/test.js | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/lib/test.js b/lib/test.js index 7b8372693..1c0e161bc 100644 --- a/lib/test.js +++ b/lib/test.js @@ -45,17 +45,25 @@ Test.prototype._assert = function () { Test.prototype._setAssertModule = function (assertModule) { var lib = assertModule || assert; - Object.keys(lib).forEach(function (el) { - Test.prototype[el] = function () { - this._assert(); - - try { - lib[el].apply(lib, 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) { From 0eb3bcff8f83f48db5537a99a3250eb7625a7fff Mon Sep 17 00:00:00 2001 From: Jens Lind Date: Sat, 12 Sep 2015 15:19:04 +0200 Subject: [PATCH 5/5] Check the title first --- lib/test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/test.js b/lib/test.js index 1c0e161bc..94d440904 100644 --- a/lib/test.js +++ b/lib/test.js @@ -10,12 +10,12 @@ function Test(title, assertModule, fn) { } if (fn === undefined) { - if (typeof assertModule === 'function') { - fn = assertModule; - assertModule = null; - } else if (typeof title === 'function') { + if (typeof title === 'function') { fn = title; title = null; + } else if (typeof assertModule === 'function') { + fn = assertModule; + assertModule = null; } }