-
-
Notifications
You must be signed in to change notification settings - Fork 720
/
bdd.js
61 lines (53 loc) · 1.26 KB
/
bdd.js
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
const {
CucumberExpression,
ParameterTypeRegistry,
ParameterType,
} = require('cucumber-expressions');
let steps = {};
const STACK_POSITION = 2;
/**
* @param {*} step
* @param {*} fn
*/
const addStep = (step, fn) => {
const stack = (new Error()).stack;
steps[step] = fn;
fn.line = stack && stack.split('\n')[STACK_POSITION];
if (fn.line) fn.line = fn.line.trim().replace(/^at (.*?)\(/, '(');
};
const parameterTypeRegistry = new ParameterTypeRegistry();
const matchStep = (step) => {
for (const stepName in steps) {
if (stepName.indexOf('/') === 0) {
const res = step.match(new RegExp(stepName.slice(1, -1)));
if (res) {
const fn = steps[stepName];
fn.params = res.slice(1);
return fn;
}
continue;
}
const expression = new CucumberExpression(stepName, parameterTypeRegistry);
const res = expression.match(step);
if (res) {
const fn = steps[stepName];
fn.params = res.map(arg => arg.getValue());
return fn;
}
}
throw new Error(`No steps matching "${step.toString()}"`);
};
const clearSteps = () => {
steps = {};
};
const getSteps = () => {
return steps;
};
module.exports = {
Given: addStep,
When: addStep,
Then: addStep,
matchStep,
getSteps,
clearSteps,
};