-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex1.test.js
More file actions
79 lines (70 loc) · 1.86 KB
/
ex1.test.js
File metadata and controls
79 lines (70 loc) · 1.86 KB
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
'use strict';
const { adopt } = require('./ex1');
function assert(condition, message) {
if (!condition) throw new Error(message || 'Assertion failed');
}
(async () => {
// 1) Double resolve: only first wins
const t1 = {
then(resolve) {
resolve(1);
resolve(2);
throw new Error('ignored');
}
};
const r1 = await adopt(Promise.resolve(), t1);
assert(r1 === 1, 'only first resolve should win');
// 2) then getter throws => reject with that error
const t2 = {};
Object.defineProperty(t2, 'then', {
get() { throw new Error('boom'); }
});
try {
await adopt(Promise.resolve(), t2);
assert(false, 'should reject when then getter throws');
} catch (e) {
assert(e && e.message === 'boom', 'must reject with getter error');
}
// 3) then throws before settling => reject
const t3 = {
then() {
throw new Error('kaboom');
}
};
try {
await adopt(Promise.resolve(), t3);
assert(false, 'should reject when then throws before settling');
} catch (e) {
assert(e && e.message === 'kaboom', 'must reject with then error');
}
// 4) then calls reject => reject
const t4 = {
then(_resolve, reject) {
reject(new Error('nope'));
}
};
try {
await adopt(Promise.resolve(), t4);
assert(false, 'should reject when thenable rejects');
} catch (e) {
assert(e && e.message === 'nope', 'must reject with rejection reason');
}
// 5) self-resolution protection: resolving with itself must TypeError
let self;
const t5 = {
then(resolve) {
resolve(self);
}
};
self = t5;
try {
await adopt(Promise.resolve(), t5);
assert(false, 'should reject on self-resolution');
} catch (e) {
assert(e && e.name === 'TypeError', 'self-resolution must TypeError');
}
console.log('ex1 tests passed');
})().catch((err) => {
console.error(err);
process.exit(1);
});