-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
82 lines (73 loc) · 1.97 KB
/
index.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
/** @private */
declare namespace JestTyte {
interface DoneCallback {
(...args: any[]): any;
fail(error?: string | { message: string }): any;
}
type ProvidesCallback = (cb: DoneCallback) => any;
}
/** @private */
const noop = () => {};
/**
* A noop function that prevents the given jest callback from actually running
* @example
* type MyType = number;
*
* describe("MyType", () => {
* it("resolves to a number", tyte((
* value: MyType,
* values: MyType[],
* ) => {
* tyte.expectType<number>(value);
* tyte.expectType<number[]>(values);
* }));
* });
*/
function tyte<Subjects extends any[]>(callback: (...subjects: Subjects) => any): JestTyte.ProvidesCallback;
function tyte() {
return noop;
}
namespace tyte {
/**
* Substitute for a type in .each arrays
* @example
* test.each([
* tyte.subject as number,
* tyte.subject as string,
* ])("is a number or a string", tyte((subject) => {
* tyte.expectType<number | string>(subject);
* }));
*/
export const subject: unknown = null;
/**
* Expect a given value to be of a given type
* @example
* tyte.expectType<number>(Date.now());
*/
export const expectType = noop as (
<Value = never>(subject: Value) => void
);
/**
* Sub-module for function-related TypeScript-only assertions
*/
export namespace fn {
/**
* Expect a given function to have parameters according to a given list of types
* @example
* tyte.fn.expectParams<[number]>(Math.round);
* tyte.fn.expectParams<[]>(Math.random);
*/
export const expectParams = noop as (
<Params extends any[] = []>(subject: (...params: Params) => any) => void
);
/**
* Expect a given function to return a value of a given type
* @example
* tyte.fn.expectReturns<string>(Object.toString);
*/
export const expectReturns = noop as (
<Output = never>(subject: (...params: any[]) => Output) => void
);
}
}
export default tyte;