From 59dece3f98e7a17c2a5850597c8c6f4b05f9987b Mon Sep 17 00:00:00 2001 From: Pascal Date: Mon, 13 May 2019 17:51:12 -0400 Subject: [PATCH 1/9] accept options.global or options.globals --- lib/mocha.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mocha.js b/lib/mocha.js index aab5604448..9828ac148d 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -114,7 +114,7 @@ function Mocha(options) { .useColors(options.color) .slow(options.slow) .useInlineDiffs(options.inlineDiffs) - .globals(options.globals); + .globals(options.global || options.globals); if ('enableTimeouts' in options) { utils.deprecate( From 738fcdaa919a22e5f8c7dded9b0b5777e472583c Mon Sep 17 00:00:00 2001 From: Pascal Date: Mon, 13 May 2019 19:53:06 -0400 Subject: [PATCH 2/9] added tests for options.global and options.globals --- test/unit/mocha.spec.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/test/unit/mocha.spec.js b/test/unit/mocha.spec.js index 16fbbd7f82..d27d57078c 100644 --- a/test/unit/mocha.spec.js +++ b/test/unit/mocha.spec.js @@ -21,6 +21,7 @@ describe('Mocha', function() { sandbox.stub(Mocha.prototype, 'useColors').returnsThis(); sandbox.stub(utils, 'deprecate'); sandbox.stub(Mocha.prototype, 'timeout').returnsThis(); + sandbox.stub(Mocha.prototype, 'globals').returnsThis(); }); describe('when "useColors" option is defined', function() { @@ -64,6 +65,36 @@ describe('Mocha', function() { ); }); }); + + describe('when options.global is provided', function() { + it('should pass options.global to #globals()', function() { + // eslint-disable-next-line no-new + new Mocha({global: ['singular']}); + expect(Mocha.prototype.globals, 'to have a call satisfying', [ + ['singular'] + ]).and('was called once'); + }); + }); + + describe('when options.globals is provided', function() { + it('should pass options.globals to #globals()', function() { + // eslint-disable-next-line no-new + new Mocha({globals: ['plural']}); + expect(Mocha.prototype.globals, 'to have a call satisfying', [ + ['plural'] + ]).and('was called once'); + }); + }); + + describe('when options.global AND options.globals are provided', function() { + it('should pass options.global to #globals(), ignoring options.globals', function() { + // eslint-disable-next-line no-new + new Mocha({global: ['singular'], globals: ['plural']}); + expect(Mocha.prototype.globals, 'to have a call satisfying', [ + ['singular'] + ]).and('was called once'); + }); + }); }); describe('#allowUncaught()', function() { From 48e776d24f9599c5e10010e0e8b80cff7e9aebaf Mon Sep 17 00:00:00 2001 From: Pascal Date: Tue, 14 May 2019 08:59:15 -0400 Subject: [PATCH 3/9] skip new tests to verify test failure --- 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 d27d57078c..b215595770 100644 --- a/test/unit/mocha.spec.js +++ b/test/unit/mocha.spec.js @@ -66,7 +66,7 @@ describe('Mocha', function() { }); }); - describe('when options.global is provided', function() { + describe.skip('when options.global is provided', function() { it('should pass options.global to #globals()', function() { // eslint-disable-next-line no-new new Mocha({global: ['singular']}); @@ -76,7 +76,7 @@ describe('Mocha', function() { }); }); - describe('when options.globals is provided', function() { + describe.skip('when options.globals is provided', function() { it('should pass options.globals to #globals()', function() { // eslint-disable-next-line no-new new Mocha({globals: ['plural']}); @@ -86,7 +86,7 @@ describe('Mocha', function() { }); }); - describe('when options.global AND options.globals are provided', function() { + describe.skip('when options.global AND options.globals are provided', function() { it('should pass options.global to #globals(), ignoring options.globals', function() { // eslint-disable-next-line no-new new Mocha({global: ['singular'], globals: ['plural']}); From d85f580d4ae2d77ba24d8fbaa215b2d4ea05005a Mon Sep 17 00:00:00 2001 From: Pascal Date: Tue, 14 May 2019 09:13:01 -0400 Subject: [PATCH 4/9] re-enable options.global tests --- 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 b215595770..d27d57078c 100644 --- a/test/unit/mocha.spec.js +++ b/test/unit/mocha.spec.js @@ -66,7 +66,7 @@ describe('Mocha', function() { }); }); - describe.skip('when options.global is provided', function() { + describe('when options.global is provided', function() { it('should pass options.global to #globals()', function() { // eslint-disable-next-line no-new new Mocha({global: ['singular']}); @@ -76,7 +76,7 @@ describe('Mocha', function() { }); }); - describe.skip('when options.globals is provided', function() { + describe('when options.globals is provided', function() { it('should pass options.globals to #globals()', function() { // eslint-disable-next-line no-new new Mocha({globals: ['plural']}); @@ -86,7 +86,7 @@ describe('Mocha', function() { }); }); - describe.skip('when options.global AND options.globals are provided', function() { + describe('when options.global AND options.globals are provided', function() { it('should pass options.global to #globals(), ignoring options.globals', function() { // eslint-disable-next-line no-new new Mocha({global: ['singular'], globals: ['plural']}); From 9b8b611849f1800f6e1dbbe630e1cd4cc8862d48 Mon Sep 17 00:00:00 2001 From: Pascal Date: Wed, 29 May 2019 09:56:12 -0400 Subject: [PATCH 5/9] consume options.global or options.globals in the constructor. filter unique values --- lib/mocha.js | 14 +++++++++++--- test/unit/mocha.spec.js | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/mocha.js b/lib/mocha.js index 9828ac148d..f3d88b5afa 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -106,6 +106,11 @@ function Mocha(options) { options.color = 'color' in options ? options.color : options.useColors; } + // Globals are passed in as options.global, with options.globals for backward + // compatibility. Globals are stored internally at this.options.globals. + options.globals = options.global || options.globals || []; + delete options.global; + this.grep(options.grep) .fgrep(options.fgrep) .ui(options.ui) @@ -114,7 +119,7 @@ function Mocha(options) { .useColors(options.color) .slow(options.slow) .useInlineDiffs(options.inlineDiffs) - .globals(options.global || options.globals); + .globals(options.globals); if ('enableTimeouts' in options) { utils.deprecate( @@ -551,9 +556,12 @@ Mocha.prototype._growl = growl.notify; * mocha.globals(['jQuery', 'MyLib']); */ Mocha.prototype.globals = function(globals) { - this.options.globals = (this.options.globals || []) + this.options.globals = this.options.globals .concat(globals) - .filter(Boolean); + .filter(Boolean) + .filter(function(elt, idx, arr) { + return arr.indexOf(elt) === idx; + }); return this; }; diff --git a/test/unit/mocha.spec.js b/test/unit/mocha.spec.js index d27d57078c..3e6871ce1c 100644 --- a/test/unit/mocha.spec.js +++ b/test/unit/mocha.spec.js @@ -74,6 +74,10 @@ describe('Mocha', function() { ['singular'] ]).and('was called once'); }); + it('should delete mocha.options.global', function() { + var mocha = new Mocha({global: ['singular']}); + expect(mocha.options.global, 'to be', undefined); + }); }); describe('when options.globals is provided', function() { @@ -94,6 +98,10 @@ describe('Mocha', function() { ['singular'] ]).and('was called once'); }); + it('should delete mocha.options.global', function() { + var mocha = new Mocha({global: ['singular'], globals: ['plural']}); + expect(mocha.options.global, 'to be', undefined); + }); }); }); @@ -205,6 +213,14 @@ describe('Mocha', function() { expect(mocha.options.globals, 'to contain', elem, elem2); expect(mocha.options.globals, 'to have length', elems.length); }); + + it('should not have duplicates', function() { + var mocha = new Mocha({globals: [elem, elem2]}); + var elems = [elem, elem2]; + mocha.globals(elems); + expect(mocha.options.globals, 'to contain', elem, elem2); + expect(mocha.options.globals, 'to have length', elems.length); + }); }); }); From f65b75e95fd3da6877314a6eecdac4b33e85f466 Mon Sep 17 00:00:00 2001 From: Pascal Date: Wed, 29 May 2019 11:17:34 -0400 Subject: [PATCH 6/9] fixed API doc link --- lib/mocha.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mocha.js b/lib/mocha.js index f3d88b5afa..2723719e1a 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -545,7 +545,7 @@ Mocha.prototype._growl = growl.notify; * Specifies whitelist of variable names to be expected in global scope. * * @public - * @see {@link https://mochajs.org/#--globals-names|CLI option} + * @see {@link https://mochajs.org/#-global-variable-name|CLI option} * @see {@link Mocha#checkLeaks} * @param {String[]|String} globals - Accepted global variable name(s). * @return {Mocha} this From f973cc0e77559bdb6c648f94b9339e2f3354a961 Mon Sep 17 00:00:00 2001 From: Pascal Date: Thu, 30 May 2019 09:27:26 -0400 Subject: [PATCH 7/9] update comment --- lib/mocha.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/mocha.js b/lib/mocha.js index 2723719e1a..c0a5aa6dd9 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -106,8 +106,7 @@ function Mocha(options) { options.color = 'color' in options ? options.color : options.useColors; } - // Globals are passed in as options.global, with options.globals for backward - // compatibility. Globals are stored internally at this.options.globals. + // Globals are passed in as options.global, with options.globals for backward compatibility. options.globals = options.global || options.globals || []; delete options.global; From e1a79a64d5ea75388dfedbd81701d53eddd9eb37 Mon Sep 17 00:00:00 2001 From: Pascal Date: Thu, 30 May 2019 09:27:41 -0400 Subject: [PATCH 8/9] added third element to test addition --- test/unit/mocha.spec.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/unit/mocha.spec.js b/test/unit/mocha.spec.js index 3e6871ce1c..fa023720b9 100644 --- a/test/unit/mocha.spec.js +++ b/test/unit/mocha.spec.js @@ -198,6 +198,7 @@ describe('Mocha', function() { describe('when argument is valid', function() { var elem = 'foo'; var elem2 = 'bar'; + var elem3 = 'baz'; it('should add string to the whitelist', function() { var mocha = new Mocha(opts); @@ -216,9 +217,9 @@ describe('Mocha', function() { it('should not have duplicates', function() { var mocha = new Mocha({globals: [elem, elem2]}); - var elems = [elem, elem2]; + var elems = [elem, elem2, elem3]; mocha.globals(elems); - expect(mocha.options.globals, 'to contain', elem, elem2); + expect(mocha.options.globals, 'to contain', elem, elem2, elem3); expect(mocha.options.globals, 'to have length', elems.length); }); }); From 04f65263d744f92795a6d45bad834d412cb89f41 Mon Sep 17 00:00:00 2001 From: Pascal Date: Thu, 30 May 2019 12:34:30 -0400 Subject: [PATCH 9/9] added quotes around options.global and options.globals --- test/unit/mocha.spec.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/unit/mocha.spec.js b/test/unit/mocha.spec.js index fa023720b9..ed99a3e257 100644 --- a/test/unit/mocha.spec.js +++ b/test/unit/mocha.spec.js @@ -66,8 +66,8 @@ describe('Mocha', function() { }); }); - describe('when options.global is provided', function() { - it('should pass options.global to #globals()', function() { + describe('when "options.global" is provided', function() { + it('should pass "options.global" to #globals()', function() { // eslint-disable-next-line no-new new Mocha({global: ['singular']}); expect(Mocha.prototype.globals, 'to have a call satisfying', [ @@ -80,8 +80,8 @@ describe('Mocha', function() { }); }); - describe('when options.globals is provided', function() { - it('should pass options.globals to #globals()', function() { + describe('when "options.globals" is provided', function() { + it('should pass "options.globals" to #globals()', function() { // eslint-disable-next-line no-new new Mocha({globals: ['plural']}); expect(Mocha.prototype.globals, 'to have a call satisfying', [ @@ -90,8 +90,8 @@ describe('Mocha', function() { }); }); - describe('when options.global AND options.globals are provided', function() { - it('should pass options.global to #globals(), ignoring options.globals', function() { + describe('when "options.global" AND "options.globals" are provided', function() { + it('should pass "options.global" to #globals(), ignoring "options.globals"', function() { // eslint-disable-next-line no-new new Mocha({global: ['singular'], globals: ['plural']}); expect(Mocha.prototype.globals, 'to have a call satisfying', [