UNPKG

11.9 kBTypeScriptView Raw
1/**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7import type {EqualsFunction} from '@jest/expect-utils';
8import type * as jestMatcherUtils from 'jest-matcher-utils';
9import {Tester} from '@jest/expect-utils';
10import {TesterContext} from '@jest/expect-utils';
11
12export declare abstract class AsymmetricMatcher<T>
13 implements AsymmetricMatcher_2
14{
15 protected sample: T;
16 protected inverse: boolean;
17 $$typeof: symbol;
18 constructor(sample: T, inverse?: boolean);
19 protected getMatcherContext(): MatcherContext;
20 abstract asymmetricMatch(other: unknown): boolean;
21 abstract toString(): string;
22 getExpectedType?(): string;
23 toAsymmetricMatcher?(): string;
24}
25
26declare type AsymmetricMatcher_2 = {
27 asymmetricMatch(other: unknown): boolean;
28 toString(): string;
29 getExpectedType?(): string;
30 toAsymmetricMatcher?(): string;
31};
32
33export declare interface AsymmetricMatchers {
34 any(sample: unknown): AsymmetricMatcher_2;
35 anything(): AsymmetricMatcher_2;
36 arrayContaining(sample: Array<unknown>): AsymmetricMatcher_2;
37 closeTo(sample: number, precision?: number): AsymmetricMatcher_2;
38 objectContaining(sample: Record<string, unknown>): AsymmetricMatcher_2;
39 stringContaining(sample: string): AsymmetricMatcher_2;
40 stringMatching(sample: string | RegExp): AsymmetricMatcher_2;
41}
42
43export declare type AsyncExpectationResult = Promise<SyncExpectationResult>;
44
45export declare interface BaseExpect {
46 assertions(numberOfAssertions: number): void;
47 addEqualityTesters(testers: Array<Tester>): void;
48 extend(matchers: MatchersObject): void;
49 extractExpectedAssertionsErrors(): ExpectedAssertionsErrors;
50 getState(): MatcherState;
51 hasAssertions(): void;
52 setState(state: Partial<MatcherState>): void;
53}
54
55export declare type Expect = {
56 <T = unknown>(actual: T): Matchers<void, T> &
57 Inverse<Matchers<void, T>> &
58 PromiseMatchers<T>;
59} & BaseExpect &
60 AsymmetricMatchers &
61 Inverse<Omit<AsymmetricMatchers, 'any' | 'anything'>>;
62
63declare const expect: Expect;
64export default expect;
65export {expect};
66
67export declare type ExpectationResult =
68 | SyncExpectationResult
69 | AsyncExpectationResult;
70
71declare type ExpectedAssertionsErrors = Array<{
72 actual: string | number;
73 error: Error;
74 expected: string;
75}>;
76
77declare type Inverse<Matchers> = {
78 /**
79 * Inverse next matcher. If you know how to test something, `.not` lets you test its opposite.
80 */
81 not: Matchers;
82};
83
84export declare class JestAssertionError extends Error {
85 matcherResult?: Omit<SyncExpectationResult, 'message'> & {
86 message: string;
87 };
88}
89
90export declare type MatcherContext = MatcherUtils & Readonly<MatcherState>;
91
92export declare type MatcherFunction<Expected extends Array<unknown> = []> =
93 MatcherFunctionWithContext<MatcherContext, Expected>;
94
95export declare type MatcherFunctionWithContext<
96 Context extends MatcherContext = MatcherContext,
97 Expected extends Array<any> = [] /** TODO should be: extends Array<unknown> = [] */,
98> = (
99 this: Context,
100 actual: unknown,
101 ...expected: Expected
102) => ExpectationResult;
103
104export declare interface Matchers<R extends void | Promise<void>, T = unknown> {
105 /**
106 * Ensures the last call to a mock function was provided specific args.
107 */
108 lastCalledWith(...expected: Array<unknown>): R;
109 /**
110 * Ensure that the last call to a mock function has returned a specified value.
111 */
112 lastReturnedWith(expected?: unknown): R;
113 /**
114 * Ensure that a mock function is called with specific arguments on an Nth call.
115 */
116 nthCalledWith(nth: number, ...expected: Array<unknown>): R;
117 /**
118 * Ensure that the nth call to a mock function has returned a specified value.
119 */
120 nthReturnedWith(nth: number, expected?: unknown): R;
121 /**
122 * Checks that a value is what you expect. It calls `Object.is` to compare values.
123 * Don't use `toBe` with floating-point numbers.
124 */
125 toBe(expected: unknown): R;
126 /**
127 * Ensures that a mock function is called.
128 */
129 toBeCalled(): R;
130 /**
131 * Ensures that a mock function is called an exact number of times.
132 */
133 toBeCalledTimes(expected: number): R;
134 /**
135 * Ensure that a mock function is called with specific arguments.
136 */
137 toBeCalledWith(...expected: Array<unknown>): R;
138 /**
139 * Using exact equality with floating point numbers is a bad idea.
140 * Rounding means that intuitive things fail.
141 * The default for `precision` is 2.
142 */
143 toBeCloseTo(expected: number, precision?: number): R;
144 /**
145 * Ensure that a variable is not undefined.
146 */
147 toBeDefined(): R;
148 /**
149 * When you don't care what a value is, you just want to
150 * ensure a value is false in a boolean context.
151 */
152 toBeFalsy(): R;
153 /**
154 * For comparing floating point numbers.
155 */
156 toBeGreaterThan(expected: number | bigint): R;
157 /**
158 * For comparing floating point numbers.
159 */
160 toBeGreaterThanOrEqual(expected: number | bigint): R;
161 /**
162 * Ensure that an object is an instance of a class.
163 * This matcher uses `instanceof` underneath.
164 */
165 toBeInstanceOf(expected: unknown): R;
166 /**
167 * For comparing floating point numbers.
168 */
169 toBeLessThan(expected: number | bigint): R;
170 /**
171 * For comparing floating point numbers.
172 */
173 toBeLessThanOrEqual(expected: number | bigint): R;
174 /**
175 * Used to check that a variable is NaN.
176 */
177 toBeNaN(): R;
178 /**
179 * This is the same as `.toBe(null)` but the error messages are a bit nicer.
180 * So use `.toBeNull()` when you want to check that something is null.
181 */
182 toBeNull(): R;
183 /**
184 * Use when you don't care what a value is, you just want to ensure a value
185 * is true in a boolean context. In JavaScript, there are six falsy values:
186 * `false`, `0`, `''`, `null`, `undefined`, and `NaN`. Everything else is truthy.
187 */
188 toBeTruthy(): R;
189 /**
190 * Used to check that a variable is undefined.
191 */
192 toBeUndefined(): R;
193 /**
194 * Used when you want to check that an item is in a list.
195 * For testing the items in the list, this uses `===`, a strict equality check.
196 */
197 toContain(expected: unknown): R;
198 /**
199 * Used when you want to check that an item is in a list.
200 * For testing the items in the list, this matcher recursively checks the
201 * equality of all fields, rather than checking for object identity.
202 */
203 toContainEqual(expected: unknown): R;
204 /**
205 * Used when you want to check that two objects have the same value.
206 * This matcher recursively checks the equality of all fields, rather than checking for object identity.
207 */
208 toEqual(expected: unknown): R;
209 /**
210 * Ensures that a mock function is called.
211 */
212 toHaveBeenCalled(): R;
213 /**
214 * Ensures that a mock function is called an exact number of times.
215 */
216 toHaveBeenCalledTimes(expected: number): R;
217 /**
218 * Ensure that a mock function is called with specific arguments.
219 */
220 toHaveBeenCalledWith(...expected: Array<unknown>): R;
221 /**
222 * Ensure that a mock function is called with specific arguments on an Nth call.
223 */
224 toHaveBeenNthCalledWith(nth: number, ...expected: Array<unknown>): R;
225 /**
226 * If you have a mock function, you can use `.toHaveBeenLastCalledWith`
227 * to test what arguments it was last called with.
228 */
229 toHaveBeenLastCalledWith(...expected: Array<unknown>): R;
230 /**
231 * Use to test the specific value that a mock function last returned.
232 * If the last call to the mock function threw an error, then this matcher will fail
233 * no matter what value you provided as the expected return value.
234 */
235 toHaveLastReturnedWith(expected?: unknown): R;
236 /**
237 * Used to check that an object has a `.length` property
238 * and it is set to a certain numeric value.
239 */
240 toHaveLength(expected: number): R;
241 /**
242 * Use to test the specific value that a mock function returned for the nth call.
243 * If the nth call to the mock function threw an error, then this matcher will fail
244 * no matter what value you provided as the expected return value.
245 */
246 toHaveNthReturnedWith(nth: number, expected?: unknown): R;
247 /**
248 * Use to check if property at provided reference keyPath exists for an object.
249 * For checking deeply nested properties in an object you may use dot notation or an array containing
250 * the keyPath for deep references.
251 *
252 * Optionally, you can provide a value to check if it's equal to the value present at keyPath
253 * on the target object. This matcher uses 'deep equality' (like `toEqual()`) and recursively checks
254 * the equality of all fields.
255 *
256 * @example
257 *
258 * expect(houseForSale).toHaveProperty('kitchen.area', 20);
259 */
260 toHaveProperty(
261 expectedPath: string | Array<string>,
262 expectedValue?: unknown,
263 ): R;
264 /**
265 * Use to test that the mock function successfully returned (i.e., did not throw an error) at least one time
266 */
267 toHaveReturned(): R;
268 /**
269 * Use to ensure that a mock function returned successfully (i.e., did not throw an error) an exact number of times.
270 * Any calls to the mock function that throw an error are not counted toward the number of times the function returned.
271 */
272 toHaveReturnedTimes(expected: number): R;
273 /**
274 * Use to ensure that a mock function returned a specific value.
275 */
276 toHaveReturnedWith(expected?: unknown): R;
277 /**
278 * Check that a string matches a regular expression.
279 */
280 toMatch(expected: string | RegExp): R;
281 /**
282 * Used to check that a JavaScript object matches a subset of the properties of an object
283 */
284 toMatchObject(
285 expected: Record<string, unknown> | Array<Record<string, unknown>>,
286 ): R;
287 /**
288 * Ensure that a mock function has returned (as opposed to thrown) at least once.
289 */
290 toReturn(): R;
291 /**
292 * Ensure that a mock function has returned (as opposed to thrown) a specified number of times.
293 */
294 toReturnTimes(expected: number): R;
295 /**
296 * Ensure that a mock function has returned a specified value at least once.
297 */
298 toReturnWith(expected?: unknown): R;
299 /**
300 * Use to test that objects have the same types as well as structure.
301 */
302 toStrictEqual(expected: unknown): R;
303 /**
304 * Used to test that a function throws when it is called.
305 */
306 toThrow(expected?: unknown): R;
307 /**
308 * If you want to test that a specific error is thrown inside a function.
309 */
310 toThrowError(expected?: unknown): R;
311}
312
313declare type MatchersObject = {
314 [name: string]: RawMatcherFn;
315};
316
317export declare interface MatcherState {
318 assertionCalls: number;
319 currentConcurrentTestName?: () => string | undefined;
320 currentTestName?: string;
321 error?: Error;
322 expand?: boolean;
323 expectedAssertionsNumber: number | null;
324 expectedAssertionsNumberError?: Error;
325 isExpectingAssertions: boolean;
326 isExpectingAssertionsError?: Error;
327 isNot?: boolean;
328 numPassingAsserts: number;
329 promise?: string;
330 suppressedErrors: Array<Error>;
331 testPath?: string;
332}
333
334export declare interface MatcherUtils {
335 customTesters: Array<Tester>;
336 dontThrow(): void;
337 equals: EqualsFunction;
338 utils: typeof jestMatcherUtils & {
339 iterableEquality: Tester;
340 subsetEquality: Tester;
341 };
342}
343
344declare type PromiseMatchers<T = unknown> = {
345 /**
346 * Unwraps the reason of a rejected promise so any other matcher can be chained.
347 * If the promise is fulfilled the assertion fails.
348 */
349 rejects: Matchers<Promise<void>, T> & Inverse<Matchers<Promise<void>, T>>;
350 /**
351 * Unwraps the value of a fulfilled promise so any other matcher can be chained.
352 * If the promise is rejected the assertion fails.
353 */
354 resolves: Matchers<Promise<void>, T> & Inverse<Matchers<Promise<void>, T>>;
355};
356
357declare type RawMatcherFn<Context extends MatcherContext = MatcherContext> = {
358 (this: Context, actual: any, ...expected: Array<any>): ExpectationResult;
359};
360
361export declare type SyncExpectationResult = {
362 pass: boolean;
363 message(): string;
364};
365
366export {Tester};
367
368export {TesterContext};
369
370export {};