-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhelpers.test.ts
150 lines (136 loc) · 4.33 KB
/
helpers.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
import { describe, expect, expectTypeOf, it } from "vitest";
import type {
InferPromise,
IsAsyncFunction,
IsFunction,
ListContains,
ListContainsFunction,
ListContainsPromiseOrAsyncFunction,
Union,
UnionContainsPromise,
UnwrapList,
} from "./helpers.js";
import {
assertUnreachable,
isAsyncFn,
isFunction,
isPromise,
} from "./helpers.js";
describe("helpers", () => {
describe("isPromise", () => {
it("tells whether a value represents a promise", () => {
expect(isPromise({})).toBe(false);
expect(isPromise(null)).toBe(false);
expect(isPromise(undefined)).toBe(false);
expect(isPromise(Promise.resolve(12))).toBe(true);
// biome-ignore lint/suspicious/noThenProperty:
expect(isPromise({ then: () => {} })).toBe(true);
});
});
describe("isFunction", () => {
it("tells whether a value represents a function", () => {
expect(isFunction(() => {})).toBe(true);
expect(isFunction(async () => {})).toBe(true);
expect(isFunction({})).toBe(false);
});
});
describe("isAsyncFn", () => {
it("tells whether a value represents an async function", () => {
expect(isAsyncFn(() => {})).toBe(false);
expect(isAsyncFn(() => Promise.resolve(12))).toBe(false);
expect(isAsyncFn(async () => {})).toBe(true);
});
});
describe("assertUnreachable", () => {
it("throws an error when called", () => {
// @ts-expect-error
expect(() => assertUnreachable("")).toThrowError("Unreachable case: ");
});
it("complains (TS) when not all cases are handles", () => {
const value = "a" as "a" | "b";
switch (value) {
case "a":
break;
default:
// @ts-expect-error Argument of type is not assignable to parameter of type 'never'
assertUnreachable(value);
}
});
});
describe("IsAsyncFunction", () => {
it("tells whether a type represents an async function", () => {
expectTypeOf<IsAsyncFunction<() => number>>().toEqualTypeOf<false>();
expectTypeOf<
IsAsyncFunction<() => Promise<void>>
>().toEqualTypeOf<true>();
});
});
describe("IsFunction", () => {
it("tells whether a type represents a function", () => {
expectTypeOf<IsFunction<() => number>>().toEqualTypeOf<true>();
expectTypeOf<IsFunction<() => Promise<void>>>().toEqualTypeOf<true>();
expectTypeOf<IsFunction<{ a: 1 }>>().toEqualTypeOf<false>();
});
});
describe("ListContains", () => {
it("tells whether an entry in a list is true", () => {
expectTypeOf<ListContains<[false, false]>>().toEqualTypeOf<false>();
expectTypeOf<ListContains<[false, true]>>().toEqualTypeOf<true>();
expectTypeOf<ListContains<[true, true]>>().toEqualTypeOf<true>();
});
});
describe("ListContainsPromiseOrAsyncFunction", () => {
it("tells whether a list contains a promise or async function", () => {
expectTypeOf<
ListContainsPromiseOrAsyncFunction<[() => void]>
>().toEqualTypeOf<false>();
expectTypeOf<
ListContainsPromiseOrAsyncFunction<[() => Promise<void>, () => void]>
>().toEqualTypeOf<true>();
expectTypeOf<
ListContainsPromiseOrAsyncFunction<[() => Promise<void>]>
>().toEqualTypeOf<true>();
});
});
describe("ListContainsFunction", () => {
it("tells whether a list contains a function", () => {
expectTypeOf<ListContainsFunction<[() => void]>>().toEqualTypeOf<true>();
expectTypeOf<ListContainsFunction<[1]>>().toEqualTypeOf<false>();
expectTypeOf<
ListContainsFunction<[1, () => void]>
>().toEqualTypeOf<true>();
});
});
describe("Union", () => {
it("creates a union type from a list", () => {
expectTypeOf<Union<["a", "b", "c"]>>().toEqualTypeOf<"a" | "b" | "c">();
});
});
describe("UnwrapList", () => {
it("unwraps a list of types", () => {
expectTypeOf<
UnwrapList<[{ a: "a" }, () => { b: "b" }, () => Promise<{ c: "c" }>]>
>().toEqualTypeOf<[{ a: "a" }, { b: "b" }, { c: "c" }]>();
});
});
describe("InferPromise", () => {
it("infers the type of a promise", () => {
expectTypeOf<InferPromise<Promise<{ a: "a" }>>>().toEqualTypeOf<{
a: "a";
}>();
});
});
describe("UnionContainsPromise", () => {
it("tells whether a union contains a promise", () => {
expectTypeOf<
UnionContainsPromise<Promise<number> | number>
>().toEqualTypeOf<true>();
expectTypeOf<
UnionContainsPromise<string | number>
>().toEqualTypeOf<false>();
expectTypeOf<
UnionContainsPromise<Promise<number>>
>().toEqualTypeOf<true>();
});
});
});