Skip to content

Commit

Permalink
feat(build): add a helper to merge mocha config objects
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Bajtoš <mbajtoss@gmail.com>
  • Loading branch information
bajtos committed Jun 26, 2020
1 parent b2a72a3 commit 3ce9eef
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ exports.typeScriptPath = path.resolve(
require.resolve('typescript/package.json'),
'..',
);

exports.mergeMochaConfigs = require('./src/merge-mocha-configs');
1 change: 1 addition & 0 deletions packages/build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"eslint": "^7.3.1",
"fs-extra": "^9.0.1",
"glob": "^7.1.6",
"lodash": "^4.17.15",
"mocha": "^8.0.1",
"nyc": "^15.1.0",
"prettier": "^2.0.5",
Expand Down
39 changes: 39 additions & 0 deletions packages/build/src/merge-mocha-configs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright IBM Corp. 2017,2020. All Rights Reserved.
// Node module: @loopback/build
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

'use strict';

const debug = require('debug')('loopback:build:merge-mocha-configs');
const {assignWith} = require('lodash');

module.exports = mergeMochaConfigs;

/**
* Merge multiple Mocha configuration files into a single one.
*
* @param {MochaConfig[]} configs A list of Mocha configuration objects
* as provided by `.mocharc.js` files.
*/
function mergeMochaConfigs(...configs) {
debug('Merging mocha configurations', ...configs);
const result = assignWith({}, ...configs, assignMochaConfigEntry);
debug('Merged config:', result);
return result;
}

function assignMochaConfigEntry(targetValue, sourceValue, key) {
switch (key) {
case 'timeout':
return Math.max(targetValue || 0, sourceValue);
case 'require':
if (Array.isArray(sourceValue)) {
debug('Adding an array of files to require:', sourceValue);
return [...(targetValue || []), ...sourceValue];
} else {
debug('Adding a single file to require:', sourceValue);
return [...(targetValue || []), sourceValue];
}
}
}

0 comments on commit 3ce9eef

Please sign in to comment.