-
Notifications
You must be signed in to change notification settings - Fork 3
/
step-manager.js
99 lines (81 loc) · 1.75 KB
/
step-manager.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
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
const Runner = require('./runner');
module.exports = class StepManager {
constructor({
generator,
args,
deepEqual,
}) {
if (!generator) {
throw new Error(`ExpectGen requires an generator, passed in ${generator}`);
}
this.generator = generator;
this.deepEqual = deepEqual;
this.args = args;
this.steps = [];
}
yields(expectedValue, result) {
this.steps.push({
result,
expectedValue,
stack: getCallStack('yields'),
});
return this;
}
catches(error, expectedValue) {
this.steps.push({
error,
expectedValue,
stack: getCallStack('catches'),
});
return this;
}
throws(error) {
this.steps.push({
error,
expectedThrow: true,
stack: getCallStack('throws'),
});
return this;
}
catchesAndFinishes(error, expectedValue) {
this.steps.push({
error,
expectedValue,
expectedDone: true,
stack: getCallStack('catchesAndFinishes'),
});
return this;
}
next(result) {
this.steps.push({
result,
stack: getCallStack('next'),
});
return this;
}
finishes(expectedValue) {
this.steps.push({
expectedValue,
expectedDone: true,
stack: getCallStack('finishes'),
});
return this;
}
run(context = null) {
const it = this.generator.apply(context, this.args);
const runner = new Runner(it, this.steps, this.deepEqual);
return runner.run();
}
toJSON(context = null) {
return JSON.parse(JSON.stringify(this.run(context)));
}
}
const getCallStack = (message) => {
const err = new Error(message);
if (!err || !err.stack) return message;
const stack = err.stack
.split('\n')
.slice(2, 10)
.join('\n');
return stack;
}