This repository has been archived by the owner on Nov 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unexpectedGenerator.test.js
183 lines (162 loc) · 4.82 KB
/
unexpectedGenerator.test.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
import unexpectedGenerator from './unexpectedGenerator';
function makeIterator(array) {
var nextIndex = 0;
return {
next: function () {
return nextIndex < array.length
? { value: array[nextIndex++], done: false }
: { done: true };
},
};
}
function* argGenerator(max) {
let count = 0;
while (count < max) {
// Increments counter with the number next() is called with
count += yield count;
}
return;
}
function* accepterGenerator() {
yield Promise.resolve();
}
describe('unexpected-generators plugin', () => {
it('is an unexpected plugin', () => {
return expect(unexpectedGenerator, 'to satisfy', {
name: 'unexpected-generators',
installInto: expect.it('to be a function'),
});
});
describe('iterators', () => {
let testExpect;
let testIterator;
beforeEach(() => {
testExpect = expect.clone().use(unexpectedGenerator);
testIterator = makeIterator(['this', 'is', 'a', 'test']);
});
it('<iterator> to yield item <any>', () =>
expect(
() => testExpect(testIterator, 'to yield item', 'this'),
'not to error',
));
it('<iterator> to yield item <any>, diff', () =>
expect(
() => testExpect(testIterator, 'to yield item', 'is'),
'to error',
"expected iterator to yield item 'is'\n\n-this\n+is",
));
it('<iterator> to yield item <any>, iterator done', () => {
testIterator.next();
testIterator.next();
testIterator.next();
testIterator.next();
return expect(
() => testExpect(testIterator, 'to yield item', 'anything'),
'to error',
"Iterator was expected to yield 'anything' but was done",
);
});
it('<iterator> to yield items <array>', () =>
expect(
() =>
testExpect(testIterator, 'to yield items', [
'this',
'is',
'a',
'test',
]),
'not to error',
));
it('<iterator> to yield items <array>, subject too short', () =>
expect(
() =>
testExpect(testIterator, 'to yield items', [
'far ',
'too',
'long',
'this',
'is',
'a',
'test',
]),
'to error',
'Iterator was expected to yield at least 7 values but was done after 4',
));
it('<iterator> to yield items <array>, diff', () =>
expect(
() =>
testExpect(testIterator, 'to yield items', [
'not',
'really',
'a',
'test',
]),
'to error',
"expected iterator to yield items [ 'not', 'really', 'a', 'test' ]\n" +
'\n' +
'[\n' +
" 'this', // should equal 'not'\n" +
' //\n' +
' // -this\n' +
' // +not\n' +
" 'is', // should equal 'really'\n" +
' //\n' +
' // -is\n' +
' // +really\n' +
" 'a',\n" +
" 'test'\n" +
']',
));
it('<iterator> yielding with <any>', () =>
expect(
() => testExpect(argGenerator(4), 'yielding with', 2),
'not to error',
).then((value) => expect(value, 'to be', 0)));
it('<iterator> yielding with <any> <assertion>', () =>
expect(
() => testExpect(argGenerator(4), 'yielding with', 2, 'to be', 0),
'not to error',
));
it('<iterator> yielding with <any>, iterator done', () =>
expect(
() => testExpect(argGenerator(0), 'yielding with', 2),
'to error',
'Iterator was expected to yield but was done',
));
it('<iterator> to be done, pass', () =>
expect(() => testExpect(argGenerator(0), 'to be done'), 'not to error'));
it('<iterator> to be done, fail', () =>
expect(
() => testExpect(argGenerator(5), 'to be done'),
'to error',
'expected iterator to be done',
));
it('<iterator> not to be done, pass', () =>
expect(
() => testExpect(argGenerator(5), 'not to be done'),
'not to error',
));
it('<iterator> not to be done, fail', () =>
expect(
() => testExpect(argGenerator(0), 'not to be done'),
'to error',
'expected iterator not to be done',
));
it('<iterator> to be done with <any>, pass', () =>
expect(
() =>
testExpect(
accepterGenerator(),
'to yield item',
testExpect.it('to be a', 'Promise'),
).and('to be done with', 2),
'not to error',
));
it('<iterator> to be done with <any>, fail', () =>
expect(
() => testExpect(accepterGenerator(), 'to be done with', 2),
'to error',
'expected iterator to be done with 2',
));
});
});