-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
214 lines (178 loc) · 6.42 KB
/
test.ts
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
import stub from './index';
import {Assert} from 'zora';
export default (t: Assert) => {
t.test(`create stub with generator`, t => {
t.test('when a value is yielded the stub should return that value', t => {
const fn = stub(function* () {
yield 'woot';
});
t.eq(fn('hello world'), 'woot');
t.eq(fn.calls, [['hello world']]);
});
t.test(`when several values are yielded, the stub should sequentially return the values`, t => {
const fn = stub(function* () {
yield 'woot';
yield 5;
yield {foo: 'bar'};
});
t.eq(fn('first', 'arg'), 'woot');
t.eq(fn(2), 5);
t.eq(fn(null, 'arg'), {foo: 'bar'});
t.eq(fn.calls, [
['first', 'arg'],
[2],
[null, 'arg']
]);
});
t.test(`when an error is thrown, the stub should throw`, t => {
const fn = stub(function* () {
throw new Error('message');
});
try {
fn('abc');
t.fail('should not get here');
} catch (e) {
t.eq(e.message, 'message');
t.eq(fn.calls, [
['abc']
]);
}
});
t.test(`when a a value is yielded in an async generator, the stub should behave as an async function`, async t => {
const fn = stub(async function* () {
yield [1, 2, 3];
});
const val = await fn(42);
t.eq(val, [1, 2, 3]);
t.eq(fn.calls, [[42]]);
});
t.test(`when a promise is yielded, the stub should behave as an async function`, async t => {
const fn = stub(function* () {
yield Promise.resolve([1, 2, 3]);
});
const val = await fn(42);
t.eq(val, [1, 2, 3]);
t.eq(fn.calls, [[42]]);
});
t.test(`when a promise rejection is yielded, the stub should behave as an async function`, async t => {
const fn = stub(function* () {
yield Promise.reject(new Error('some message'));
});
try {
// @ts-ignore
const val = await fn(42);
t.fail(`should not get here`);
} catch (e) {
t.eq(e.message, 'some message');
t.eq(fn.calls, [[42]]);
}
});
t.test(`when an error is thrown in a async generator, the stub should behave as an async function`, async t => {
const fn = stub(async function* () {
throw new Error('some message');
});
try {
await fn('a', 2);
t.fail(`should not get here`);
} catch (e) {
t.eq(e.message, 'some message');
t.eq(fn.calls, [
['a', 2]
]);
}
});
t.test(`stub should throw if a it is called more than expected`, t => {
const fn = stub(function* () {
yield 'bim';
});
const val = fn('arg');
t.eq(val, 'bim');
try {
fn('out of bound');
t.fail(`should not get here`);
} catch (e) {
t.eq(e.message, `stub exhausted, call not expected`);
t.eq(fn.calls, [
['arg'],
['out of bound']
]);
}
});
});
t.test(`create stub with provided value`, t => {
t.test('The stub should return that value', t => {
const fn = stub('woot');
t.eq(fn('hello world'), 'woot');
t.eq(fn.calls, [
['hello world']
]);
});
t.test(`when several values are provided, the stub should sequentially return the values`, t => {
const fn = stub('woot', 5, {foo: 'bar'});
t.eq(fn('first', 'arg'), 'woot');
t.eq(fn(2), 5);
t.eq(fn(null, 'arg'), {foo: 'bar'});
t.eq(fn.calls, [
['first', 'arg'],
[2],
[null, 'arg']
]);
});
t.test(`When the value is a Promise, the stub should behave as an async function`, async t => {
const fn = stub(Promise.resolve([1, 2, 3]));
const val = await fn(42);
t.eq(val, [1, 2, 3]);
t.eq(fn.calls, [[42]]);
});
t.test(`when a promise rejection is passed, the stub should behave as an async function`, async t => {
const fn = stub(Promise.reject(new Error(`some message`)));
try {
await fn('a', 2);
t.fail(`should not get here`);
} catch (e) {
t.eq(e.message, 'some message');
t.eq(fn.calls, [
['a', 2]
]);
}
});
});
t.test(`create stub with no parameters`, t => {
t.test(`Return(value) should make the stub return that value`, t => {
const fn = stub().return(42);
t.eq(fn(1), 42);
fn.return('woot');
t.eq(fn({woot: true}), 'woot');
t.eq(fn.calls, [
[1],
[{woot: true}]
]);
});
t.test(`resolve(value) should make the stub return that value as if it was an asynchronous function`, async t => {
const fn = stub().resolve(42);
const val = await fn(1, 2, 3);
t.eq(val, 42);
t.eq(fn.calls, [
[1, 2, 3]
]);
});
t.test(`reject(reason) should stub an asynchronous function throwing an error`, async t => {
const fn = stub().reject(new Error(`message`));
try {
await fn('foo');
} catch (e) {
t.eq(e.message, 'message');
t.eq(fn.calls, [['foo']]);
}
});
t.test(`throw(error) should make stub throw an exception on next call`, t => {
const fn = stub().throw(new Error(`some error`));
try {
fn('blah');
t.fail(`should not get here`);
} catch (e) {
t.eq(e.message, 'some error');
}
});
});
};