UNPKG

4.6 kBTypeScriptView Raw
1/**
2 * A test result represents the result of an actor test that can either be passed or failed.
3 *
4 * Test results are immutable.
5 */
6export type TestResult<T, TS = undefined> = TestResultPassed<T, TS> | TestResultFailed;
7/**
8 * Create a new test result that represents a passed value.
9 * @param value The value the test passed with.
10 */
11export declare function passTest<T>(value: T): TestResultPassed<T, undefined>;
12/**
13 * Create a new test result that represents a passed void value.
14 */
15export declare function passTestVoid(): TestResultPassed<any, undefined>;
16/**
17 * Create a new test result that represents a passed value with side data.
18 * @param value The value the test passed with.
19 * @param sideData Additional data to pass to the run phase.
20 */
21export declare function passTestWithSideData<T, S>(value: T, sideData: S): TestResultPassed<T, S>;
22/**
23 * Create a new test result that represents a passed void value with side data.
24 * @param sideData Additional data to pass to the run phase.
25 */
26export declare function passTestVoidWithSideData<TS>(sideData: TS): TestResultPassed<any, TS>;
27/**
28 * Create a new test result that represents a test failure.
29 * @param message The error message that describes the failure.
30 */
31export declare function failTest(message: string): TestResultFailed;
32/**
33 * A passed test result.
34 * This should not be constructed manually.
35 * Instead, `testPass` should be used.
36 */
37export declare class TestResultPassed<T, TS> {
38 protected readonly value: T;
39 protected readonly sideData: TS;
40 constructor(passValue: T, sideData: TS);
41 /**
42 * Check if the test has passed.
43 * If true, it will contain a value.
44 */
45 isPassed(): this is TestResultPassed<T, TS>;
46 /**
47 * Check if the test has failed.
48 * If true, it will contain a failure message.
49 */
50 isFailed(): this is TestResultFailed;
51 /**
52 * Get the value of the passed test, or undefined if the test failed.
53 */
54 get(): T;
55 /**
56 * Get the value of the passed test, or throw an error if the test failed.
57 */
58 getOrThrow(): T;
59 /**
60 * The side data that will be passed to run.
61 */
62 getSideData(): TS;
63 /**
64 * Get the failure message callback of the failed test, or undefined if the test passed.
65 */
66 getFailMessage(): undefined;
67 /**
68 * For passed tests, map the passed value to another value.
69 * Failed tests will remain unchanged.
70 *
71 * This will not mutate the test result, and instead return a new test result.
72 *
73 * @param mapper A function that will transform the passed value.
74 */
75 map<T2>(mapper: (value: T, sideData: TS) => T2): TestResultPassed<T2, TS>;
76 /**
77 * For passed tests, asynchronously map the passed value to another value.
78 * Failed tests will remain unchanged.
79 *
80 * This will not mutate the test result, and instead return a new test result.
81 *
82 * @param mapper A function that will transform the passed value.
83 */
84 mapAsync<T2>(mapper: (value: T, sideData: TS) => Promise<T2>): Promise<TestResultPassed<T2, TS>>;
85}
86/**
87 * A failed test result.
88 * This should not be constructed manually.
89 * Instead, `testFail` should be used.
90 */
91export declare class TestResultFailed {
92 protected readonly failMessage: string;
93 constructor(failMessage: string);
94 /**
95 * Check if the test has passed.
96 * If true, it will contain a value.
97 */
98 isPassed(): this is TestResultPassed<any, any>;
99 /**
100 * Check if the test has failed.
101 * If true, it will contain a failure message.
102 */
103 isFailed(): this is TestResultFailed;
104 /**
105 * Get the value of the passed test, or undefined if the test failed.
106 */
107 get(): undefined;
108 /**
109 * Get the value of the passed test, or throw an error if the test failed.
110 */
111 getOrThrow(): never;
112 /**
113 * The side data that will be passed to run.
114 */
115 getSideData(): never;
116 /**
117 * Get the failure message callback of the failed test, or undefined if the test passed.
118 */
119 getFailMessage(): string;
120 /**
121 * For passed tests, map the passed value to another value.
122 * Failed tests will remain unchanged.
123 *
124 * This will not mutate the test result, and instead return a new test result.
125 */
126 map(): TestResultFailed;
127 /**
128 * For passed tests, asynchronously map the passed value to another value.
129 * Failed tests will remain unchanged.
130 *
131 * This will not mutate the test result, and instead return a new test result.
132 */
133 mapAsync(): Promise<TestResultFailed>;
134}