UNPKG

10.7 kBTypeScriptView Raw
1/// <reference types="node" />
2
3export = tape;
4
5/**
6 * Create a new test with an optional name string and optional opts object.
7 * cb(t) fires with the new test object t once all preceding tests have finished.
8 * Tests execute serially.
9 */
10declare function tape(name: string, cb: tape.TestCase): void;
11declare function tape(name: string, opts: tape.TestOptions, cb: tape.TestCase): void;
12declare function tape(cb: tape.TestCase): void;
13declare function tape(opts: tape.TestOptions, cb: tape.TestCase): void;
14
15declare namespace tape {
16 interface TestCase {
17 (test: Test): void | Promise<void>;
18 }
19
20 /**
21 * Available opts options for the tape function.
22 */
23 interface TestOptions {
24 /** See test.skip. */
25 skip?: boolean | undefined;
26 /** Set a timeout for the test, after which it will fail. See tape.timeoutAfter. */
27 timeout?: number | undefined;
28 /**
29 * Configure max depth of expected/actual object printing. Environmental variable
30 * NODE_TAPE_OBJECT_PRINT_DEPTH can set the desired default depth for all tests;
31 * locally-set values will take precedence.
32 */
33 objectPrintDepth?: number | undefined;
34 /** Test will be allowed to fail. */
35 todo?: boolean | undefined;
36 }
37
38 /**
39 * Available options for tape assertions.
40 */
41 interface AssertOptions {
42 /** Skip the assertion. Can also be a message explaining why the test is skipped. */
43 skip?: boolean | string | undefined;
44 /** Allows the assertion to fail. */
45 todo?: boolean | string | undefined;
46 /** An optional description of the assertion. */
47 message?: string | undefined;
48 }
49
50 /**
51 * Options for the createStream function.
52 */
53 interface StreamOptions {
54 objectMode?: boolean | undefined;
55 }
56
57 /**
58 * Generate a new test that will be skipped over.
59 */
60 export function skip(name: string, cb: tape.TestCase): void;
61 export function skip(name: string, opts: tape.TestOptions, cb: tape.TestCase): void;
62 export function skip(cb: tape.TestCase): void;
63 export function skip(opts: tape.TestOptions, cb: tape.TestCase): void;
64
65 /**
66 * The onFinish hook will get invoked when ALL tape tests have finished right before tape is about to print the test summary.
67 */
68 export function onFinish(cb: () => void): void;
69
70 /**
71 * The onFailure hook will get invoked whenever any tape tests have failed.
72 */
73 export function onFailure(cb: () => void): void;
74
75 /**
76 * Like test(name?, opts?, cb) except if you use .only this is the only test case that will run for the entire process, all other test cases using tape will be ignored.
77 */
78 export function only(name: string, cb: tape.TestCase): void;
79 export function only(name: string, opts: tape.TestOptions, cb: tape.TestCase): void;
80 export function only(cb: tape.TestCase): void;
81 export function only(opts: tape.TestOptions, cb: tape.TestCase): void;
82
83 /**
84 * Create a new test harness instance, which is a function like test(), but with a new pending stack and test state.
85 */
86 export function createHarness(opts?: { autoclose?: boolean; noOnly?: boolean }): typeof tape;
87
88 /**
89 * Create a memoized test harness instance, which is a function like test(), but with a new pending stack and test state.
90 */
91 export function getHarness(
92 opts?: {
93 noOnly?: boolean;
94 exit?: boolean;
95 stream?: ReturnType<typeof import("through")>;
96 } & StreamOptions,
97 ): typeof tape;
98
99 /**
100 * Create a stream of output, bypassing the default output stream that writes messages to console.log().
101 * By default stream will be a text stream of TAP output, but you can get an object stream instead by setting opts.objectMode to true.
102 */
103 export function createStream(opts?: tape.StreamOptions): NodeJS.ReadableStream;
104
105 export interface Test {
106 /**
107 * Create a subtest with a new test handle st from cb(st) inside the current test.
108 * cb(st) will only fire when t finishes.
109 * Additional tests queued up after t will not be run until all subtests finish.
110 */
111 test(name: string, cb: tape.TestCase): void;
112 test(name: string, opts: TestOptions, cb: tape.TestCase): void;
113
114 /**
115 * Declare that n assertions should be run. end() will be called automatically after the nth assertion.
116 * If there are any more assertions after the nth, or after end() is called, they will generate errors.
117 */
118 plan(n: number): void;
119
120 /**
121 * Declare the end of a test explicitly.
122 * If err is passed in t.end will assert that it is falsey.
123 */
124 end(err?: any): void;
125
126 /**
127 * Generate a failing assertion with a message msg.
128 */
129 fail(msg?: string, extra?: AssertOptions): void;
130
131 /**
132 * Generate a passing assertion with a message msg.
133 */
134 pass(msg?: string, extra?: AssertOptions): void;
135
136 /**
137 * Automatically timeout the test after X ms.
138 */
139 timeoutAfter(ms: number): void;
140
141 /**
142 * Generate an assertion that will be skipped over.
143 */
144 skip(msg?: string, extra?: AssertOptions): void;
145
146 /**
147 * Assert that value is truthy with an optional description message msg.
148 */
149 ok(value: any, msg?: string, extra?: AssertOptions): void;
150 true: Test["ok"];
151 assert: Test["ok"];
152
153 /**
154 * Assert that value is falsy with an optional description message msg.
155 */
156 notOk(value: any, msg?: string, extra?: AssertOptions): void;
157 false: Test["notOk"];
158 notok: Test["notOk"];
159
160 /**
161 * Assert that err is falsy.
162 * If err is non-falsy, use its err.message as the description message.
163 */
164 error(err: any, msg?: string, extra?: AssertOptions): void;
165 ifError: Test["error"];
166 ifErr: Test["error"];
167 iferror: Test["error"];
168
169 /**
170 * Assert that a === b with an optional description msg.
171 */
172 equal(actual: any, expected: any, msg?: string, extra?: AssertOptions): void;
173 equals: Test["equal"];
174 isEqual: Test["equal"];
175 is: Test["equal"];
176 strictEqual: Test["equal"];
177 strictEquals: Test["equal"];
178
179 /**
180 * Assert that a !== b with an optional description msg.
181 */
182 notEqual(actual: any, expected: any, msg?: string, extra?: AssertOptions): void;
183 notEquals: Test["notEqual"];
184 notStrictEqual: Test["notEqual"];
185 notStrictEquals: Test["notEqual"];
186 isNotEqual: Test["notEqual"];
187 isNot: Test["notEqual"];
188 not: Test["notEqual"];
189 doesNotEqual: Test["notEqual"];
190 isInequal: Test["notEqual"];
191
192 /**
193 * Assert that actual == expected with an optional description of the assertion msg.
194 */
195 looseEqual(actual: any, expected: any, msg?: string, extra?: AssertOptions): void;
196 looseEquals: Test["looseEqual"];
197
198 /**
199 * Assert that actual != expected with an optional description of the assertion msg.
200 */
201 notLooseEqual(actual: any, expected: any, msg?: string, extra?: AssertOptions): void;
202 notLooseEquals: Test["looseEqual"];
203
204 /**
205 * Assert that a and b have the same structure and nested values using node's deepEqual() algorithm with strict comparisons (===) on leaf nodes and an optional description msg.
206 */
207 deepEqual(actual: any, expected: any, msg?: string, extra?: AssertOptions): void;
208 deepEquals: Test["deepEqual"];
209 isEquivalent: Test["deepEqual"];
210 same: Test["deepEqual"];
211
212 /**
213 * Assert that a and b do not have the same structure and nested values using node's deepEqual() algorithm with strict comparisons (===) on leaf nodes and an optional description msg.
214 */
215 notDeepEqual(actual: any, expected: any, msg?: string, extra?: AssertOptions): void;
216 notEquivalent: Test["notDeepEqual"];
217 notDeeply: Test["notDeepEqual"];
218 notSame: Test["notDeepEqual"];
219 isNotDeepEqual: Test["notDeepEqual"];
220 isNotDeeply: Test["notDeepEqual"];
221 isNotEquivalent: Test["notDeepEqual"];
222 isInequivalent: Test["notDeepEqual"];
223
224 /**
225 * Assert that a and b have the same structure and nested values using node's deepEqual() algorithm with loose comparisons (==) on leaf nodes and an optional description msg.
226 */
227 deepLooseEqual(actual: any, expected: any, msg?: string, extra?: AssertOptions): void;
228
229 /**
230 * Assert that a and b do not have the same structure and nested values using node's deepEqual() algorithm with loose comparisons (==) on leaf nodes and an optional description msg.
231 */
232 notDeepLooseEqual(actual: any, expected: any, msg?: string, extra?: AssertOptions): void;
233
234 /**
235 * Assert that the function call fn() throws an exception. expected, if present, must be a RegExp, Function, or Object.
236 */
237 throws(fn: () => void, msg?: string, extra?: AssertOptions): void;
238 throws(fn: () => void, exceptionExpected: object, msg?: string, extra?: AssertOptions): void;
239
240 /**
241 * Assert that the function call fn() does not throw an exception.
242 */
243 doesNotThrow(fn: () => void, msg?: string, extra?: AssertOptions): void;
244 doesNotThrow(fn: () => void, exceptionExpected: RegExp | Function, msg?: string, extra?: AssertOptions): void;
245
246 /**
247 * Print a message without breaking the tap output.
248 * (Useful when using e.g. tap-colorize where output is buffered & console.log will print in incorrect order vis-a-vis tap output.)
249 */
250 comment(msg: string): void;
251
252 /**
253 * Assert that string matches the RegExp regexp. Will throw (not just fail) when the first two arguments are the wrong type.
254 */
255 match(actual: string, expected: RegExp, msg?: string, extra?: AssertOptions): void;
256
257 /**
258 * Assert that string does not match the RegExp regexp. Will throw (not just fail) when the first two arguments are the wrong type.
259 */
260 doesNotMatch(actual: string, expected: RegExp, msg?: string, extra?: AssertOptions): void;
261
262 /**
263 * Register a callback to run after the individual test has completed. Multiple registered teardown callbacks will run in order.
264 */
265 teardown(callback: () => void | Promise<void>): void;
266 }
267}