/
MochaAdapter.ts
118 lines (97 loc) · 3.32 KB
/
MochaAdapter.ts
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { LogicError } from '@serenity-js/core';
import type { TestRunnerAdapter } from '@serenity-js/core/lib/adapter';
import type { ModuleLoader } from '@serenity-js/core/lib/io';
import type { Outcome } from '@serenity-js/core/lib/model';
import { ExecutionIgnored, ImplementationPending } from '@serenity-js/core/lib/model';
import * as fs from 'fs';
import * as path from 'path'; // eslint-disable-line unicorn/import-style
import type { MochaConfig } from './MochaConfig';
import type Mocha = require('mocha');
/**
* Allows for programmatic execution of Mocha test scenarios,
* using {@apilink SerenityReporterForMocha} to report progress.
*
* ## Learn more
* - {@apilink TestRunnerAdapter}
*
* @group Integration
*/
export class MochaAdapter implements TestRunnerAdapter {
private mocha: Mocha;
private totalScenarios: number;
constructor(
private readonly config: MochaConfig,
private readonly loader: ModuleLoader,
) {
}
/**
* Scenario success threshold for this test runner.
*/
successThreshold(): Outcome | { Code: number } {
return this.config.strict
? ExecutionIgnored
: ImplementationPending;
}
/**
* Loads test scenarios.
*
* @param pathsToScenarios
*/
async load(pathsToScenarios: string[]): Promise<void> {
const _Mocha = this.loader.require('mocha');
this.mocha = new _Mocha({
...this.config,
reporter: require.resolve('../index'),
});
this.mocha.fullTrace();
this.mocha.files = pathsToScenarios;
this.mocha.suite.on('pre-require', (context: Mocha.MochaGlobals, file: string, mocha: Mocha) => {
this.requireAny(this.config.require);
});
await this.mocha.loadFilesAsync()
const mochaRunner = new _Mocha.Runner(this.mocha.suite, {
delay: false,
});
if (this.config.grep) {
mochaRunner.grep(this.mocha.options.grep as RegExp, this.config.invert);
}
this.totalScenarios = mochaRunner.total;
}
/**
* Returns the number of loaded scenarios
*
* @throws {@apilink LogicError}
* If called before `load`
*/
scenarioCount(): number {
if (this.totalScenarios === undefined) {
throw new LogicError('Make sure to call `load` before calling `scenarioCount`');
}
return this.totalScenarios;
}
/**
* Runs loaded test scenarios.
*
* @throws {@apilink LogicError}
* If called before `load`
*/
run(): Promise<void> {
return new Promise((resolve, reject) => {
if (this.mocha === undefined) {
throw new LogicError('Make sure to call `load` before calling `run`');
}
this.mocha.run(numberOfFailures => resolve())
});
}
private requireAny(filesOrModules: string | string[]) {
const requires = filesOrModules
? [].concat(filesOrModules).filter(item => !! item)
: [];
requires.forEach(fileOrModule => {
const required = fs.existsSync(fileOrModule) || fs.existsSync(`${ fileOrModule }.js`)
? path.resolve(fileOrModule) // local file
: fileOrModule; // module
require(required);
});
}
}