UNPKG

4.36 kBTypeScriptView Raw
1import { InspectionFailure, PropertyMatcher } from './have-resource';
2/**
3 * A matcher for an object that contains at least the given fields with the given matchers (or literals)
4 *
5 * Only does lenient matching one level deep, at the next level all objects must declare the
6 * exact expected keys again.
7 */
8export declare function objectLike<A extends object>(pattern: A): PropertyMatcher;
9/**
10 * A matcher for an object that contains at least the given fields with the given matchers (or literals)
11 *
12 * Switches to "deep" lenient matching. Nested objects also only need to contain declared keys.
13 */
14export declare function deepObjectLike<A extends object>(pattern: A): PropertyMatcher;
15/**
16 * Match exactly the given value
17 *
18 * This is the default, you only need this to escape from the deep lenient matching
19 * of `deepObjectLike`.
20 */
21export declare function exactValue(expected: any): PropertyMatcher;
22/**
23 * A matcher for a list that contains all of the given elements in any order
24 */
25export declare function arrayWith(...elements: any[]): PropertyMatcher;
26/**
27 * Helper function to make matcher failure reporting a little easier
28 *
29 * Our protocol is weird (change a string on a passed-in object and return 'false'),
30 * but I don't want to change that right now.
31 */
32export declare function failMatcher(inspection: InspectionFailure, error: string): boolean;
33/**
34 * Match a given literal value against a matcher
35 *
36 * If the matcher is a callable, use that to evaluate the value. Otherwise, the values
37 * must be literally the same.
38 */
39export declare function match(value: any, matcher: any, inspection: InspectionFailure): boolean;
40/**
41 * Do a glob-like pattern match (which only supports *s). Supports multiline strings.
42 */
43export declare function stringLike(pattern: string): PropertyMatcher;
44/**
45 * Matches any value
46 */
47export declare function anything(): PropertyMatcher;
48/**
49 * Negate an inner matcher
50 */
51export declare function notMatching(matcher: any): PropertyMatcher;
52export declare type TypeValidator<T> = (x: any) => x is T;
53/**
54 * Captures a value onto an object if it matches a given inner matcher
55 *
56 * @example
57 *
58 * const someValue = Capture.aString();
59 * expect(stack).toHaveResource({
60 * // ...
61 * Value: someValue.capture(stringMatching('*a*')),
62 * });
63 * console.log(someValue.capturedValue);
64 */
65export declare class Capture<T = any> {
66 private readonly typeValidator?;
67 /**
68 * A Capture object that captures any type
69 */
70 static anyType(): Capture<any>;
71 /**
72 * A Capture object that captures a string type
73 */
74 static aString(): Capture<string>;
75 /**
76 * A Capture object that captures a custom type
77 */
78 static a<T>(validator: TypeValidator<T>): Capture<T>;
79 private _value?;
80 private _didCapture;
81 private _wasInvoked;
82 protected constructor(typeValidator?: TypeValidator<T> | undefined);
83 /**
84 * Capture the value if the inner matcher successfully matches it
85 *
86 * If no matcher is given, `anything()` is assumed.
87 *
88 * And exception will be thrown if the inner matcher returns `true` and
89 * the value turns out to be of a different type than the `Capture` object
90 * is expecting.
91 */
92 capture(matcher?: any): PropertyMatcher;
93 /**
94 * Whether a value was successfully captured
95 */
96 get didCapture(): boolean;
97 /**
98 * Return the value that was captured
99 *
100 * Throws an exception if now value was captured
101 */
102 get capturedValue(): T;
103}
104/**
105 * Match on the innards of a JSON string, instead of the complete string
106 */
107export declare function encodedJson(matcher: any): PropertyMatcher;
108/**
109 * Make a matcher out of the given argument if it's not a matcher already
110 *
111 * If it's not a matcher, it will be treated as a literal.
112 */
113export declare function matcherFrom(matcher: any): PropertyMatcher;
114/**
115 * Annotate a matcher with toJSON
116 *
117 * We will JSON.stringify() values if we have a match failure, but for matchers this
118 * would show (in traditional JS fashion) something like '[function Function]', or more
119 * accurately nothing at all since functions cannot be JSONified.
120 *
121 * We override to JSON() in order to produce a readadable version of the matcher.
122 */
123export declare function annotateMatcher<A extends object>(how: A, matcher: PropertyMatcher): PropertyMatcher;