UNPKG

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