This repository has been archived by the owner on Oct 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extend-expect.js
59 lines (53 loc) · 1.41 KB
/
extend-expect.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
'use strict';
function generateSuggestion(idx, expected, history) {
let output = '';
if (idx >= 0) {
let n = 0;
while (n <= idx) {
const [_, arg] = history[n];
const nextSugg = (n === idx ? ` expect(itr).toYieldValue(${JSON.stringify(expected)});\n` : ` itr.next(${arg === undefined ? '' : arg});\n`);
output += nextSugg;
n++;
}
return output;
}
}
function toYieldValue(iter, expected) {
const nextStep = iter.next();
let foundIdx;
const history = iter.history.map(
([val, arg], idx) => {
const equalsExpected = this.equals(expected, val);
if (equalsExpected && !foundIdx) {
foundIdx = idx;
}
return `${equalsExpected ? ">" : " "} ${idx + 1}: ${val}`
}
);
const suggestion = foundIdx >= 0 ? `\nsuggestion:\n${generateSuggestion(foundIdx, expected, iter.history)}` : '';
const pass = this.equals(nextStep.value, expected);
return {
pass,
message: () => `expected: ${expected}
received: ${nextStep.value}
history:
${history.join("\n")}
${suggestion}`
};
}
function iteratorWithHistory(itr) {
const history = [];
return {
history,
next: value => {
const output = itr.next(value);
history.push([output.value, value]);
return output;
}
};
}
var extensions = /*#__PURE__*/Object.freeze({
toYieldValue: toYieldValue,
iteratorWithHistory: iteratorWithHistory
});
expect.extend(extensions);