forked from codeceptjs/CodeceptJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep.js
301 lines (269 loc) · 7.85 KB
/
step.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// TODO: place MetaStep in other file, disable rule
/* eslint-disable max-classes-per-file */
const store = require('./store');
const Secret = require('./secret');
const event = require('./event');
const STACK_LINE = 4;
/**
* Each command in test executed through `I.` object is wrapped in Step.
* Step allows logging executed commands and triggers hook before and after step execution.
* @param {CodeceptJS.Helper} helper
* @param {string} name
*/
class Step {
static get TIMEOUT_ORDER() {
return {
/**
* timeouts set with order below zero only override timeouts of higher order if their value is smaller
*/
testOrSuite: -5,
/**
* 0-9 - designated for override of timeouts set from code, 5 is used by stepTimeout plugin when stepTimeout.config.overrideStepLimits=true
*/
stepTimeoutHard: 5,
/**
* 10-19 - designated for timeouts set from code, 15 is order of I.setTimeout(t) operation
*/
codeLimitTime: 15,
/**
* 20-29 - designated for timeout settings which could be overriden in tests code, 25 is used by stepTimeout plugin when stepTimeout.config.overrideStepLimits=false
*/
stepTimeoutSoft: 25,
};
}
constructor(helper, name) {
/** @member {string} */
this.actor = 'I'; // I = actor
/** @member {CodeceptJS.Helper} */
this.helper = helper; // corresponding helper
/** @member {string} */
this.name = name; // name of a step console
/** @member {string} */
this.helperMethod = name; // helper method
/** @member {string} */
this.status = 'pending';
/**
* @member {string} suffix
* @memberof CodeceptJS.Step#
*/
/** @member {string} */
this.prefix = this.suffix = '';
/** @member {string} */
this.comment = '';
/** @member {Array<*>} */
this.args = [];
/** @member {MetaStep} */
this.metaStep = undefined;
/** @member {string} */
this.stack = '';
const timeouts = new Map();
/**
* @method
* @returns {number|undefined}
*/
this.getTimeout = function () {
let totalTimeout;
// iterate over all timeouts starting from highest values of order
new Map([...timeouts.entries()].sort().reverse()).forEach((timeout, order) => {
if (timeout !== undefined && (
// when orders >= 0 - timeout value overrides those set with higher order elements
order >= 0
// when `order < 0 && totalTimeout === undefined` - timeout is used when nothing is set by elements with higher order
|| totalTimeout === undefined
// when `order < 0` - timeout overrides higher values of timeout or 'no timeout' (totalTimeout === 0) set by elements with higher order
|| timeout > 0 && (timeout < totalTimeout || totalTimeout === 0)
)) {
totalTimeout = timeout;
}
});
return totalTimeout;
};
/**
* @method
* @param {number} timeout - timeout in milliseconds or 0 if no timeout
* @param {number} order - order defines the priority of timeout, timeouts set with lower order override those set with higher order.
* When order below 0 value of timeout only override if new value is lower
*/
this.setTimeout = function (timeout, order) {
timeouts.set(order, timeout);
};
this.setTrace();
}
/** @function */
setTrace() {
Error.captureStackTrace(this);
}
/** @param {Array<*>} args */
setArguments(args) {
this.args = args;
}
/**
* @param {...any} args
* @return {*}
*/
run() {
this.args = Array.prototype.slice.call(arguments);
if (store.dryRun) {
this.setStatus('success');
return Promise.resolve(new Proxy({}, dryRunResolver()));
}
let result;
try {
result = this.helper[this.helperMethod].apply(this.helper, this.args);
this.setStatus('success');
} catch (err) {
this.setStatus('failed');
throw err;
}
return result;
}
/** @param {string} status */
setStatus(status) {
this.status = status;
if (this.metaStep) {
this.metaStep.setStatus(status);
}
}
/** @return {string} */
humanize() {
return this.name
// insert a space before all caps
.replace(/([A-Z])/g, ' $1')
// _ chars to spaces
.replace('_', ' ')
// uppercase the first character
.replace(/^(.)|\s(.)/g, $1 => $1.toLowerCase());
}
/** @return {string} */
humanizeArgs() {
return this.args.map((arg) => {
if (!arg) {
return '';
}
if (typeof arg === 'string') {
return `"${arg}"`;
}
if (Array.isArray(arg)) {
try {
const res = JSON.stringify(arg);
return res;
} catch (err) {
return `[${arg.toString()}]`;
}
} else if (typeof arg === 'function') {
return arg.toString();
} else if (typeof arg === 'undefined') {
return `${arg}`;
} else if (arg instanceof Secret) {
return arg.getMasked();
} else if (arg.toString && arg.toString() !== '[object Object]') {
return arg.toString();
} else if (typeof arg === 'object') {
return JSON.stringify(arg);
}
return arg;
}).join(', ');
}
/** @return {string} */
line() {
const lines = this.stack.split('\n');
if (lines[STACK_LINE]) {
return lines[STACK_LINE].trim().replace(global.codecept_dir || '', '.').trim();
}
return '';
}
/** @return {string} */
toString() {
return `${this.prefix}${this.actor} ${this.humanize()} ${this.humanizeArgs()}${this.suffix}`;
}
/** @return {string} */
toCode() {
return `${this.prefix}${this.actor}.${this.name}(${this.humanizeArgs()})${this.suffix}`;
}
isMetaStep() {
return this.constructor.name === 'MetaStep';
}
/** @return {boolean} */
hasBDDAncestor() {
let hasBDD = false;
let processingStep;
processingStep = this;
while (processingStep.metaStep) {
if (processingStep.metaStep.actor.match(/^(Given|When|Then|And)/)) {
hasBDD = true;
break;
} else {
processingStep = processingStep.metaStep;
}
}
return hasBDD;
}
}
/** @extends Step */
class MetaStep extends Step {
constructor(obj, method) {
super(null, method);
this.actor = obj;
}
/** @return {boolean} */
isBDD() {
if (this.actor && this.actor.match && this.actor.match(/^(Given|When|Then|And)/)) {
return true;
}
return false;
}
isWithin() {
if (this.actor && this.actor.match && this.actor.match(/^(Within)/)) {
return true;
}
return false;
}
toString() {
const actorText = !this.isBDD() && !this.isWithin() ? `${this.actor}:` : this.actor;
return `${this.prefix}${actorText} ${this.humanize()} ${this.humanizeArgs()}${this.suffix}`;
}
humanize() {
return this.name;
}
setTrace() {
}
setContext(context) {
this.context = context;
}
/** @return {*} */
run(fn) {
this.status = 'queued';
this.setArguments(Array.from(arguments).slice(1));
let result;
const registerStep = (step) => {
this.metaStep = null;
step.metaStep = this;
};
event.dispatcher.prependListener(event.step.before, registerStep);
let rethrownError = null;
try {
this.startTime = Date.now();
result = fn.apply(this.context, this.args);
} catch (error) {
this.setStatus('failed');
rethrownError = error;
} finally {
this.endTime = Date.now();
event.dispatcher.removeListener(event.step.before, registerStep);
}
if (rethrownError) { throw rethrownError; }
return result;
}
}
Step.TIMEOUTS = {};
/** @type {Class<MetaStep>} */
Step.MetaStep = MetaStep;
module.exports = Step;
function dryRunResolver() {
return {
get(target, prop) {
if (prop === 'toString') return () => '<VALUE>';
return new Proxy({}, dryRunResolver());
},
};
}