From 15b5bd732afb1d85c843e0bfe6d8462e0cefc83f Mon Sep 17 00:00:00 2001 From: Paul Roebuck Date: Sat, 16 Feb 2019 08:34:34 -0600 Subject: [PATCH 1/7] fix(lib/mocha.js): Remove falsy values from `.globals` Added filter to remove "undefined"/empty string values. --- lib/mocha.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/mocha.js b/lib/mocha.js index 9707f4c66d..2b2dc13341 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -543,7 +543,9 @@ Mocha.prototype._growl = growl.notify; * mocha.globals(['jQuery', 'MyLib']); */ Mocha.prototype.globals = function(globals) { - this.options.globals = (this.options.globals || []).concat(globals); + this.options.globals = (this.options.globals || []) + .concat(globals) + .filter(Boolean); return this; }; From 89a723d0a211d5f81f78f8db48f2682475ff676b Mon Sep 17 00:00:00 2001 From: Paul Roebuck Date: Sat, 16 Feb 2019 08:36:52 -0600 Subject: [PATCH 2/7] test(unit/mocha.spec.js): Add test for `.globals` --- test/unit/mocha.spec.js | 48 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/test/unit/mocha.spec.js b/test/unit/mocha.spec.js index 73c74f6b8b..dbbf79c6d1 100644 --- a/test/unit/mocha.spec.js +++ b/test/unit/mocha.spec.js @@ -5,7 +5,7 @@ var Mocha = require('../../lib/mocha'); var sinon = require('sinon'); describe('Mocha', function() { - var opts = {reporter: function() {}}; // no output + var opts = {reporter: utils.noop}; // no output var sandbox; beforeEach(function() { @@ -135,6 +135,52 @@ describe('Mocha', function() { }); }); + describe('.globals()', function() { + it('should be an empty array initially', function() { + var mocha = new Mocha(); + expect(mocha.options.globals, 'to be empty'); + }); + + it('should be chainable', function() { + var mocha = new Mocha(opts); + expect(mocha.globals(), 'to be', mocha); + }); + + describe('when argument is invalid', function() { + it('should not add empty string to the whitelist', function() { + var mocha = new Mocha(opts); + mocha.globals(''); + expect(mocha.options.globals, 'to be empty'); + }); + + it('should not add empty array to the whitelist', function() { + var mocha = new Mocha(opts); + mocha.globals([]); + expect(mocha.options.globals, 'to be empty'); + }); + }); + + describe('when argument is valid', function() { + var elem = 'foo'; + var elem2 = 'bar'; + + it('should add string to the whitelist', function() { + var mocha = new Mocha(opts); + mocha.globals(elem); + expect(mocha.options.globals, 'to contain', elem); + expect(mocha.options.globals, 'to have length', 1); + }); + + it('should add string array to the whitelist', function() { + var mocha = new Mocha(opts); + var elems = [elem, elem2]; + mocha.globals(elems); + expect(mocha.options.globals, 'to contain', elem, elem2); + expect(mocha.options.globals, 'to have length', elems.length); + }); + }); + }); + describe('.growl()', function() { describe('if capable of notifications', function() { it('should set the growl option to true', function() { From fe903464207adb1f486f8e437b94c0f45ee32dc8 Mon Sep 17 00:00:00 2001 From: Paul Roebuck Date: Sat, 16 Feb 2019 08:49:05 -0600 Subject: [PATCH 3/7] refactor(unit/mocha.spec.js): Reorder option tests lexically **No changes to actual code.** Just put `describe`s in order. --- test/unit/mocha.spec.js | 156 ++++++++++++++++++++-------------------- 1 file changed, 78 insertions(+), 78 deletions(-) diff --git a/test/unit/mocha.spec.js b/test/unit/mocha.spec.js index dbbf79c6d1..80093ea880 100644 --- a/test/unit/mocha.spec.js +++ b/test/unit/mocha.spec.js @@ -41,84 +41,55 @@ describe('Mocha', function() { }); }); - describe('.run(fn)', function() { - it('should not raise errors if callback was not provided', function() { - sandbox.stub(Mocha.Runner.prototype, 'run'); + describe('.allowUncaught()', function() { + it('should set the allowUncaught option to true', function() { var mocha = new Mocha(opts); - expect(function() { - mocha.run(); - }, 'not to throw'); + mocha.allowUncaught(); + expect(mocha.options, 'to have property', 'allowUncaught', true); }); - it('should execute the callback when complete', function(done) { + it('should be chainable', function() { var mocha = new Mocha(opts); - sandbox.stub(Mocha.Runner.prototype, 'run').callsArg(0); - mocha.run(done); - }); - }); - - describe('.reporter("xunit").run(fn)', function() { - it('should not raise errors if callback was not provided', function() { - var mocha = new Mocha(); - expect(function() { - try { - mocha.reporter('xunit').run(); - } catch (e) { - console.log(e); - expect.fail(e.message); - } - }, 'not to throw'); + expect(mocha.allowUncaught(), 'to be', mocha); }); }); - describe('.invert()', function() { - it('should set the invert option to true', function() { + describe('.bail()', function() { + it('should set the suite._bail to true if there is no arguments', function() { var mocha = new Mocha(opts); - mocha.invert(); - expect(mocha.options, 'to have property', 'invert', true); + mocha.bail(); + expect(mocha.suite._bail, 'to be', true); }); it('should be chainable', function() { var mocha = new Mocha(opts); - expect(mocha.invert(), 'to be', mocha); + expect(mocha.bail(), 'to be', mocha); }); }); - describe('.ignoreLeaks()', function() { - it('should set the ignoreLeaks option to true when param equals true', function() { - var mocha = new Mocha(opts); - mocha.ignoreLeaks(true); - expect(mocha.options, 'to have property', 'ignoreLeaks', true); - }); - - it('should set the ignoreLeaks option to false when param equals false', function() { - var mocha = new Mocha(opts); - mocha.ignoreLeaks(false); - expect(mocha.options, 'to have property', 'ignoreLeaks', false); - }); - - it('should set the ignoreLeaks option to false when the param is undefined', function() { + describe('.checkLeaks()', function() { + it('should set the ignoreLeaks option to false', function() { var mocha = new Mocha(opts); - mocha.ignoreLeaks(); + mocha.checkLeaks(); expect(mocha.options, 'to have property', 'ignoreLeaks', false); }); it('should be chainable', function() { var mocha = new Mocha(opts); - expect(mocha.ignoreLeaks(), 'to be', mocha); + expect(mocha.checkLeaks(), 'to be', mocha); }); }); - describe('.checkLeaks()', function() { - it('should set the ignoreLeaks option to false', function() { + describe('.delay()', function() { + it('should set the delay option to true', function() { var mocha = new Mocha(opts); - mocha.checkLeaks(); - expect(mocha.options, 'to have property', 'ignoreLeaks', false); + mocha.delay(); + expect(mocha.options, 'to have property', 'delay', true); }); it('should be chainable', function() { var mocha = new Mocha(opts); - expect(mocha.checkLeaks(), 'to be', mocha); + expect(mocha.delay(), 'to be', mocha); }); }); @@ -210,28 +181,41 @@ describe('Mocha', function() { }); }); - describe('.useInlineDiffs()', function() { - it('should set the useInlineDiffs option to true when param equals true', function() { + describe('.ignoreLeaks()', function() { + it('should set the ignoreLeaks option to true when param equals true', function() { var mocha = new Mocha(opts); - mocha.useInlineDiffs(true); - expect(mocha.options, 'to have property', 'useInlineDiffs', true); + mocha.ignoreLeaks(true); + expect(mocha.options, 'to have property', 'ignoreLeaks', true); }); - it('should set the useInlineDiffs option to false when param equals false', function() { + it('should set the ignoreLeaks option to false when param equals false', function() { var mocha = new Mocha(opts); - mocha.useInlineDiffs(false); - expect(mocha.options, 'to have property', 'useInlineDiffs', false); + mocha.ignoreLeaks(false); + expect(mocha.options, 'to have property', 'ignoreLeaks', false); }); - it('should set the useInlineDiffs option to false when the param is undefined', function() { + it('should set the ignoreLeaks option to false when the param is undefined', function() { var mocha = new Mocha(opts); - mocha.useInlineDiffs(); - expect(mocha.options, 'to have property', 'useInlineDiffs', false); + mocha.ignoreLeaks(); + expect(mocha.options, 'to have property', 'ignoreLeaks', false); }); it('should be chainable', function() { var mocha = new Mocha(opts); - expect(mocha.useInlineDiffs(), 'to be', mocha); + expect(mocha.ignoreLeaks(), 'to be', mocha); + }); + }); + + describe('.invert()', function() { + it('should set the invert option to true', function() { + var mocha = new Mocha(opts); + mocha.invert(); + expect(mocha.options, 'to have property', 'invert', true); + }); + + it('should be chainable', function() { + var mocha = new Mocha(opts); + expect(mocha.invert(), 'to be', mocha); }); }); @@ -249,42 +233,58 @@ describe('Mocha', function() { }); }); - describe('.allowUncaught()', function() { - it('should set the allowUncaught option to true', function() { + describe('.reporter("xunit").run(fn)', function() { + it('should not raise errors if callback was not provided', function() { + var mocha = new Mocha(); + expect(function() { + try { + mocha.reporter('xunit').run(); + } catch (e) { + console.log(e); + expect.fail(e.message); + } + }, 'not to throw'); + }); + }); + + describe('.run(fn)', function() { + it('should not raise errors if callback was not provided', function() { + sandbox.stub(Mocha.Runner.prototype, 'run'); var mocha = new Mocha(opts); - mocha.allowUncaught(); - expect(mocha.options, 'to have property', 'allowUncaught', true); + expect(function() { + mocha.run(); + }, 'not to throw'); }); - it('should be chainable', function() { + it('should execute the callback when complete', function(done) { var mocha = new Mocha(opts); - expect(mocha.allowUncaught(), 'to be', mocha); + sandbox.stub(Mocha.Runner.prototype, 'run').callsArg(0); + mocha.run(done); }); }); - describe('.delay()', function() { - it('should set the delay option to true', function() { + describe('.useInlineDiffs()', function() { + it('should set the useInlineDiffs option to true when param equals true', function() { var mocha = new Mocha(opts); - mocha.delay(); - expect(mocha.options, 'to have property', 'delay', true); + mocha.useInlineDiffs(true); + expect(mocha.options, 'to have property', 'useInlineDiffs', true); }); - it('should be chainable', function() { + it('should set the useInlineDiffs option to false when param equals false', function() { var mocha = new Mocha(opts); - expect(mocha.delay(), 'to be', mocha); + mocha.useInlineDiffs(false); + expect(mocha.options, 'to have property', 'useInlineDiffs', false); }); - }); - describe('.bail()', function() { - it('should set the suite._bail to true if there is no arguments', function() { + it('should set the useInlineDiffs option to false when the param is undefined', function() { var mocha = new Mocha(opts); - mocha.bail(); - expect(mocha.suite._bail, 'to be', true); + mocha.useInlineDiffs(); + expect(mocha.options, 'to have property', 'useInlineDiffs', false); }); it('should be chainable', function() { var mocha = new Mocha(opts); - expect(mocha.bail(), 'to be', mocha); + expect(mocha.useInlineDiffs(), 'to be', mocha); }); }); From d11e5f5b3ae583596acd17250b6b32a0701b6b44 Mon Sep 17 00:00:00 2001 From: Paul Roebuck Date: Sat, 16 Feb 2019 11:49:16 -0600 Subject: [PATCH 4/7] test(unit/mocha.spec.js): Annotate prototype tests Changed `describe` titles from `.method` to `#method` for Mocha prototypes. --- test/unit/mocha.spec.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/test/unit/mocha.spec.js b/test/unit/mocha.spec.js index 80093ea880..932e20b2dc 100644 --- a/test/unit/mocha.spec.js +++ b/test/unit/mocha.spec.js @@ -41,7 +41,7 @@ describe('Mocha', function() { }); }); - describe('.allowUncaught()', function() { + describe('#allowUncaught()', function() { it('should set the allowUncaught option to true', function() { var mocha = new Mocha(opts); mocha.allowUncaught(); @@ -54,7 +54,7 @@ describe('Mocha', function() { }); }); - describe('.bail()', function() { + describe('#bail()', function() { it('should set the suite._bail to true if there is no arguments', function() { var mocha = new Mocha(opts); mocha.bail(); @@ -67,7 +67,7 @@ describe('Mocha', function() { }); }); - describe('.checkLeaks()', function() { + describe('#checkLeaks()', function() { it('should set the ignoreLeaks option to false', function() { var mocha = new Mocha(opts); mocha.checkLeaks(); @@ -80,7 +80,7 @@ describe('Mocha', function() { }); }); - describe('.delay()', function() { + describe('#delay()', function() { it('should set the delay option to true', function() { var mocha = new Mocha(opts); mocha.delay(); @@ -93,7 +93,7 @@ describe('Mocha', function() { }); }); - describe('.fullTrace()', function() { + describe('#fullTrace()', function() { it('should set the fullStackTrace option to true', function() { var mocha = new Mocha(opts); mocha.fullTrace(); @@ -106,7 +106,7 @@ describe('Mocha', function() { }); }); - describe('.globals()', function() { + describe('#globals()', function() { it('should be an empty array initially', function() { var mocha = new Mocha(); expect(mocha.options.globals, 'to be empty'); @@ -152,7 +152,7 @@ describe('Mocha', function() { }); }); - describe('.growl()', function() { + describe('#growl()', function() { describe('if capable of notifications', function() { it('should set the growl option to true', function() { var mocha = new Mocha(opts); @@ -181,7 +181,7 @@ describe('Mocha', function() { }); }); - describe('.ignoreLeaks()', function() { + describe('#ignoreLeaks()', function() { it('should set the ignoreLeaks option to true when param equals true', function() { var mocha = new Mocha(opts); mocha.ignoreLeaks(true); @@ -206,7 +206,7 @@ describe('Mocha', function() { }); }); - describe('.invert()', function() { + describe('#invert()', function() { it('should set the invert option to true', function() { var mocha = new Mocha(opts); mocha.invert(); @@ -219,7 +219,7 @@ describe('Mocha', function() { }); }); - describe('.noHighlighting()', function() { + describe('#noHighlighting()', function() { // :NOTE: Browser-only option... it('should set the noHighlighting option to true', function() { var mocha = new Mocha(opts); @@ -233,7 +233,7 @@ describe('Mocha', function() { }); }); - describe('.reporter("xunit").run(fn)', function() { + describe('#reporter("xunit")#run(fn)', function() { it('should not raise errors if callback was not provided', function() { var mocha = new Mocha(); expect(function() { @@ -247,7 +247,7 @@ describe('Mocha', function() { }); }); - describe('.run(fn)', function() { + describe('#run(fn)', function() { it('should not raise errors if callback was not provided', function() { sandbox.stub(Mocha.Runner.prototype, 'run'); var mocha = new Mocha(opts); @@ -263,7 +263,7 @@ describe('Mocha', function() { }); }); - describe('.useInlineDiffs()', function() { + describe('#useInlineDiffs()', function() { it('should set the useInlineDiffs option to true when param equals true', function() { var mocha = new Mocha(opts); mocha.useInlineDiffs(true); From 442205f25ac93cc1ec56e6abe5f16ac80c662a2b Mon Sep 17 00:00:00 2001 From: Paul Roebuck Date: Sat, 16 Feb 2019 11:54:37 -0600 Subject: [PATCH 5/7] refactor(unit/mocha.spec.js): Rename suite title 'error handling' as '#reporter()' Renamed suite and migrated lexically. Added its chainable test. --- test/unit/mocha.spec.js | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/test/unit/mocha.spec.js b/test/unit/mocha.spec.js index 932e20b2dc..616c424999 100644 --- a/test/unit/mocha.spec.js +++ b/test/unit/mocha.spec.js @@ -233,6 +233,26 @@ describe('Mocha', function() { }); }); + describe('#reporter()', function() { + it('should throw reporter error if an invalid reporter is given', function() { + var updatedOpts = {reporter: 'invalidReporter', reporterOptions: {}}; + var throwError = function() { + // eslint-disable-next-line no-new + new Mocha(updatedOpts); + }; + expect(throwError, 'to throw', { + message: "invalid reporter 'invalidReporter'", + code: 'ERR_MOCHA_INVALID_REPORTER', + reporter: 'invalidReporter' + }); + }); + + it('should be chainable', function() { + var mocha = new Mocha(opts); + expect(mocha.reporter(), 'to be', mocha); + }); + }); + describe('#reporter("xunit")#run(fn)', function() { it('should not raise errors if callback was not provided', function() { var mocha = new Mocha(); @@ -287,19 +307,4 @@ describe('Mocha', function() { expect(mocha.useInlineDiffs(), 'to be', mocha); }); }); - - describe('error handling', function() { - it('should throw reporter error if an invalid reporter is given', function() { - var updatedOpts = {reporter: 'invalidReporter', reporterOptions: {}}; - var throwError = function() { - // eslint-disable-next-line no-new - new Mocha(updatedOpts); - }; - expect(throwError, 'to throw', { - message: "invalid reporter 'invalidReporter'", - code: 'ERR_MOCHA_INVALID_REPORTER', - reporter: 'invalidReporter' - }); - }); - }); }); From bf9939bee5a4c623259dd370f6cd1399a12d0013 Mon Sep 17 00:00:00 2001 From: Paul Roebuck Date: Sat, 16 Feb 2019 12:00:32 -0600 Subject: [PATCH 6/7] refactor(unit/mocha.spec.js): Reparented '#reporter("xunit")#run(fn)' test suite Moved aforementioned suite inside '#run()' test suite. Don't see its value as a variant of the original test, but... --- test/unit/mocha.spec.js | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/test/unit/mocha.spec.js b/test/unit/mocha.spec.js index 616c424999..4355260769 100644 --- a/test/unit/mocha.spec.js +++ b/test/unit/mocha.spec.js @@ -253,21 +253,13 @@ describe('Mocha', function() { }); }); - describe('#reporter("xunit")#run(fn)', function() { - it('should not raise errors if callback was not provided', function() { - var mocha = new Mocha(); - expect(function() { - try { - mocha.reporter('xunit').run(); - } catch (e) { - console.log(e); - expect.fail(e.message); - } - }, 'not to throw'); + describe('#run(fn)', function() { + it('should execute the callback when complete', function(done) { + var mocha = new Mocha(opts); + sandbox.stub(Mocha.Runner.prototype, 'run').callsArg(0); + mocha.run(done); }); - }); - describe('#run(fn)', function() { it('should not raise errors if callback was not provided', function() { sandbox.stub(Mocha.Runner.prototype, 'run'); var mocha = new Mocha(opts); @@ -276,10 +268,19 @@ describe('Mocha', function() { }, 'not to throw'); }); - it('should execute the callback when complete', function(done) { - var mocha = new Mocha(opts); - sandbox.stub(Mocha.Runner.prototype, 'run').callsArg(0); - mocha.run(done); + describe('#reporter("xunit")#run(fn)', function() { + // :TBD: Why does specifying reporter differentiate this test from preceding one + it('should not raise errors if callback was not provided', function() { + var mocha = new Mocha(); + expect(function() { + try { + mocha.reporter('xunit').run(); + } catch (e) { + console.log(e); + expect.fail(e.message); + } + }, 'not to throw'); + }); }); }); From c638cbe994aa60633c1b77d5e2a822ce7066a827 Mon Sep 17 00:00:00 2001 From: Paul Roebuck Date: Sat, 16 Feb 2019 12:33:18 -0600 Subject: [PATCH 7/7] test(unit/mocha.spec.js): Change verbage of test titles for `#globals` --- test/unit/mocha.spec.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/unit/mocha.spec.js b/test/unit/mocha.spec.js index 4355260769..e9065c03e8 100644 --- a/test/unit/mocha.spec.js +++ b/test/unit/mocha.spec.js @@ -118,13 +118,13 @@ describe('Mocha', function() { }); describe('when argument is invalid', function() { - it('should not add empty string to the whitelist', function() { + it('should not modify the whitelist when given empty string', function() { var mocha = new Mocha(opts); mocha.globals(''); expect(mocha.options.globals, 'to be empty'); }); - it('should not add empty array to the whitelist', function() { + it('should not modify the whitelist when given empty array', function() { var mocha = new Mocha(opts); mocha.globals([]); expect(mocha.options.globals, 'to be empty'); @@ -142,7 +142,7 @@ describe('Mocha', function() { expect(mocha.options.globals, 'to have length', 1); }); - it('should add string array to the whitelist', function() { + it('should add contents of string array to the whitelist', function() { var mocha = new Mocha(opts); var elems = [elem, elem2]; mocha.globals(elems);