@vitest/expect 3.2.0-beta.1 → 3.2.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -9,6 +9,70 @@ declare const JEST_MATCHERS_OBJECT: unique symbol;
9
9
  declare const GLOBAL_EXPECT: unique symbol;
10
10
  declare const ASYMMETRIC_MATCHERS_OBJECT: unique symbol;
11
11
 
12
+ /* eslint-disable unicorn/no-instanceof-builtins -- we check both */
13
+
14
+ interface AsymmetricMatcherInterface {
15
+ asymmetricMatch: (other: unknown) => boolean;
16
+ toString: () => string;
17
+ getExpectedType?: () => string;
18
+ toAsymmetricMatcher?: () => string;
19
+ }
20
+ declare abstract class AsymmetricMatcher<
21
+ T,
22
+ State extends MatcherState = MatcherState
23
+ > implements AsymmetricMatcherInterface {
24
+ protected sample: T;
25
+ protected inverse: boolean;
26
+ // should have "jest" to be compatible with its ecosystem
27
+ $$typeof: symbol;
28
+ constructor(sample: T, inverse?: boolean);
29
+ protected getMatcherContext(expect?: Chai.ExpectStatic): State;
30
+ abstract asymmetricMatch(other: unknown): boolean;
31
+ abstract toString(): string;
32
+ getExpectedType?(): string;
33
+ toAsymmetricMatcher?(): string;
34
+ }
35
+ declare class StringContaining extends AsymmetricMatcher<string> {
36
+ constructor(sample: string, inverse?: boolean);
37
+ asymmetricMatch(other: string): boolean;
38
+ toString(): string;
39
+ getExpectedType(): string;
40
+ }
41
+ declare class Anything extends AsymmetricMatcher<void> {
42
+ asymmetricMatch(other: unknown): boolean;
43
+ toString(): string;
44
+ toAsymmetricMatcher(): string;
45
+ }
46
+ declare class ObjectContaining extends AsymmetricMatcher<Record<string, unknown>> {
47
+ constructor(sample: Record<string, unknown>, inverse?: boolean);
48
+ getPrototype(obj: object): any;
49
+ hasProperty(obj: object | null, property: string): boolean;
50
+ asymmetricMatch(other: any): boolean;
51
+ toString(): string;
52
+ getExpectedType(): string;
53
+ }
54
+ declare class ArrayContaining<T = unknown> extends AsymmetricMatcher<Array<T>> {
55
+ constructor(sample: Array<T>, inverse?: boolean);
56
+ asymmetricMatch(other: Array<T>): boolean;
57
+ toString(): string;
58
+ getExpectedType(): string;
59
+ }
60
+ declare class Any extends AsymmetricMatcher<any> {
61
+ constructor(sample: unknown);
62
+ fnNameFor(func: Function): string;
63
+ asymmetricMatch(other: unknown): boolean;
64
+ toString(): string;
65
+ getExpectedType(): string;
66
+ toAsymmetricMatcher(): string;
67
+ }
68
+ declare class StringMatching extends AsymmetricMatcher<RegExp> {
69
+ constructor(sample: string | RegExp, inverse?: boolean);
70
+ asymmetricMatch(other: string): boolean;
71
+ toString(): string;
72
+ getExpectedType(): string;
73
+ }
74
+ declare const JestAsymmetricMatchers: ChaiPlugin;
75
+
12
76
  declare function matcherHint(matcherName: string, received?: string, expected?: string, options?: MatcherHintOptions): string;
13
77
  declare function printReceived(object: unknown): string;
14
78
  declare function printExpected(value: unknown): string;
@@ -65,7 +129,9 @@ interface MatcherState {
65
129
  isExpectingAssertions?: boolean;
66
130
  isExpectingAssertionsError?: Error | null;
67
131
  isNot: boolean;
132
+ // environment: VitestEnvironment
68
133
  promise: string;
134
+ // snapshotState: SnapshotState
69
135
  suppressedErrors: Array<Error>;
70
136
  testPath?: string;
71
137
  utils: ReturnType<typeof getMatcherUtils> & {
@@ -85,15 +151,22 @@ interface SyncExpectationResult {
85
151
  }
86
152
  type AsyncExpectationResult = Promise<SyncExpectationResult>;
87
153
  type ExpectationResult = SyncExpectationResult | AsyncExpectationResult;
88
- interface RawMatcherFn<T extends MatcherState = MatcherState> {
89
- (this: T, received: any, ...expected: Array<any>): ExpectationResult;
154
+ interface RawMatcherFn<
155
+ T extends MatcherState = MatcherState,
156
+ E extends Array<any> = Array<any>
157
+ > {
158
+ (this: T, received: any, ...expected: E): ExpectationResult;
90
159
  }
91
- type MatchersObject<T extends MatcherState = MatcherState> = Record<string, RawMatcherFn<T>> & ThisType<T>;
92
- interface ExpectStatic extends Chai.ExpectStatic, AsymmetricMatchersContaining {
160
+ // Allow unused `T` to preserve its name for extensions.
161
+ // Type parameter names must be identical when extending those types.
162
+ // eslint-disable-next-line
163
+ interface Matchers<T = any> {}
164
+ type MatchersObject<T extends MatcherState = MatcherState> = Record<string, RawMatcherFn<T>> & ThisType<T> & { [K in keyof Matchers<T>]? : RawMatcherFn<T, Parameters<Matchers<T>[K]>> };
165
+ interface ExpectStatic extends Chai.ExpectStatic, Matchers, AsymmetricMatchersContaining {
93
166
  <T>(actual: T, message?: string): Assertion<T>;
94
167
  extend: (expects: MatchersObject) => void;
95
- anything: () => any;
96
- any: (constructor: unknown) => any;
168
+ anything: () => AsymmetricMatcher<unknown>;
169
+ any: (constructor: unknown) => AsymmetricMatcher<unknown>;
97
170
  getState: () => MatcherState;
98
171
  setState: (state: Partial<MatcherState>) => void;
99
172
  not: AsymmetricMatchersContaining;
@@ -135,14 +208,14 @@ interface AsymmetricMatchersContaining extends CustomMatcher {
135
208
  * @example
136
209
  * expect({ a: '1', b: 2 }).toEqual(expect.objectContaining({ a: '1' }))
137
210
  */
138
- objectContaining: <T = any>(expected: T) => any;
211
+ objectContaining: <T = any>(expected: DeeplyAllowMatchers<T>) => any;
139
212
  /**
140
213
  * Matches if the received array contains all elements in the expected array.
141
214
  *
142
215
  * @example
143
216
  * expect(['a', 'b', 'c']).toEqual(expect.arrayContaining(['b', 'a']));
144
217
  */
145
- arrayContaining: <T = unknown>(expected: Array<T>) => any;
218
+ arrayContaining: <T = unknown>(expected: Array<DeeplyAllowMatchers<T>>) => any;
146
219
  /**
147
220
  * Matches if the received string or regex matches the expected pattern.
148
221
  *
@@ -162,6 +235,7 @@ interface AsymmetricMatchersContaining extends CustomMatcher {
162
235
  */
163
236
  closeTo: (expected: number, precision?: number) => any;
164
237
  }
238
+ type DeeplyAllowMatchers<T> = T extends Array<infer Element> ? (DeeplyAllowMatchers<Element> | Element)[] : T extends object ? { [K in keyof T] : DeeplyAllowMatchers<T[K]> | AsymmetricMatcher<unknown> } : T;
165
239
  interface JestAssertion<T = any> extends jest.Matchers<void, T>, CustomMatcher {
166
240
  /**
167
241
  * Used when you want to check that two objects have the same value.
@@ -170,14 +244,14 @@ interface JestAssertion<T = any> extends jest.Matchers<void, T>, CustomMatcher {
170
244
  * @example
171
245
  * expect(user).toEqual({ name: 'Alice', age: 30 });
172
246
  */
173
- toEqual: <E>(expected: E) => void;
247
+ toEqual: <E>(expected: DeeplyAllowMatchers<E>) => void;
174
248
  /**
175
249
  * Use to test that objects have the same types as well as structure.
176
250
  *
177
251
  * @example
178
252
  * expect(user).toStrictEqual({ name: 'Alice', age: 30 });
179
253
  */
180
- toStrictEqual: <E>(expected: E) => void;
254
+ toStrictEqual: <E>(expected: DeeplyAllowMatchers<E>) => void;
181
255
  /**
182
256
  * Checks that a value is what you expect. It calls `Object.is` to compare values.
183
257
  * Don't use `toBe` with floating-point numbers.
@@ -204,7 +278,7 @@ interface JestAssertion<T = any> extends jest.Matchers<void, T>, CustomMatcher {
204
278
  * address: { city: 'Wonderland' }
205
279
  * });
206
280
  */
207
- toMatchObject: <E extends object | any[]>(expected: E) => void;
281
+ toMatchObject: <E extends object | any[]>(expected: DeeplyAllowMatchers<E>) => void;
208
282
  /**
209
283
  * Used when you want to check that an item is in a list.
210
284
  * For testing the items in the list, this uses `===`, a strict equality check.
@@ -556,7 +630,7 @@ type VitestAssertion<
556
630
  > = { [K in keyof A] : A[K] extends Chai.Assertion ? Assertion<T> : A[K] extends (...args: any[]) => any ? A[K] : VitestAssertion<A[K], T> } & ((type: string, message?: string) => Assertion);
557
631
  type Promisify<O> = { [K in keyof O] : O[K] extends (...args: infer A) => infer R ? Promisify<O[K]> & ((...args: A) => Promise<R>) : O[K] };
558
632
  type PromisifyAssertion<T> = Promisify<Assertion<T>>;
559
- interface Assertion<T = any> extends VitestAssertion<Chai.Assertion, T>, JestAssertion<T> {
633
+ interface Assertion<T = any> extends VitestAssertion<Chai.Assertion, T>, JestAssertion<T>, Matchers<T> {
560
634
  /**
561
635
  * Ensures a value is of a specific type.
562
636
  *
@@ -661,81 +735,50 @@ interface Assertion<T = any> extends VitestAssertion<Chai.Assertion, T>, JestAss
661
735
  rejects: PromisifyAssertion<T>;
662
736
  }
663
737
  declare global {
738
+ // support augmenting jest.Matchers by other libraries
739
+ // eslint-disable-next-line ts/no-namespace
664
740
  namespace jest {
665
- interface Matchers<
741
+ // eslint-disable-next-line unused-imports/no-unused-vars, ts/no-empty-object-type
742
+ interface Matchers<
666
743
  R,
667
744
  T = {}
668
745
  > {}
669
746
  }
670
747
  }
671
748
 
749
+ // selectively ported from https://github.com/jest-community/jest-extended
672
750
  declare const customMatchers: MatchersObject;
673
751
 
674
- interface AsymmetricMatcherInterface {
675
- asymmetricMatch: (other: unknown) => boolean;
676
- toString: () => string;
677
- getExpectedType?: () => string;
678
- toAsymmetricMatcher?: () => string;
679
- }
680
- declare abstract class AsymmetricMatcher<
681
- T,
682
- State extends MatcherState = MatcherState
683
- > implements AsymmetricMatcherInterface {
684
- protected sample: T;
685
- protected inverse: boolean;
686
- $$typeof: symbol;
687
- constructor(sample: T, inverse?: boolean);
688
- protected getMatcherContext(expect?: Chai.ExpectStatic): State;
689
- abstract asymmetricMatch(other: unknown): boolean;
690
- abstract toString(): string;
691
- getExpectedType?(): string;
692
- toAsymmetricMatcher?(): string;
693
- }
694
- declare class StringContaining extends AsymmetricMatcher<string> {
695
- constructor(sample: string, inverse?: boolean);
696
- asymmetricMatch(other: string): boolean;
697
- toString(): string;
698
- getExpectedType(): string;
699
- }
700
- declare class Anything extends AsymmetricMatcher<void> {
701
- asymmetricMatch(other: unknown): boolean;
702
- toString(): string;
703
- toAsymmetricMatcher(): string;
704
- }
705
- declare class ObjectContaining extends AsymmetricMatcher<Record<string, unknown>> {
706
- constructor(sample: Record<string, unknown>, inverse?: boolean);
707
- getPrototype(obj: object): any;
708
- hasProperty(obj: object | null, property: string): boolean;
709
- asymmetricMatch(other: any): boolean;
710
- toString(): string;
711
- getExpectedType(): string;
712
- }
713
- declare class ArrayContaining<T = unknown> extends AsymmetricMatcher<Array<T>> {
714
- constructor(sample: Array<T>, inverse?: boolean);
715
- asymmetricMatch(other: Array<T>): boolean;
716
- toString(): string;
717
- getExpectedType(): string;
718
- }
719
- declare class Any extends AsymmetricMatcher<any> {
720
- constructor(sample: unknown);
721
- fnNameFor(func: Function): string;
722
- asymmetricMatch(other: unknown): boolean;
723
- toString(): string;
724
- getExpectedType(): string;
725
- toAsymmetricMatcher(): string;
726
- }
727
- declare class StringMatching extends AsymmetricMatcher<RegExp> {
728
- constructor(sample: string | RegExp, inverse?: boolean);
729
- asymmetricMatch(other: string): boolean;
730
- toString(): string;
731
- getExpectedType(): string;
732
- }
733
- declare const JestAsymmetricMatchers: ChaiPlugin;
734
-
752
+ // Jest Expect Compact
735
753
  declare const JestChaiExpect: ChaiPlugin;
736
754
 
737
755
  declare const JestExtend: ChaiPlugin;
738
756
 
757
+ /*
758
+ Copyright (c) 2008-2016 Pivotal Labs
759
+
760
+ Permission is hereby granted, free of charge, to any person obtaining
761
+ a copy of this software and associated documentation files (the
762
+ "Software"), to deal in the Software without restriction, including
763
+ without limitation the rights to use, copy, modify, merge, publish,
764
+ distribute, sublicense, and/or sell copies of the Software, and to
765
+ permit persons to whom the Software is furnished to do so, subject to
766
+ the following conditions:
767
+
768
+ The above copyright notice and this permission notice shall be
769
+ included in all copies or substantial portions of the Software.
770
+
771
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
772
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
773
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
774
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
775
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
776
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
777
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
778
+
779
+ */
780
+
781
+ // Extracted out of jasmine 2.5.2
739
782
  declare function equals(a: unknown, b: unknown, customTesters?: Array<Tester>, strictCheck?: boolean): boolean;
740
783
  declare function isAsymmetric(obj: any): boolean;
741
784
  declare function hasAsymmetric(obj: any, seen?: Set<any>): boolean;
@@ -761,4 +804,4 @@ declare function getState<State extends MatcherState = MatcherState>(expect: Exp
761
804
  declare function setState<State extends MatcherState = MatcherState>(state: Partial<State>, expect: ExpectStatic): void;
762
805
 
763
806
  export { ASYMMETRIC_MATCHERS_OBJECT, Any, Anything, ArrayContaining, AsymmetricMatcher, GLOBAL_EXPECT, JEST_MATCHERS_OBJECT, JestAsymmetricMatchers, JestChaiExpect, JestExtend, MATCHERS_OBJECT, ObjectContaining, StringContaining, StringMatching, addCustomEqualityTesters, arrayBufferEquality, customMatchers, equals, fnNameFor, generateToBeMessage, getObjectKeys, getObjectSubset, getState, hasAsymmetric, hasProperty, isA, isAsymmetric, isImmutableUnorderedKeyed, isImmutableUnorderedSet, iterableEquality, pluralize, setState, sparseArrayEquality, subsetEquality, typeEquality };
764
- export type { Assertion, AsymmetricMatcherInterface, AsymmetricMatchersContaining, AsyncExpectationResult, ChaiPlugin, ExpectStatic, ExpectationResult, JestAssertion, MatcherHintOptions, MatcherState, MatchersObject, PromisifyAssertion, RawMatcherFn, SyncExpectationResult, Tester, TesterContext };
807
+ export type { Assertion, AsymmetricMatcherInterface, AsymmetricMatchersContaining, AsyncExpectationResult, ChaiPlugin, DeeplyAllowMatchers, ExpectStatic, ExpectationResult, JestAssertion, MatcherHintOptions, MatcherState, Matchers, MatchersObject, PromisifyAssertion, RawMatcherFn, SyncExpectationResult, Tester, TesterContext };