UNPKG

4.92 kBTypeScriptView Raw
1// Type definitions for commonjs-assert 1.5
2// Project: https://github.com/browserify/commonjs-assert, https://github.com/defunctzombie/commonjs-assert
3// Definitions by: Nico Gallinal <https://github.com/nicoabie>
4// Linus Unnebäck <https://github.com/LinusU>
5// ExE Boss <https://github.com/ExE-Boss>
6// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
7
8/** An alias of `assert.ok()`. */
9declare function assert(value: any, message?: string | Error): asserts value;
10declare namespace assert {
11 class AssertionError extends Error {
12 actual: any;
13 expected: any;
14 operator: string;
15 generatedMessage: boolean;
16 code: 'ERR_ASSERTION';
17
18 constructor(options?: {
19 /** If provided, the error message is set to this value. */
20 message?: string | undefined;
21 /** The `actual` property on the error instance. */
22 actual?: any;
23 /** The `expected` property on the error instance. */
24 expected?: any;
25 /** The `operator` property on the error instance. */
26 operator?: string | undefined;
27 /** If provided, the generated stack trace omits frames before this function. */
28 // tslint:disable-next-line:ban-types
29 stackStartFn?: Function | undefined;
30 });
31 }
32
33 type AssertPredicate = RegExp | (new () => object) | ((thrown: any) => boolean) | object | Error;
34
35 function fail(message?: string | Error): never;
36 /** @deprecated since v10.0.0 - use fail([message]) or other assert functions instead. */
37 function fail(
38 actual: any,
39 expected: any,
40 message?: string | Error,
41 operator?: string,
42 // tslint:disable-next-line:ban-types
43 stackStartFn?: Function,
44 ): never;
45 function ok(value: any, message?: string | Error): asserts value;
46 /** @deprecated since v9.9.0 - use strictEqual() instead. */
47 function equal(actual: any, expected: any, message?: string | Error): void;
48 /** @deprecated since v9.9.0 - use notStrictEqual() instead. */
49 function notEqual(actual: any, expected: any, message?: string | Error): void;
50 /** @deprecated since v9.9.0 - use deepStrictEqual() instead. */
51 function deepEqual(actual: any, expected: any, message?: string | Error): void;
52 /** @deprecated since v9.9.0 - use notDeepStrictEqual() instead. */
53 function notDeepEqual(actual: any, expected: any, message?: string | Error): void;
54 function strictEqual<T>(actual: any, expected: T, message?: string | Error): asserts actual is T;
55 function notStrictEqual(actual: any, expected: any, message?: string | Error): void;
56 function deepStrictEqual<T>(actual: any, expected: T, message?: string | Error): asserts actual is T;
57 function notDeepStrictEqual(actual: any, expected: any, message?: string | Error): void;
58
59 function throws(block: () => any, message?: string | Error): void;
60 function throws(block: () => any, error: AssertPredicate, message?: string | Error): void;
61 function doesNotThrow(block: () => any, message?: string | Error): void;
62 function doesNotThrow(block: () => any, error: AssertPredicate, message?: string | Error): void;
63
64 function ifError(value: any): asserts value is null | undefined;
65
66 function rejects(block: (() => Promise<any>) | Promise<any>, message?: string | Error): Promise<void>;
67 function rejects(
68 block: (() => Promise<any>) | Promise<any>,
69 error: AssertPredicate,
70 message?: string | Error,
71 ): Promise<void>;
72 function doesNotReject(block: (() => Promise<any>) | Promise<any>, message?: string | Error): Promise<void>;
73 function doesNotReject(
74 block: (() => Promise<any>) | Promise<any>,
75 error: AssertPredicate,
76 message?: string | Error,
77 ): Promise<void>;
78
79 function match(value: string, regExp: RegExp, message?: string | Error): void;
80 function doesNotMatch(value: string, regExp: RegExp, message?: string | Error): void;
81
82 const strict: Omit<
83 typeof assert,
84 | 'equal'
85 | 'notEqual'
86 | 'deepEqual'
87 | 'notDeepEqual'
88 | 'ok'
89 | 'strictEqual'
90 | 'deepStrictEqual'
91 | 'ifError'
92 | 'strict'
93 > & {
94 (value: any, message?: string | Error): asserts value;
95 equal: typeof strictEqual;
96 notEqual: typeof notStrictEqual;
97 deepEqual: typeof deepStrictEqual;
98 notDeepEqual: typeof notDeepStrictEqual;
99
100 // Mapped types and assertion functions are incompatible?
101 // TS2775: Assertions require every name in the call target
102 // to be declared with an explicit type annotation.
103 ok: typeof ok;
104 strictEqual: typeof strictEqual;
105 deepStrictEqual: typeof deepStrictEqual;
106 ifError: typeof ifError;
107 strict: typeof strict;
108 };
109}
110
111export = assert;