forked from pryv/open-pryv.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.mocharc.js
More file actions
97 lines (87 loc) · 2.96 KB
/
.mocharc.js
File metadata and controls
97 lines (87 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* @license
* Copyright (C) Pryv https://pryv.com
* This file is part of Pryv.io and released under BSD-Clause-3 License
* Refer to LICENSE file
*/
/**
* Base Mocha configuration for all components
*
* Environment variables:
* - MOCHA_PARALLEL=1: Enable parallel test execution
* - MOCHA_NON_PARALLEL=1: Run only tests excluded from parallel mode
*
* Usage in component .mocharc.js:
* const { createConfig } = require('../../.mocharc.js');
* module.exports = createConfig({
* require: 'test/helpers.js', // optional
* timeout: 5000, // optional override
* nonParallelTests: [...] // optional exclusion list
* });
*/
const os = require('os');
const isParallel = process.env.MOCHA_PARALLEL === '1';
const isNonParallelOnly = process.env.MOCHA_NON_PARALLEL === '1';
// Default to `cpuCount - 1` jobs so high-core CI / dev machines actually
// scale, while leaving 1 core for the OS + supporting daemons (PG, Mongo,
// rqlite). Overridable per-component via `createConfig({ parallelJobs: N })`
// or globally via `MOCHA_JOBS=N` env var. Lower bound = 2 (parallel mode
// with 1 worker is pointless).
const cpuCount = os.cpus().length;
const envJobs = parseInt(process.env.MOCHA_JOBS || '', 10);
const defaultParallelJobs = Number.isFinite(envJobs) && envJobs > 0
? envJobs
: Math.max(2, cpuCount - 1);
// Base configuration
const baseConfig = {
exit: true,
slow: 75,
timeout: 2000,
ui: 'bdd',
diff: true,
reporter: 'dot',
spec: 'test/**/*.test.js'
};
/**
* Create a mocha config for a component
* @param {Object} options - Configuration options
* @param {string} [options.require] - Module to require before tests
* @param {number} [options.timeout] - Test timeout in ms
* @param {number} [options.slow] - Slow test threshold in ms
* @param {string[]} [options.nonParallelTests] - Tests to exclude from parallel mode
* @param {number} [options.parallelJobs] - Number of parallel jobs (default: 2)
* @returns {Object} Mocha configuration
*/
function createConfig (options = {}) {
const {
require: requireModule,
timeout = baseConfig.timeout,
slow = baseConfig.slow,
nonParallelTests = [],
parallelJobs = defaultParallelJobs
} = options;
const config = {
...baseConfig,
slow,
timeout: isParallel ? timeout * 2 : timeout
};
if (requireModule) {
config.require = Array.isArray(requireModule) ? requireModule : [requireModule];
}
if (isNonParallelOnly && nonParallelTests.length > 0) {
// Run only the non-parallel tests
config.spec = nonParallelTests;
} else if (isParallel) {
config.parallel = true;
config.jobs = parallelJobs;
if (nonParallelTests.length > 0) {
config.ignore = nonParallelTests;
}
}
return config;
}
// Export both the base config (for direct use) and the factory function
module.exports = baseConfig;
module.exports.createConfig = createConfig;
module.exports.isParallel = isParallel;
module.exports.isNonParallelOnly = isNonParallelOnly;