-
Notifications
You must be signed in to change notification settings - Fork 0
/
fail.test.ts
107 lines (72 loc) · 2.64 KB
/
fail.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
import test from "node:test";
import { ok, equal } from "node:assert/strict";
import { expectNotType, expectType } from "tsd";
import Future from "./index.js";
{
const a = Future.fail("error");
// Expect the fail function to infer exact type of the non-thenable value.
expectType<Future.Self<never, string>>(a);
a.catch(() => {});
}
{
const a = Future.fail(Future.of(1));
// Expect the Future to receive the Right type of the FutureLike argument as the Left type.
expectType<Future.Self<never, number>>(a);
a.catch(() => {});
}
{
const a = Future.fail(Future.fail(1));
// Expect the Future to receive the Left type of the FutureLike argument as the Left type.
expectType<Future.Self<never, number>>(a);
a.catch(() => {});
}
{
const a = Future.fail(Promise.resolve(1));
// Expect the Future to receive the Left type as unknown if the resolved PromiseLike is an argument.
expectType<Future.Self<never, unknown>>(a);
expectNotType<Future.Self<never, number>>(a);
a.catch(() => {});
}
{
const a = Future.fail<number, string>(Promise.resolve(1));
// Expect the Left type to be configurable if an argument is the resolved PromiseLike.
expectType<Future.Self<never, number | string>>(a);
a.catch(() => {});
}
{
const a = Future.fail(Promise.reject(1));
// Expect the Future to receive the Left type as unknown if the rejected PromiseLike is an argument.
expectType<Future.Self<never, unknown>>(a);
a.catch(() => {});
}
{
const a = Future.fail<string, number>(Promise.reject(1));
// Expect the Left type to be configurable if an argument is the rejected PromiseLike.
expectType<Future.Self<never, number | string>>(a);
a.catch(() => {});
}
{
const a = Future.fail();
// Invokation of the fail function without any argument creates an empty rejected Future.
expectType<Future.Self<never, void>>(a);
a.catch(() => {});
}
test("fail function should wrap a non-Promise value into the Future (Promise)", async () => {
const a = Future.fail(8);
ok(a instanceof Promise);
return a.catch(() => {});
});
test("fail function should fail future with a value", async () => {
const a: Future.Self<number, "foo"> = Future.fail("foo");
return a.catch((error) => equal(error, "foo"));
});
test("fail function rejects a passed Future", async (context) => {
await context.test("fulfilled Future", async () => {
const a: Future.Self<never, number> = Future.fail(Future.of(8));
return a.catch((n) => equal(n, 8));
});
await context.test("rejected Future", async () => {
const a: Future.Self<never, "mark"> = Future.fail(Promise.reject("mark"));
return a.catch((v) => equal(v, "mark"));
});
});