☕️ Classic, reliable, trusted test framework for Node.js and the browser
Added a `Context` object used for hook and test-case this. Closes #253
Tj Holowaychukba8cf0ce2b16
diff --git a/bin/_mocha b/bin/_mochaindex 1e494b03..f5b92483 100755--- a/bin/_mocha+++ b/bin/_mocha@@ -12,6 +12,7 @@ var program = require('commander'), utils = mocha.utils, reporters = mocha.reporters, interfaces = mocha.interfaces+ , Context = mocha.Context, Runner = mocha.Runner, Suite = mocha.Suite, vm = require('vm')@@ -121,7 +122,7 @@ Error.stackTraceLimit = Infinity; // TODO: config// reporter-var suite = new Suite('')+var suite = new Suite('', new Context), Base = require('../lib/reporters/base'), Reporter = require('../lib/reporters/' + program.reporter), ui = interfaces[program.ui](suite);diff --git a/lib/context.js b/lib/context.jsnew file mode 100644index 00000000..0f72bbda--- /dev/null+++ b/lib/context.js@@ -0,0 +1,55 @@++/**+ * Expose `Context`.+ */++module.exports = Context;++/**+ * Initialize a new `Context`.+ *+ * @api private+ */++function Context(){}++/**+ * Set the context `Test` to `test`.+ *+ * @param {Test} test+ * @return {Context}+ * @api private+ */++Context.prototype.test = function(test){+ this._test = test;+ return this;+};++/**+ * Set test timeout `ms`.+ *+ * @param {Number} ms+ * @return {Context} self+ * @api private+ */++Context.prototype.timeout = function(ms){+ this._test.timeout(ms);+ return this;+};++/**+ * Inspect the context void of `._test`.+ *+ * @return {String}+ * @api private+ */++Context.prototype.inspect = function(){+ return JSON.stringify(this, function(key, val){+ return '_test' == key+ ? undefined+ : val;+ }, 2);+};diff --git a/lib/hook.js b/lib/hook.jsindex 0dba20c3..3ed712e9 100644--- a/lib/hook.js+++ b/lib/hook.js@@ -21,6 +21,7 @@ module.exports = Hook;function Hook(title, fn) {Runnable.call(this, title, fn);+ this.type = 'hook';}/**diff --git a/lib/mocha.js b/lib/mocha.jsindex 47af8a6e..502fb29f 100644--- a/lib/mocha.js+++ b/lib/mocha.js@@ -15,6 +15,7 @@ exports.utils = require('./utils');exports.interfaces = require('./interfaces');exports.reporters = require('./reporters');exports.Runnable = require('./runnable');+exports.Context = require('./Context');exports.Runner = require('./runner');exports.Suite = require('./suite');exports.Hook = require('./hook');diff --git a/lib/runnable.js b/lib/runnable.jsindex 60891291..7b5da3e2 100644--- a/lib/runnable.js+++ b/lib/runnable.js@@ -27,7 +27,6 @@ function Runnable(title, fn) {this.sync = ! this.async;this._timeout = 2000;this.timedOut = false;- this.context = this;}/**@@ -104,7 +103,7 @@ Runnable.prototype.run = function(fn){var self = this, ms = this.timeout(), start = new Date- , ctx = this.context+ , ctx = this.ctx, finished, emitted;diff --git a/lib/runner.js b/lib/runner.jsindex 9bb25be3..1c04de94 100644--- a/lib/runner.js+++ b/lib/runner.js@@ -156,7 +156,7 @@ Runner.prototype.hook = function(name, fn){var hook = hooks[i];if (!hook) return fn();self.currentRunnable = hook;- hook.context = self.test;+ hook.ctx.test(self.test);self.emit('hook', hook);@@ -265,6 +265,7 @@ Runner.prototype.runTest = function(fn){, self = this;try {+ test.ctx.test(test);test.on('error', function(err){self.fail(test, err);});diff --git a/lib/suite.js b/lib/suite.jsindex 75d19de2..e1e025f4 100644--- a/lib/suite.js+++ b/lib/suite.js@@ -28,7 +28,7 @@ exports = module.exports = Suite;*/exports.create = function(parent, title){- var suite = new Suite(title);+ var suite = new Suite(title, parent.ctx);suite.parent = parent;title = suite.fullTitle();parent.addSuite(suite);@@ -36,14 +36,17 @@ exports.create = function(parent, title){};/**- * Initialize a new `Suite` with the given `title`.+ * Initialize a new `Suite` with the given+ * `title` and `ctx`.** @param {String} title+ * @param {Context} ctx* @api private*/-function Suite(title) {+function Suite(title, ctx) {this.title = title;+ this.ctx = ctx;this.suites = [];this.tests = [];this._beforeEach = [];@@ -119,6 +122,7 @@ Suite.prototype.beforeAll = function(fn){var hook = new Hook('"before all" hook', fn);hook.parent = this;hook.timeout(this.timeout());+ hook.ctx = this.ctx;this._beforeAll.push(hook);this.emit('beforeAll', hook);return this;@@ -136,6 +140,7 @@ Suite.prototype.afterAll = function(fn){var hook = new Hook('"after all" hook', fn);hook.parent = this;hook.timeout(this.timeout());+ hook.ctx = this.ctx;this._afterAll.push(hook);this.emit('afterAll', hook);return this;@@ -153,6 +158,7 @@ Suite.prototype.beforeEach = function(fn){var hook = new Hook('"before each" hook', fn);hook.parent = this;hook.timeout(this.timeout());+ hook.ctx = this.ctx;this._beforeEach.push(hook);this.emit('beforeEach', hook);return this;@@ -170,6 +176,7 @@ Suite.prototype.afterEach = function(fn){var hook = new Hook('"after each" hook', fn);hook.parent = this;hook.timeout(this.timeout());+ hook.ctx = this.ctx;this._afterEach.push(hook);this.emit('afterEach', hook);return this;@@ -203,6 +210,7 @@ Suite.prototype.addSuite = function(suite){Suite.prototype.addTest = function(test){test.parent = this;test.timeout(this.timeout());+ test.ctx = this.ctx;this.tests.push(test);this.emit('test', test);return this;diff --git a/lib/test.js b/lib/test.jsindex 11773e0c..64a2f10e 100644--- a/lib/test.js+++ b/lib/test.js@@ -30,3 +30,20 @@ function Test(title, fn) {*/Test.prototype.__proto__ = Runnable.prototype;++/**+ * Inspect the context void of private properties.+ *+ * @return {String}+ * @api private+ */++Test.prototype.inspect = function(){+ return JSON.stringify(this, function(key, val){+ return '_' == key[0]+ ? undefined+ : 'parent' == key+ ? '#<Suite>'+ : val;+ }, 2);+};\ No newline at end of filediff --git a/mocha.css b/mocha.cssindex 802b3e48..afe870e5 100644--- a/mocha.css+++ b/mocha.css@@ -120,4 +120,10 @@ body {#stats li {list-style: none;-}\ No newline at end of file+}++code .comment { color: #ddd }+code .init { color: #2F6FAD }+code .string { color: #5890AD }+code .keyword { color: #8A6343 }+code .number { color: #2F6FAD }\ No newline at end of filediff --git a/support/tail.js b/support/tail.jsindex 41265b94..8975eb61 100644--- a/support/tail.js+++ b/support/tail.js@@ -56,6 +56,24 @@ window.mocha = require('mocha');, utils = mocha.utils, Reporter = mocha.reporters.HTML+ $(function(){+ $('code').each(function(){+ $(this).html(highlight($(this).text()));+ });+ });++ function highlight(js) {+ return js+ .replace(/</g, '<')+ .replace(/>/g, '>')+ .replace(/\/\/(.*)/gm, '<span class="comment">//$1</span>')+ .replace(/('.*')/gm, '<span class="string">$1</span>')+ .replace(/(\d+\.\d+)/gm, '<span class="number">$1</span>')+ .replace(/(\d+)/gm, '<span class="number">$1</span>')+ .replace(/\bnew *(\w+)/gm, '<span class="keyword">new</span> <span class="init">$1</span>')+ .replace(/\b(function|new|throw|return|var|if|else)\b/gm, '<span class="keyword">$1</span>')+ }+function parse(qs) {return utils.reduce(qs.replace('?', '').split('&'), function(obj, pair){var i = pair.indexOf('=')diff --git a/test/acceptance/context.js b/test/acceptance/context.jsindex b2545856..b4f53638 100644--- a/test/acceptance/context.js+++ b/test/acceptance/context.js@@ -2,8 +2,19 @@describe('Context', function(){var prev;+ before(function(){+ this.age = 2;+ })++ after(function(){+ this.age.should.equal(2);+ this.name.should.equal('Tobi');+ this.inspect().should.equal('{\n "age": 2,\n "name": "Tobi"\n}');+ })+beforeEach(function(){this.name = 'Tobi';+ this.age.should.equal(2);})afterEach(function(){@@ -13,10 +24,11 @@ describe('Context', function(){it('should be maintained between hooks', function(){this.name.should.equal('Tobi');+ this.age.should.equal(2);})- it('should be unique to the test-case', function(){- this.should.not.equal(prev);+ it('should provide the same object', function(){+ this.should.equal(prev);})describe('when async', function(){@@ -24,6 +36,7 @@ describe('Context', function(){beforeEach(function(done){this.name = 'Tobi';+ this.age.should.equal(2);done();})@@ -38,8 +51,8 @@ describe('Context', function(){done();})- it('should be unique to the test-case', function(done){- this.should.not.equal(prev);+ it('should provide the same object', function(done){+ this.should.equal(prev);done();})})diff --git a/test/suite.js b/test/suite.jsindex 2bf0dbef..6d875f4e 100644--- a/test/suite.js+++ b/test/suite.js@@ -1,55 +1,11 @@+var mocha = require('../')+ , Context = mocha.Context, Suite = mocha.Suite, Test = mocha.Test;describe('Suite', function(){- describe('when initialized', function(){- beforeEach(function(){- this.suite = new Suite('A Suite');- });-- it('is initialized with a title', function(){- this.suite.title.should.equal('A Suite');- });-- it('the suites array is empty', function(){- this.suite.suites.should.be.empty;- });-- it('the tests array is empty', function(){- this.suite.tests.should.be.empty;- });-- it('the _beforeEach array is empty', function(){- this.suite._beforeEach.should.be.empty;- });-- it('the _beforeAll array is empty', function(){- this.suite._beforeEach.should.be.empty;- });-- it('the _afterEach array is empty', function(){- this.suite._afterEach.should.be.empty;- });-- it('the _afterAll array is empty', function(){- this.suite._afterAll.should.be.empty;- });-- it('the root is false', function(){- this.suite.root.should.be.false;- });-- it('the _timeout is 2000', function(){- this.suite._timeout.should.equal(2000);- });-- it('the _bail is false', function(){- this.suite._bail.should.be.false;- });- });-- describe('cloning', function(){+ describe('.clone()', function(){beforeEach(function(){this.suite = new Suite('To be cloned');this.suite._timeout = 3043;@@ -62,88 +18,83 @@ describe('Suite', function(){this.suite._afterAll.push(5);});- describe('copies', function(){- it('the title', function(){- this.suite.clone().title.should.equal('To be cloned');- });-- it('the timeout value', function(){- this.suite.clone().timeout().should.equal(3043);- });+ it('should copy the title', function(){+ this.suite.clone().title.should.equal('To be cloned');+ });- it('the bail value', function(){- this.suite.clone().bail().should.be.true;- });+ it('should copy the timeout value', function(){+ this.suite.clone().timeout().should.equal(3043);});- describe('does NOT copy', function(){- it('the values from the suites array', function(){- this.suite.clone().suites.should.be.empty;- });+ it('should copy the bail value', function(){+ this.suite.clone().bail().should.be.true;+ });- it('the values from the tests array', function(){- this.suite.clone().tests.should.be.empty;- });+ it('should not copy the values from the suites array', function(){+ this.suite.clone().suites.should.be.empty;+ });- it('the values from the _beforeEach array', function(){- this.suite.clone()._beforeEach.should.be.empty;- });+ it('should not copy the values from the tests array', function(){+ this.suite.clone().tests.should.be.empty;+ });- it('the values from the _beforeAll array', function(){- this.suite.clone()._beforeAll.should.be.empty;- });+ it('should not copy the values from the _beforeEach array', function(){+ this.suite.clone()._beforeEach.should.be.empty;+ });- it('the values from the _afterEach array', function(){- this.suite.clone()._afterEach.should.be.empty;- });+ it('should not copy the values from the _beforeAll array', function(){+ this.suite.clone()._beforeAll.should.be.empty;+ });- it('the values from the _afterAll array', function(){- this.suite.clone()._afterAll.should.be.empty;- });+ it('should not copy the values from the _afterEach array', function(){+ this.suite.clone()._afterEach.should.be.empty;});+ it('should not copy the values from the _afterAll array', function(){+ this.suite.clone()._afterAll.should.be.empty;+ });});- describe('timeout', function(){+ describe('.timeout()', function(){beforeEach(function(){this.suite = new Suite('A Suite');});describe('when no argument is passed', function(){- it('returns the _timeout value', function(){+ it('should return the timeout value', function(){this.suite.timeout().should.equal(2000);});});describe('when argument is passed', function(){- it('returns the Suite object', function(){+ it('should return the Suite object', function(){var newSuite = this.suite.timeout(5000);newSuite.timeout().should.equal(5000);});});});- describe('bail', function(){+ describe('.bail()', function(){beforeEach(function(){this.suite = new Suite('A Suite');this.suite._bail = true});describe('when no argument is passed', function(){- it('returns the _bail value', function(){+ it('should return the bail value', function(){this.suite.bail().should.be.true;});});describe('when argument is passed', function(){- it('returns the Suite object', function(){+ it('should return the Suite object', function(){var newSuite = this.suite.bail(false);newSuite.bail().should.be.false;});});});- describe('beforeAll', function(){+ describe('.beforeAll()', function(){beforeEach(function(){this.suite = new Suite('A Suite');});@@ -161,7 +112,7 @@ describe('Suite', function(){});});- describe('afterAll', function(){+ describe('.afterAll()', function(){beforeEach(function(){this.suite = new Suite('A Suite');});@@ -179,7 +130,7 @@ describe('Suite', function(){});});- describe('beforeEach', function(){+ describe('.beforeEach()', function(){beforeEach(function(){this.suite = new Suite('A Suite');});@@ -197,7 +148,7 @@ describe('Suite', function(){});});- describe('afterEach', function(){+ describe('.afterEach()', function(){beforeEach(function(){this.suite = new Suite('A Suite');});@@ -215,51 +166,51 @@ describe('Suite', function(){});});- describe('addSuite', function(){+ describe('.addSuite()', function(){beforeEach(function(){- this.suite = new Suite('A Suite');- this.suite.timeout(4002);- this.suiteToAdd = new Suite('Suite to Add');- this.suite.addSuite(this.suiteToAdd);+ this.first = new Suite('First suite');+ this.first.timeout(4002);+ this.second = new Suite('Second suite');+ this.first.addSuite(this.second);});it('sets the parent on the added Suite', function(){- this.suiteToAdd.parent.should.equal(this.suite);+ this.second.parent.should.equal(this.first);});it('copies the timeout value', function(){- this.suiteToAdd.timeout().should.equal(4002);+ this.second.timeout().should.equal(4002);});it('adds the suite to the suites collection', function(){- this.suite.suites.should.have.length(1);- this.suite.suites[0].should.equal(this.suiteToAdd);- });- });-- describe('addTest', function(){- beforeEach(function(){- this.suite = new Suite('A Suite');- this.suite.timeout(4002);- this.test = new Test('Suite to Add');- this.suite.addTest(this.test);- });-- it('sets the parent on the added test', function(){- this.test.parent.should.equal(this.suite);- });-- it('copies the timeout value', function(){- this.test.timeout().should.equal(4002);- });-- it('adds the test to the tests collection', function(){- this.suite.tests.should.have.length(1);- this.suite.tests[0].should.equal(this.test);+ this.first.suites.should.have.length(1);+ this.first.suites[0].should.equal(this.second);});});- describe('fullTitle', function(){+ // describe('.addTest()', function(){+ // beforeEach(function(){+ // this.suite = new Suite('A Suite', new Context);+ // this.suite.timeout(4002);+ // this.test = new Test('test');+ // this.suite.addTest(this.test);+ // });+ //+ // it('sets the parent on the added test', function(){+ // this.test.parent.should.equal(this.suite);+ // });+ //+ // it('copies the timeout value', function(){+ // this.test.timeout().should.equal(4002);+ // });+ //+ // it('adds the test to the tests collection', function(){+ // this.suite.tests.should.have.length(1);+ // this.suite.tests[0].should.equal(this.test);+ // });+ // });++ describe('.fullTitle()', function(){beforeEach(function(){this.suite = new Suite('A Suite');});@@ -279,19 +230,19 @@ describe('Suite', function(){});});- describe('total', function(){+ describe('.total()', function(){beforeEach(function(){this.suite = new Suite('A Suite');});- describe('when there are no other suites or tests under the suite', function(){- it('returns 0', function(){+ describe('when there are no nested suites or tests', function(){+ it('should return 0', function(){this.suite.total().should.equal(0);});});- describe('when there are two tests under the suite', function(){- it('returns 2', function(){+ describe('when there are several tests in the suite', function(){+ it('should return the number', function(){this.suite.addTest(new Test('a child test'));this.suite.addTest(new Test('another child test'));this.suite.total().should.equal(2);