UNPKG

9.35 kBTypeScriptView Raw
1// Type definitions for code 4.0
2// Project: https://github.com/hapijs/code
3// Definitions by: Prashant Tiwari <https://github.com/prashaantt>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5
6/** Generates an assertion object. */
7export function expect<T>(value: T | T[], prefix?: string): AssertionChain<T>;
8/** Makes the test fail with the given message. */
9export function fail(message: string): void;
10/** Returns the total number of assertions created using the expect() method. */
11export function count(): number;
12/** Returns an array of the locations where incomplete assertions were declared or null if no incomplete assertions found. */
13export function incomplete(): string[] | null;
14/** Returns the filename, line number, and column number of where the error was created. */
15export function thrownAt(error?: Error): CodeError;
16/** Configure code. */
17export const settings: Settings;
18
19export type AssertionChain<T> = Assertion<T> & Expectation<T>;
20
21export type Assertion<T> = Grammar<T> & Flags<T>;
22
23export type Expectation<T> = Types<T> & Values<T>;
24
25export interface Grammar<T> {
26 /** Connecting word. */
27 a: AssertionChain<T>;
28 /** Connecting word. */
29 an: AssertionChain<T>;
30 /** Connecting word. */
31 and: AssertionChain<T>;
32 /** Connecting word. */
33 at: AssertionChain<T>;
34 /** Connecting word. */
35 be: AssertionChain<T>;
36 /** Connecting word. */
37 have: AssertionChain<T>;
38 /** Connecting word. */
39 in: AssertionChain<T>;
40 /** Connecting word. */
41 to: AssertionChain<T>;
42}
43
44export interface Flags<T> {
45 /** Inverses the expected result of any assertion */
46 not: AssertionChain<T>;
47 /**
48 * Requires that inclusion matches appear only once in the provided value.
49 * Used by include().
50 */
51 once: AssertionChain<T>;
52 /**
53 * Requires that only the provided elements appear in the provided value.
54 * Used by include().
55 */
56 only: AssertionChain<T>;
57 /**
58 * Allows a partial match when asserting inclusion
59 * Used by include(). Defaults to false.
60 */
61 part: AssertionChain<T>;
62 /**
63 * Performs a comparison using strict equality (===).
64 * Code defaults to deep comparison. Used by equal() and include().
65 */
66 shallow: AssertionChain<T>;
67}
68
69export interface Types<T> {
70 /** Asserts that the reference value is an arguments object. */
71 arguments(): AssertionChain<T>;
72 /** Asserts that the reference value is an Array. */
73 array(): AssertionChain<T>;
74 /** Asserts that the reference value is a boolean. */
75 boolean(): AssertionChain<T>;
76 /** Asserts that the reference value is a Buffer. */
77 buffer(): AssertionChain<T>;
78 /** Asserts that the reference value is a Date. */
79 date(): AssertionChain<T>;
80 /** Asserts that the reference value is an error. */
81 error(type?: any, message?: string | RegExp): AssertionChain<T>;
82 /** Asserts that the reference value is a function. */
83 function(): AssertionChain<T>;
84 /** Asserts that the reference value is a number. */
85 number(): AssertionChain<T>;
86 /** Asserts that the reference value is a RegExp. */
87 regexp(): AssertionChain<T>;
88 /** Asserts that the reference value is a string. */
89 string(): AssertionChain<T>;
90 /** Asserts that the reference value is an object (excluding array, buffer, or other native objects). */
91 object(): AssertionChain<T>;
92}
93
94export interface Values<T> {
95 /** Asserts that the reference value is true. */
96 true(): AssertionChain<T>;
97 /** Asserts that the reference value is false. */
98 false(): AssertionChain<T>;
99 /** Asserts that the reference value is null. */
100 null(): AssertionChain<T>;
101 /** Asserts that the reference value is undefined. */
102 undefined(): AssertionChain<T>;
103 /** Asserts that the reference value (a string, array, or object) includes the provided values. */
104 include(values: string | string[] | T | T[]): AssertionChain<T>;
105 /** Asserts that the reference value (a string, array, or object) includes the provided values. */
106 includes(values: string | string[] | T | T[]): AssertionChain<T>;
107 /** Asserts that the reference value (a string, array, or object) includes the provided values. */
108 contain(values: string | string[] | T | T[]): AssertionChain<T>;
109 /** Asserts that the reference value (a string, array, or object) includes the provided values. */
110 contains(values: string | string[] | T | T[]): AssertionChain<T>;
111 /** Asserts that the reference value (a string) starts with the provided value. */
112 startWith(value: string): AssertionChain<T>;
113 /** Asserts that the reference value (a string) starts with the provided value. */
114 startsWith(value: string): AssertionChain<T>;
115 /** Asserts that the reference value (a string) ends with the provided value. */
116 endWith(value: string): AssertionChain<T>;
117 /** Asserts that the reference value (a string) ends with the provided value. */
118 endsWith(value: string): AssertionChain<T>;
119 /** Asserts that the reference value exists (not null or undefined). */
120 exist(): AssertionChain<T>;
121 /** Asserts that the reference value exists (not null or undefined). */
122 exists(): AssertionChain<T>;
123 /** Asserts that the reference value has a length property equal to zero or an object with no keys. */
124 empty(): AssertionChain<T>;
125 /** Asserts that the reference value has a length property matching the provided size or an object with the specified number of keys. */
126 length(size: number): AssertionChain<T>;
127 /** Asserts that the reference value equals the provided value. */
128 equal(value: T | T[], options?: any): AssertionChain<T>;
129 /** Asserts that the reference value equals the provided value. */
130 equals(value: T | T[], options?: any): AssertionChain<T>;
131 /** Asserts that the reference value is greater than (>) the provided value. */
132 above(value: T): AssertionChain<T>;
133 /** Asserts that the reference value is greater than (>) the provided value. */
134 greaterThan(value: T): AssertionChain<T>;
135 /** Asserts that the reference value is at least (>=) the provided value. */
136 least(value: T): AssertionChain<T>;
137 /** Asserts that the reference value is at least (>=) the provided value. */
138 min(value: T): AssertionChain<T>;
139 /** Asserts that the reference value is less than (<) the provided value. */
140 below(value: T): AssertionChain<T>;
141 /** Asserts that the reference value is less than (<) the provided value. */
142 lessThan(value: T): AssertionChain<T>;
143 /** Asserts that the reference value is at most (<=) the provided value. */
144 most(value: T): AssertionChain<T>;
145 /** Asserts that the reference value is at most (<=) the provided value. */
146 max(value: T): AssertionChain<T>;
147 /** Asserts that the reference value is within (from <= value <= to) the provided values. */
148 within(from: T, to: T): AssertionChain<T>;
149 /** Asserts that the reference value is within (from <= value <= to) the provided values. */
150 range(from: T, to: T): AssertionChain<T>;
151 /** Asserts that the reference value is between but not equal (from < value < to) the provided values. */
152 between(from: T, to: T): AssertionChain<T>;
153 /** Asserts that the reference value is about the provided value within a delta margin of difference. */
154 about(value: number, delta: number): AssertionChain<T>;
155 /** Asserts that the reference value has the provided instanceof value. */
156 instanceof(type: any): AssertionChain<T>;
157 /** Asserts that the reference value has the provided instanceof value. */
158 instanceOf(type: any): AssertionChain<T>;
159 /** Asserts that the reference value's toString() representation matches the provided regular expression. */
160 match(regex: RegExp): AssertionChain<T>;
161 /** Asserts that the reference value's toString() representation matches the provided regular expression. */
162 matches(regex: RegExp): AssertionChain<T>;
163 /** Asserts that the Promise reference value rejects with an exception when called */
164 reject(type?: any, message?: string | RegExp): AssertionChain<T>;
165 /** Asserts that the Promise reference value rejects with an exception when called */
166 rejects(type?: any, message?: string | RegExp): AssertionChain<T>;
167 /** Asserts that the reference value satisfies the provided validator function. */
168 satisfy(validator: (value: T) => boolean): AssertionChain<T>;
169 /** Asserts that the reference value satisfies the provided validator function. */
170 satisfies(validator: (value: T) => boolean): AssertionChain<T>;
171 /** Asserts that the function reference value throws an exception when called. */
172 throw(type?: any, message?: string | RegExp): AssertionChain<T>;
173 /** Asserts that the function reference value throws an exception when called. */
174 throws(type?: any, message?: string | RegExp): AssertionChain<T>;
175}
176
177export interface Settings {
178 /**
179 * Truncate long assertion error messages for readability?
180 * Defaults to true.
181 */
182 truncateMessages?: boolean | undefined;
183 /**
184 * Ignore object prototypes when doing a deep comparison?
185 * Defaults to false.
186 */
187 comparePrototypes?: boolean | undefined;
188}
189
190export interface CodeError {
191 filename: string;
192 line: string;
193 column: string;
194}