UNPKG

12.9 kBTypeScriptView Raw
1/// <reference types="node" />
2
3type Func = (...args: any[]) => any;
4export {};
5
6import { Stream } from "stream";
7
8export function array(arr: any[], message?: string): asserts arr is any[];
9
10export function bool(bool: boolean, message?: string): asserts bool is boolean;
11
12export function buffer(buffer: Buffer, message?: string): asserts buffer is Buffer;
13
14export function func(func: Func, message?: string): asserts func is Func;
15
16export function number(number: number, message?: string): asserts number is number;
17
18export function finite(finite: number, message?: string): asserts finite is number;
19
20export function object<T extends object = any>(obj: T, message?: string): asserts obj is T;
21
22export function string(str: string, message?: string): asserts str is string;
23
24export function stream(stream: Stream, message?: string): asserts stream is Stream;
25
26export function date(date: Date, message?: string): asserts date is Date;
27
28export function regexp(regexp: RegExp, message?: string): asserts regexp is RegExp;
29
30export function uuid(uuid: string, message?: string): asserts uuid is string;
31
32export function arrayOfArray(arr: any[][], message?: string): asserts arr is any[][];
33
34export function arrayOfBool(arr: boolean[], message?: string): asserts arr is boolean[];
35
36export function arrayOfBuffer(arr: Buffer[], message?: string): asserts arr is Buffer[];
37
38export function arrayOfFunc(arr: Func[], message?: string): asserts arr is Func[];
39
40export function arrayOfNumber(arr: number[], message?: string): asserts arr is number[];
41
42export function arrayOfFinite(arr: number[], message?: string): asserts arr is number[];
43
44export function arrayOfObject<T extends object = any>(arr: T[], message?: string): asserts arr is T[];
45
46export function arrayOfString(arr: string[], message?: string): asserts arr is string[];
47
48export function arrayOfStream(arr: Stream[], message?: string): asserts arr is Stream[];
49
50export function arrayOfDate(arr: Date[], message?: string): asserts arr is Date[];
51
52export function arrayOfRegexp(arr: RegExp[], message?: string): asserts arr is RegExp[];
53
54export function arrayOfUuid(arr: string[], message?: string): asserts arr is string[];
55
56export function optionalArray(arr: any[] | undefined, message?: string): asserts arr is any[] | undefined;
57
58export function optionalBool(bool: boolean | undefined, message?: string): asserts bool is boolean | undefined;
59
60export function optionalBuffer(buffer: Buffer | undefined, message?: string): asserts buffer is Buffer | undefined;
61
62export function optionalFunc(options: Func | undefined, message?: string): asserts options is Func | undefined;
63
64export function optionalNumber(options: number | undefined, message?: string): asserts options is number | undefined;
65
66export function optionalFinite(options: number | undefined, message?: string): asserts options is number | undefined;
67
68export function optionalObject<T extends object = any>(
69 options: T | undefined,
70 message?: string,
71): asserts options is T | undefined;
72
73export function optionalString(options: string | undefined, message?: string): asserts options is string | undefined;
74
75export function optionalStream(options: Stream | undefined, message?: string): asserts options is Stream | undefined;
76
77export function optionalDate(options: Date | undefined, message?: string): asserts options is Date | undefined;
78
79export function optionalRegexp(options: RegExp | undefined, message?: string): asserts options is RegExp | undefined;
80
81export function optionalUuid(options: string | undefined, message?: string): asserts options is string | undefined;
82
83export function optionalArrayOfArray(arr: any[][] | undefined, message?: string): asserts arr is any[][] | undefined;
84
85export function optionalArrayOfBool(arr: boolean[] | undefined, message?: string): asserts arr is boolean[] | undefined;
86
87export function optionalArrayOfBuffer(arr: Buffer[] | undefined, message?: string): asserts arr is Buffer[] | undefined;
88
89export function optionalArrayOfFunc(arr: Func[] | undefined, message?: string): asserts arr is Func[] | undefined;
90
91export function optionalArrayOfNumber(arr: number[] | undefined, message?: string): asserts arr is number[] | undefined;
92
93export function optionalArrayOfFinite(arr: number[] | undefined, message?: string): asserts arr is number[] | undefined;
94
95export function optionalArrayOfObject<T extends object = any>(
96 arr: T[] | undefined,
97 message?: string,
98): asserts arr is T[] | undefined;
99
100export function optionalArrayOfString(arr: string[] | undefined, message?: string): asserts arr is string[] | undefined;
101
102export function optionalArrayOfStream(arr: Stream[] | undefined, message?: string): asserts arr is Stream[] | undefined;
103
104export function optionalArrayOfDate(arr: Date[] | undefined, message?: string): asserts arr is Date[] | undefined;
105
106export function optionalArrayOfRegexp(arr: RegExp[] | undefined, message?: string): asserts arr is RegExp[] | undefined;
107
108export function optionalArrayOfUuid(arr: string[] | undefined, message?: string): asserts arr is string[] | undefined;
109
110export function AssertionError(options: any, message?: string): void;
111
112/**
113 * Throws an `AssertionError`. If `message` is falsy, the error message is set
114 * as the values of `actual` and `expected` separated by the provided `operator`.
115 * Otherwise, the error message is the value of `message`.
116 *
117 * ```js
118 * const assert = require('assert');
119 *
120 * assert.fail(1, 2, undefined, '>');
121 * // AssertionError: 1 > 2
122 *
123 * assert.fail(1, 2, 'whoops', '>');
124 * // AssertionError: whoops
125 * ```
126 */
127export function fail(actual: any, expected: any, message: any, operator: any): void;
128
129/**
130 * Tests if `value` is truthy. It is equivalent to `assert.equal(!!value, true, message)`.
131 *
132 * If `value` is not truthy, an `AssertionError` is thrown with a `message` property
133 * set equal to the value of the `message` parameter.
134 * If the `message` parameter is `undefined`, a default error message is assigned.
135 *
136 * ```js
137 * const assert = require('assert');
138 *
139 * assert.ok(true);
140 * // OK
141 * assert.ok(1);
142 * // OK
143 * assert.ok(false);
144 * // throws "AssertionError: false == true"
145 * assert.ok(0);
146 * // throws "AssertionError: 0 == true"
147 * assert.ok(false, 'it\'s false');
148 * // throws "AssertionError: it's false"
149 * ```
150 */
151export function ok(options: any, message?: string): void;
152
153/**
154 * Tests shallow, coercive equality between the actual and expected parameters
155 * using the equal comparison operator ( `==` ).
156 *
157 * ```js
158 * const assert = require('assert');
159 *
160 * assert.equal(1, 1);
161 * // OK, 1 == 1
162 * assert.equal(1, '1');
163 * // OK, 1 == '1'
164 *
165 * assert.equal(1, 2);
166 * // AssertionError: 1 == 2
167 * assert.equal({a: {b: 1}}, {a: {b: 1}});
168 * //AssertionError: { a: { b: 1 } } == { a: { b: 1 } }
169 * ```
170 *
171 * If the values are not equal, an `AssertionError` is thrown with
172 * a `message` property set equal to the value of the `message` parameter.
173 * If the `message` parameter is undefined, a default error message is assigned.
174 */
175export function equal(actual: any, expected: any, message?: string): void;
176
177/**
178 * Tests shallow, coercive inequality with the not equal comparison operator ( `!=` ).
179 *
180 * ```js
181 * const assert = require('assert');
182 *
183 * assert.notEqual(1, 2);
184 * // OK
185 *
186 * assert.notEqual(1, 1);
187 * // AssertionError: 1 != 1
188 *
189 * assert.notEqual(1, '1');
190 * // AssertionError: 1 != '1'
191 * ```
192 *
193 * If the values are equal, an `AssertionError` is thrown with
194 * a `message` property set equal to the value of the `message` parameter.
195 * If the `message` parameter is undefined, a default error message is assigned.
196 */
197export function notEqual(actual: any, expected: any, message?: string): void;
198
199/**
200 * Tests for deep equality between the `actual` and `expected` parameters.
201 * Primitive values are compared with the equal comparison operator ( `==` ).
202 *
203 * Only enumerable "own" properties are considered.
204 * The `deepEqual()` implementation does not test object prototypes, attached symbols,
205 * or non-enumerable properties. This can lead to some potentially surprising results.
206 * For example, the following example does not throw an `AssertionError` because
207 * the properties on the Error object are non-enumerable:
208 *
209 * ```js
210 * // WARNING: This does not throw an AssertionError!
211 * assert.deepEqual(Error('a'), Error('b'));
212 * ```
213 *
214 * "Deep" equality means that the enumerable "own" properties of child objects are evaluated also.
215 *
216 * If the values are not equal, an `AssertionError` is thrown with a `message` property
217 * set equal to the value of the `message` parameter. If the `message` parameter is undefined,
218 * a default error message is assigned.
219 */
220export function deepEqual<T>(actual: T, expected: T, message?: string): void;
221
222/**
223 * Tests for any deep inequality. Opposite of `assert.deepEqual()`.
224 *
225 * ```js
226 * const assert = require('assert');
227 *
228 * const obj1 = { a : { b : 1 } };
229 * const obj2 = { a : { b : 2 } };
230 * const obj3 = { a : { b : 1 } };
231 * const obj4 = Object.create(obj1);
232 *
233 * assert.notDeepEqual(obj1, obj1);
234 * // AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }
235 *
236 * assert.notDeepEqual(obj1, obj2);
237 * // OK, obj1 and obj2 are not deeply equal
238 *
239 * assert.notDeepEqual(obj1, obj3);
240 * // AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }
241 *
242 * assert.notDeepEqual(obj1, obj4);
243 * // OK, obj1 and obj2 are not deeply equal
244 * ```
245 *
246 * If the values are deeply equal, an `AssertionError` is thrown with
247 * a `message` property set equal to the value of the `message` parameter.
248 * If the `message` parameter is undefined, a default error message is assigned.
249 */
250export function notDeepEqual(actual: any, expected: any, message?: string): void;
251
252/**
253 * Tests strict equality as determined by the strict equality operator ( `===` ).
254 *
255 * ```js
256 * const assert = require('assert');
257 *
258 * assert.strictEqual(1, 2);
259 * // AssertionError: 1 === 2
260 *
261 * assert.strictEqual(1, 1);
262 * // OK
263 *
264 * assert.strictEqual(1, '1');
265 * // AssertionError: 1 === '1'
266 * ```
267 *
268 * If the values are not strictly equal, an `AssertionError` is thrown with
269 * a `message` property set equal to the value of the `message` parameter.
270 * If the `message` parameter is undefined, a default error message is assigned.
271 */
272export function strictEqual<T>(actual: T, expected: T, message?: string): void;
273
274/**
275 * Tests strict inequality as determined by the strict not equal operator ( `!==` ).
276 *
277 * ```js
278 * const assert = require('assert');
279 *
280 * assert.notStrictEqual(1, 2);
281 * // OK
282 *
283 * assert.notStrictEqual(1, 1);
284 * // AssertionError: 1 !== 1
285 *
286 * assert.notStrictEqual(1, '1');
287 * // OK
288 * ```
289 *
290 * If the values are strictly equal, an `AssertionError` is thrown with a `message` property
291 * set equal to the value of the `message` parameter. If the `message` parameter is undefined,
292 * a default error message is assigned.
293 */
294export function notStrictEqual(actual: any, expected: any, message?: string): void;
295
296export function throws(block: any, error?: any, message?: string): void;
297
298/**
299 * Asserts that the function `block` does not throw an error. See `assert.throws()` for more details.
300 *
301 * When `assert.doesNotThrow()` is called, it will immediately call the `block` function.
302 *
303 * If an error is thrown and it is the same type as that specified by the `error` parameter,
304 * then an `AssertionError` is thrown. If the error is of a different type,
305 * or if the `error` parameter is undefined, the error is propagated back to the caller.
306 *
307 * The following, for instance, will throw the TypeError because there is no matching error type in the assertion:
308 * ```js
309 * assert.doesNotThrow(
310 * () => {
311 * throw new TypeError('Wrong value');
312 * },
313 * SyntaxError
314 * );
315 * ```
316 *
317 * However, the following will result in an `AssertionError` with the message 'Got unwanted exception (TypeError)..':
318 * ```js
319 * assert.doesNotThrow(
320 * () => {
321 * throw new TypeError('Wrong value');
322 * },
323 * TypeError
324 * );
325 * ```
326 *
327 * If an `AssertionError` is thrown and a value is provided for the `message` parameter,
328 * the value of `message` will be appended to the `AssertionError` message:
329 * ```js
330 * assert.doesNotThrow(
331 * () => {
332 * throw new TypeError('Wrong value');
333 * },
334 * TypeError,
335 * 'Whoops'
336 * );
337 * // Throws: AssertionError: Got unwanted exception (TypeError). Whoops
338 * ```
339 */
340export function doesNotThrow(block: any, error?: any, message?: string): void;
341
342/**
343 * Throws `value` if `value` is truthy. This is useful when testing the `error` argument in callbacks.
344 * ```js
345 * const assert = require('assert');
346 *
347 * assert.ifError(0);
348 * // OK
349 * assert.ifError(1);
350 * // Throws 1
351 * assert.ifError('error');
352 * // Throws 'error'
353 * assert.ifError(new Error());
354 * // Throws Error
355 * ```
356 */
357export function ifError(value: any): void;
358
359export as namespace AssertPlus;