1 | import { URL } from 'url';
|
2 | import fs from 'fs';
|
3 |
|
4 | import { MochaOptions } from '../src-generated/mocha-runner-options.js';
|
5 |
|
6 | const mochaSchema: typeof import('../schema/mocha-runner-options.json') = JSON.parse(
|
7 | fs.readFileSync(new URL('../schema/mocha-runner-options.json', import.meta.url), 'utf-8')
|
8 | );
|
9 |
|
10 | export function serializeMochaLoadOptionsArguments(mochaOptions: MochaOptions): string[] {
|
11 | const args: string[] = [];
|
12 | if (mochaOptions['no-config']) {
|
13 | args.push('--no-config');
|
14 | }
|
15 | if (mochaOptions['no-opts']) {
|
16 | args.push('--no-opts');
|
17 | }
|
18 | if (mochaOptions['no-package']) {
|
19 | args.push('--no-package');
|
20 | }
|
21 | if (mochaOptions.package) {
|
22 | args.push('--package');
|
23 | args.push(mochaOptions.package);
|
24 | }
|
25 | if (mochaOptions.opts) {
|
26 | args.push('--opts');
|
27 | args.push(mochaOptions.opts);
|
28 | }
|
29 | if (mochaOptions.config) {
|
30 | args.push('--config');
|
31 | args.push(mochaOptions.config);
|
32 | }
|
33 | return args;
|
34 | }
|
35 |
|
36 | const SUPPORTED_MOCHA_OPTIONS = Object.freeze(Object.keys(mochaSchema.properties.mochaOptions.properties));
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | export function filterConfig(rawConfig: Record<string, any>): Partial<MochaOptions> {
|
43 | const options: Partial<MochaOptions> = {};
|
44 | Object.keys(rawConfig)
|
45 | .filter((rawOption) => SUPPORTED_MOCHA_OPTIONS.some((supportedOption) => rawOption === supportedOption))
|
46 | .forEach((option) => ((options as any)[option] = rawConfig[option]));
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | if (rawConfig._?.length) {
|
53 | if (!options.spec) {
|
54 | options.spec = [];
|
55 | }
|
56 | const specs = options.spec;
|
57 | rawConfig._.forEach((positionalArgument: string) => specs.push(positionalArgument));
|
58 | }
|
59 | return options;
|
60 | }
|