-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
143 lines (116 loc) · 3.99 KB
/
index.mjs
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import fs from 'fs';
import path from 'path';
import { ArgParser } from './src/arg-parser.mjs';
import { SpecParser } from './src/spec-parser.mjs';
import { StepBuilder } from './src/step-builder.mjs';
/**
* @typedef {{ steps(mapper: (regEx: RegExp, func: Function) => void): void }} StepModule
*/
export class JestSpec {
/**
* Constructor
* @param {boolean} [verbose]
* @param {any} [context]
*/
constructor(verbose, context) {
// Sensible defaults
this.verbose = verbose ?? false;
this.stepMap = [];
this.missingSteps = 0;
this.context = context;
}
/**
* Adds a step to the step map
* @param {RegExp} regex
* @param {Function} func
*/
map(regex, func) {
this.stepMap.push({
regex: regex,
func: func
});
}
/**
* Adds step definitions from the supplied module
* @param {StepModule} stepModule
*/
addSteps(stepModule) {
const _this = this;
stepModule.steps((regex, func) => _this.map(regex, func));
}
/**
* Parses feature text
* @param {string} text
* @returns
*/
async parse(text) {
const testMap = [];
const parser = new SpecParser();
const feature = parser.parse(text);
this.verbose && console.log('Structured feature', feature);
for (const scenario of feature.scenarios) {
const testItem = {
feature: feature.name,
scenario: scenario.name,
steps: []
};
for (const step of scenario.steps) {
let stepFound = false;
const argumentParser = new ArgParser(step);
this.stepMap.forEach((val) => {
const regexMatch = val.regex.exec(step);
if (regexMatch && regexMatch.length > 0) {
const args = [
null
];
for (let i = 1; i < regexMatch.length; i++) {
args.push(regexMatch[i]);
}
stepFound = true;
testItem.steps.push({
name: step,
func: val.func,
args: argumentParser.getArgs(val.regex, args)
});
}
});
if (!stepFound) {
this.missingSteps++;
const codeBuilder = new StepBuilder(argumentParser);
console.error('Missing step. Consider adding code:\n', codeBuilder.getSuggestedStepMethod());
}
}
testMap.push(testItem);
}
this.verbose && console.log('Test map', testMap);
return testMap;
}
/**
* Runs a specification based on a relative path, i.e. /src/specifications/Feature.feature
* @param {string} spec
*/
async run(spec) {
const featurePath = path.join(process.cwd(), spec);
const text = fs.readFileSync(featurePath, {encoding:'utf8', flag:'r'});
const tests = await this.parse(text);
if (this.missingSteps > 0) {
throw new Error(`${this.missingSteps} missing steps`);
}
for (const test of tests) {
this.verbose && console.log('Running Scenario', test.feature, test.scenario, test.steps.length);
// Context used to pass state between steps
let context = {
feature: test.feature,
scenario: test.scenario
};
for (key in this.context) {
context[key] = this.context[key];
}
for (const step of test.steps) {
step.args[0] = context;
this.verbose && console.log('Running step', step.name);
context = await step.func.apply(null, step.args);
}
}
}
}