1 | import path from 'path';
|
2 |
|
3 | import { createRequire } from 'module';
|
4 |
|
5 | import Mocha, { type RootHookObject } from 'mocha';
|
6 |
|
7 | import { MochaOptions } from '../src-generated/mocha-runner-options.js';
|
8 |
|
9 | const require = createRequire(import.meta.url);
|
10 | const mochaRoot = path.dirname(require.resolve('mocha/package.json'));
|
11 |
|
12 |
|
13 |
|
14 | const runHelpers = require(`${mochaRoot}/lib/cli/run-helpers`);
|
15 |
|
16 | let collectFiles: ((options: MochaOptions) => string[]) | undefined;
|
17 |
|
18 | /*
|
19 | * If read, object containing parsed arguments
|
20 | * @since 6.0.0'
|
21 | * @see https://mochajs.org/api/module-lib_cli_options.html#.loadOptions
|
22 | */
|
23 | // eslint-disable-next-line @typescript-eslint/no-require-imports
|
24 | const loadOptions: (argv?: string[] | string) => Record<string, any> | undefined = require(`${mochaRoot}/lib/cli/options`).loadOptions;
|
25 |
|
26 | const handleRequires: (requires?: string[]) => Promise<RootHookObject> = runHelpers.handleRequires;
|
27 | const loadRootHooks: ((rootHooks: any) => Promise<any>) | undefined = runHelpers.loadRootHooks; // loadRootHooks is available since mocha v7.2 and removed again in 8.0
|
28 |
|
29 | collectFiles = runHelpers.handleFiles;
|
30 | if (!collectFiles) {
|
31 | // Might be moved: https://github.com/mochajs/mocha/commit/15b96afccaf508312445770e3af1c145d90b28c6#diff-39b692a81eb0c9f3614247af744ab4a8
|
32 | // eslint-disable-next-line @typescript-eslint/no-require-imports
|
33 | collectFiles = require(`${mochaRoot}/lib/cli/collect-files`);
|
34 | }
|
35 |
|
36 | /**
|
37 | * Wraps Mocha class and require for testability
|
38 | */
|
39 | export class LibWrapper {
|
40 | public static Mocha = Mocha;
|
41 | public static require = require;
|
42 | public static loadOptions = loadOptions;
|
43 | public static collectFiles = collectFiles;
|
44 | public static handleRequires = handleRequires;
|
45 | public static loadRootHooks = loadRootHooks;
|
46 | }
|
47 |
|
\ | No newline at end of file |