1 | import path from 'path';
|
2 | import fs from 'fs';
|
3 |
|
4 | import { tokens, commonTokens } from '@stryker-mutator/api/plugin';
|
5 | import { Logger } from '@stryker-mutator/api/logging';
|
6 | import { RootHookObject } from 'mocha';
|
7 |
|
8 | import { MochaOptions } from '../src-generated/mocha-runner-options.js';
|
9 |
|
10 | import { LibWrapper } from './lib-wrapper.js';
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | export class MochaAdapter {
|
19 | public static readonly inject = tokens(commonTokens.logger);
|
20 |
|
21 | constructor(private readonly log: Logger) {}
|
22 |
|
23 | public create(options: Mocha.MochaOptions): Mocha {
|
24 | return new LibWrapper.Mocha(options);
|
25 | }
|
26 |
|
27 | public collectFiles(options: MochaOptions): string[] {
|
28 | const originalProcessExit = process.exit;
|
29 | try {
|
30 |
|
31 |
|
32 | (process as any).exit = () => {};
|
33 | const files = LibWrapper.collectFiles!(options);
|
34 | return files;
|
35 | } finally {
|
36 | process.exit = originalProcessExit;
|
37 | }
|
38 | }
|
39 |
|
40 | public async handleRequires(requires: string[]): Promise<RootHookObject | undefined> {
|
41 | this.log.trace('Resolving requires %s', requires);
|
42 | if (LibWrapper.handleRequires) {
|
43 | this.log.trace('Using `handleRequires`');
|
44 | const rawRootHooks = await LibWrapper.handleRequires(requires);
|
45 | if (rawRootHooks) {
|
46 | if (LibWrapper.loadRootHooks) {
|
47 |
|
48 | return await LibWrapper.loadRootHooks(rawRootHooks);
|
49 | } else {
|
50 | return (rawRootHooks as { rootHooks: RootHookObject }).rootHooks;
|
51 | }
|
52 | }
|
53 | } else {
|
54 | const modulesToRequire = requires.map((moduleName) => {
|
55 | const maybeSetupFileName = path.resolve(moduleName);
|
56 | if (fs.existsSync(maybeSetupFileName)) {
|
57 | return maybeSetupFileName;
|
58 | } else {
|
59 | return moduleName;
|
60 | }
|
61 | });
|
62 | this.log.trace('Requiring %s manually', modulesToRequire);
|
63 | modulesToRequire.forEach(LibWrapper.require);
|
64 | }
|
65 | return undefined;
|
66 | }
|
67 | }
|