UNPKG

1.88 kBPlain TextView Raw
1import path from 'path';
2
3import { createRequire } from 'module';
4
5import Mocha, { type RootHookObject } from 'mocha';
6
7import { MochaOptions } from '../src-generated/mocha-runner-options.js';
8
9const require = createRequire(import.meta.url);
10const mochaRoot = path.dirname(require.resolve('mocha/package.json'));
11// https://github.com/mochajs/mocha/blob/master/lib/cli/run-helpers.js#L132
12
13// eslint-disable-next-line @typescript-eslint/no-require-imports
14const runHelpers = require(`${mochaRoot}/lib/cli/run-helpers`);
15
16let 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
24const loadOptions: (argv?: string[] | string) => Record<string, any> | undefined = require(`${mochaRoot}/lib/cli/options`).loadOptions;
25
26const handleRequires: (requires?: string[]) => Promise<RootHookObject> = runHelpers.handleRequires;
27const loadRootHooks: ((rootHooks: any) => Promise<any>) | undefined = runHelpers.loadRootHooks; // loadRootHooks is available since mocha v7.2 and removed again in 8.0
28
29collectFiles = runHelpers.handleFiles;
30if (!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 */
39export 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