UNPKG

13.1 kBTypeScriptView Raw
1/// <reference types="node" />
2
3import Through = require("@ljharb/through");
4import mockProperty = require("mock-property");
5
6/**
7 * Create a new test with an optional name string and optional opts object.
8 * cb(t) fires with the new test object t once all preceding tests have finished.
9 * Tests execute serially.
10 */
11declare function tape(name: string, cb: tape.TestCase): void;
12declare function tape(name: string, opts: tape.TestOptions, cb: tape.TestCase): void;
13declare function tape(cb: tape.TestCase): void;
14declare function tape(opts: tape.TestOptions, cb: tape.TestCase): void;
15
16declare namespace tape {
17 interface TestCase {
18 (test: Test): void | Promise<void>;
19 }
20
21 /**
22 * Available opts options for the tape function.
23 */
24 interface TestOptions {
25 /** See test.skip. */
26 skip?: boolean | string | undefined;
27 /** Set a timeout for the test, after which it will fail. See tape.timeoutAfter. */
28 timeout?: number | undefined;
29 /**
30 * Configure max depth of expected/actual object printing. Environmental variable
31 * NODE_TAPE_OBJECT_PRINT_DEPTH can set the desired default depth for all tests;
32 * locally-set values will take precedence.
33 */
34 objectPrintDepth?: number | undefined;
35 /** Test will be allowed to fail. */
36 todo?: boolean | string | undefined;
37 }
38
39 /**
40 * Available options for tape assertions.
41 */
42 interface AssertOptions {
43 /** Skip the assertion. Can also be a message explaining why the test is skipped. */
44 skip?: boolean | string | undefined;
45 /** Allows the assertion to fail. */
46 todo?: boolean | string | undefined;
47 /** An optional description of the assertion. */
48 message?: string | undefined;
49 }
50
51 /**
52 * Options for the createStream function.
53 */
54 interface StreamOptions {
55 objectMode?: boolean | undefined;
56 }
57
58 /**
59 * Generate a new test that will be skipped over.
60 */
61 export function skip(name: string, cb: tape.TestCase): void;
62 export function skip(name: string, opts: tape.TestOptions, cb: tape.TestCase): void;
63 export function skip(cb: tape.TestCase): void;
64 export function skip(opts: tape.TestOptions, cb: tape.TestCase): void;
65
66 /**
67 * The onFinish hook will get invoked when ALL tape tests have finished right before tape is about to print the test summary.
68 */
69 export function onFinish(cb: () => void): void;
70
71 /**
72 * The onFailure hook will get invoked whenever any tape tests have failed.
73 */
74 export function onFailure(cb: () => void): void;
75
76 /**
77 * 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.
78 */
79 export function only(name: string, cb: tape.TestCase): void;
80 export function only(name: string, opts: tape.TestOptions, cb: tape.TestCase): void;
81 export function only(cb: tape.TestCase): void;
82 export function only(opts: tape.TestOptions, cb: tape.TestCase): void;
83
84 /**
85 * Create a new test harness instance, which is a function like test(), but with a new pending stack and test state.
86 */
87 export function createHarness(opts?: { autoclose?: boolean; noOnly?: boolean }): typeof tape;
88
89 /**
90 * Create a memoized test harness instance, which is a function like test(), but with a new pending stack and test state.
91 */
92 export function getHarness(
93 opts?: {
94 noOnly?: boolean;
95 exit?: boolean;
96 stream?: ReturnType<typeof Through>;
97 } & StreamOptions,
98 ): typeof tape;
99
100 /**
101 * Create a stream of output, bypassing the default output stream that writes messages to console.log().
102 * 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.
103 */
104 export function createStream(opts?: tape.StreamOptions): NodeJS.ReadableStream;
105
106 type WithRequired<T, K extends keyof T> = T & { [P in K]-?: T[P] };
107
108 export interface Test {
109 /**
110 * Create a subtest with a new test handle st from cb(st) inside the current test.
111 * cb(st) will only fire when t finishes.
112 * Additional tests queued up after t will not be run until all subtests finish.
113 */
114 test(name: string, cb: tape.TestCase): void;
115 test(name: string, opts: TestOptions, cb: tape.TestCase): void;
116 test(
117 name: string,
118 opts: WithRequired<TestOptions, "skip"> | WithRequired<TestOptions, "todo">,
119 cb?: tape.TestCase,
120 ): void;
121
122 /**
123 * Declare that n assertions should be run. end() will be called automatically after the nth assertion.
124 * If there are any more assertions after the nth, or after end() is called, they will generate errors.
125 */
126 plan(n: number): void;
127
128 /**
129 * Declare the end of a test explicitly.
130 * If err is passed in t.end will assert that it is falsey.
131 */
132 end(err?: any): void;
133
134 /**
135 * Generate a failing assertion with a message msg.
136 */
137 fail(msg?: string, extra?: AssertOptions): void;
138
139 /**
140 * Generate a passing assertion with a message msg.
141 */
142 pass(msg?: string, extra?: AssertOptions): void;
143
144 /**
145 * Automatically timeout the test after X ms.
146 */
147 timeoutAfter(ms: number): void;
148
149 /**
150 * Generate an assertion that will be skipped over.
151 */
152 skip(msg?: string, extra?: AssertOptions): void;
153
154 /**
155 * Assert that value is truthy with an optional description message msg.
156 */
157 ok(value: any, msg?: string, extra?: AssertOptions): void;
158 true: Test["ok"];
159 assert: Test["ok"];
160
161 /**
162 * Assert that value is falsy with an optional description message msg.
163 */
164 notOk(value: any, msg?: string, extra?: AssertOptions): void;
165 false: Test["notOk"];
166 notok: Test["notOk"];
167
168 /**
169 * Assert that err is falsy.
170 * If err is non-falsy, use its err.message as the description message.
171 */
172 error(err: any, msg?: string, extra?: AssertOptions): void;
173 ifError: Test["error"];
174 ifErr: Test["error"];
175 iferror: Test["error"];
176
177 /**
178 * Assert that a === b with an optional description msg.
179 */
180 equal(actual: any, expected: any, msg?: string, extra?: AssertOptions): void;
181 equals: Test["equal"];
182 isEqual: Test["equal"];
183 is: Test["equal"];
184 strictEqual: Test["equal"];
185 strictEquals: Test["equal"];
186
187 /**
188 * Assert that a !== b with an optional description msg.
189 */
190 notEqual(actual: any, expected: any, msg?: string, extra?: AssertOptions): void;
191 notEquals: Test["notEqual"];
192 notStrictEqual: Test["notEqual"];
193 notStrictEquals: Test["notEqual"];
194 isNotEqual: Test["notEqual"];
195 isNot: Test["notEqual"];
196 not: Test["notEqual"];
197 doesNotEqual: Test["notEqual"];
198 isInequal: Test["notEqual"];
199
200 /**
201 * Assert that actual == expected with an optional description of the assertion msg.
202 */
203 looseEqual(actual: any, expected: any, msg?: string, extra?: AssertOptions): void;
204 looseEquals: Test["looseEqual"];
205
206 /**
207 * Assert that actual != expected with an optional description of the assertion msg.
208 */
209 notLooseEqual(actual: any, expected: any, msg?: string, extra?: AssertOptions): void;
210 notLooseEquals: Test["looseEqual"];
211
212 /**
213 * 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.
214 */
215 deepEqual(actual: any, expected: any, msg?: string, extra?: AssertOptions): void;
216 deepEquals: Test["deepEqual"];
217 isEquivalent: Test["deepEqual"];
218 same: Test["deepEqual"];
219
220 /**
221 * 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.
222 */
223 notDeepEqual(actual: any, expected: any, msg?: string, extra?: AssertOptions): void;
224 notEquivalent: Test["notDeepEqual"];
225 notDeeply: Test["notDeepEqual"];
226 notSame: Test["notDeepEqual"];
227 isNotDeepEqual: Test["notDeepEqual"];
228 isNotDeeply: Test["notDeepEqual"];
229 isNotEquivalent: Test["notDeepEqual"];
230 isInequivalent: Test["notDeepEqual"];
231
232 /**
233 * 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.
234 */
235 deepLooseEqual(actual: any, expected: any, msg?: string, extra?: AssertOptions): void;
236
237 /**
238 * 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.
239 */
240 notDeepLooseEqual(actual: any, expected: any, msg?: string, extra?: AssertOptions): void;
241
242 /**
243 * Assert that the function call fn() throws an exception. expected, if present, must be a RegExp, Function, or Object.
244 */
245 throws(fn: () => void, msg?: string, extra?: AssertOptions): void;
246 throws(fn: () => void, exceptionExpected: object, msg?: string, extra?: AssertOptions): void;
247
248 /**
249 * Assert that the function call fn() does not throw an exception.
250 */
251 doesNotThrow(fn: () => void, msg?: string, extra?: AssertOptions): void;
252 // we actually do want any function-like value here, especially constructors
253 doesNotThrow(fn: () => void, exceptionExpected: RegExp | Function, msg?: string, extra?: AssertOptions): void;
254
255 /**
256 * Print a message without breaking the tap output.
257 * (Useful when using e.g. tap-colorize where output is buffered & console.log will print in incorrect order vis-a-vis tap output.)
258 */
259 comment(msg: string): void;
260
261 /**
262 * Assert that string matches the RegExp regexp. Will throw (not just fail) when the first two arguments are the wrong type.
263 */
264 match(actual: string, expected: RegExp, msg?: string, extra?: AssertOptions): void;
265
266 /**
267 * Assert that string does not match the RegExp regexp. Will throw (not just fail) when the first two arguments are the wrong type.
268 */
269 doesNotMatch(actual: string, expected: RegExp, msg?: string, extra?: AssertOptions): void;
270
271 /**
272 * Register a callback to run after the individual test has completed. Multiple registered teardown callbacks will run in order.
273 */
274 teardown(callback: () => void | Promise<void>): void;
275
276 captureFn<X extends SyncOrAsyncCallback>(this: Test, original: X): WrappedFn<X>;
277 // eslint-disable-next-line @definitelytyped/no-unnecessary-generics
278 capture<T extends SyncOrAsyncCallback>(
279 this: Test,
280 obj: Record<PropertyKey, unknown> | unknown[],
281 method: PropertyKey,
282 implementation?: T,
283 ): WrapResults;
284 intercept(
285 obj: Record<PropertyKey, unknown> | unknown[],
286 property: PropertyKey,
287 desc?: PropertyDescriptor,
288 ): InterceptResults;
289
290 assertion<Args extends readonly any[], R>(
291 this: Test,
292 fn: (this: Test, ...args: Args) => R,
293 ...args: Args
294 ): R;
295 }
296
297 export type SyncCallback = (...args: unknown[]) => unknown;
298 export type SyncOrAsyncCallback = (...args: unknown[]) => unknown;
299
300 export interface ReturnCall {
301 args: unknown[];
302 receiver: {};
303 returned: unknown;
304 }
305
306 export interface ThrowCall {
307 args: unknown[];
308 receiver: {};
309 threw: true;
310 }
311
312 export interface Call {
313 type: "get" | "set";
314 success: boolean;
315 value: unknown;
316 args: unknown[];
317 receiver: unknown;
318 }
319
320 export type RestoreFunction = ReturnType<typeof mockProperty>;
321
322 export interface WrapResults {
323 (): WrappedCall[];
324 restore?: RestoreFunction;
325 }
326
327 export interface WrappedFn<T extends SyncOrAsyncCallback> {
328 (this: ThisParameterType<T>, ...args: Parameters<T>): ReturnType<T>;
329 calls?: WrappedCall[];
330 }
331
332 export interface WrapObject<T extends SyncOrAsyncCallback> {
333 __proto__: null;
334 wrapped: WrappedFn<T>;
335 calls: WrappedCall[];
336 results: WrapResults;
337 }
338
339 export type WrappedCall = ReturnCall | ThrowCall;
340
341 export interface InterceptResults {
342 (): Call[];
343 restore: RestoreFunction;
344 }
345}
346
347export = tape;