UNPKG

1.28 kBJavaScriptView Raw
1// Copyright IBM Corp. and LoopBack contributors 2017,2020. All Rights Reserved.
2// Node module: @loopback/build
3// This file is licensed under the MIT License.
4// License text available at https://opensource.org/licenses/MIT
5
6'use strict';
7
8const debug = require('debug')('loopback:build:merge-mocha-configs');
9const {assignWith} = require('lodash');
10
11module.exports = mergeMochaConfigs;
12
13/**
14 * Merge multiple Mocha configuration files into a single one.
15 *
16 * @param {MochaConfig[]} configs A list of Mocha configuration objects
17 * as provided by `.mocharc.js` files.
18 */
19function mergeMochaConfigs(...configs) {
20 debug('Merging mocha configurations', ...configs);
21 const result = assignWith({}, ...configs, assignMochaConfigEntry);
22 debug('Merged config:', result);
23 return result;
24}
25
26function assignMochaConfigEntry(targetValue, sourceValue, key) {
27 switch (key) {
28 case 'timeout':
29 return Math.max(targetValue || 0, sourceValue);
30 case 'require':
31 if (Array.isArray(sourceValue)) {
32 debug('Adding an array of files to require:', sourceValue);
33 return [...(targetValue || []), ...sourceValue];
34 } else {
35 debug('Adding a single file to require:', sourceValue);
36 return [...(targetValue || []), sourceValue];
37 }
38 }
39}