From 74a9896f6412f1c869c605c289ec951b40da962c Mon Sep 17 00:00:00 2001 From: Pascal Date: Tue, 28 May 2019 12:05:27 -0400 Subject: [PATCH] added test and fix for duplicate globals --- lib/mocha.js | 8 +++++++- test/unit/mocha.spec.js | 8 ++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/mocha.js b/lib/mocha.js index aab5604448..e09aeba097 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -553,7 +553,13 @@ Mocha.prototype._growl = growl.notify; Mocha.prototype.globals = function(globals) { this.options.globals = (this.options.globals || []) .concat(globals) - .filter(Boolean); + .filter(Boolean) + .reduce(function dedupe(memo, item) { + if (memo.indexOf(item) < 0) { + memo.push(item); + } + return memo; + }, []); return this; }; diff --git a/test/unit/mocha.spec.js b/test/unit/mocha.spec.js index 16fbbd7f82..5a4ac2e701 100644 --- a/test/unit/mocha.spec.js +++ b/test/unit/mocha.spec.js @@ -174,6 +174,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); + }); }); });