| 1 |
let done = false, value = 0; |
| 2 |
|
| 3 |
|
| 4 |
function fn(impl = () => {}) { |
| 5 |
function f() { |
| 6 |
f.calls++; |
| 7 |
return impl(); |
| 8 |
} |
| 9 |
f.calls = 0; |
| 10 |
return f; |
| 11 |
} |
| 12 |
|
| 13 |
const iterator = { |
| 14 |
next: fn(() => ({ done, value })), |
| 15 |
throw: fn(), |
| 16 |
return: fn(() => { throw {} }), |
| 17 |
}; |
| 18 |
|
| 19 |
const obj = { |
| 20 |
[Symbol.iterator]: fn(() => iterator), |
| 21 |
}; |
| 22 |
|
| 23 |
const err = new Error(); |
| 24 |
|
| 25 |
expect(() => { |
| 26 |
for (const x of obj) { |
| 27 |
value++; |
| 28 |
if (value == 2) throw err; |
| 29 |
} |
| 30 |
}).toThrow(err); |
| 31 |
|
| 32 |
expect(obj[Symbol.iterator].calls).toBe(1); |
| 33 |
expect(iterator.next.calls).toBe(2); |
| 34 |
expect(iterator.throw.calls).toBe(0); |
| 35 |
expect(iterator.return.calls).toBe(1); |