UNPKG

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