Skip to content

Commit 02ec892

Browse files
authored
fix(expect): Ensure we can always self toEqual (#8094)
1 parent 247ef58 commit 02ec892

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

packages/expect/src/types.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,13 @@ export interface AsymmetricMatchersContaining extends CustomMatcher {
186186
closeTo: (expected: number, precision?: number) => any
187187
}
188188

189+
type WithAsymmetricMatcher<T> = T | AsymmetricMatcher<unknown>
190+
189191
export type DeeplyAllowMatchers<T> = T extends Array<infer Element>
190-
? (DeeplyAllowMatchers<Element> | Element)[]
192+
? WithAsymmetricMatcher<T> | DeeplyAllowMatchers<Element>[]
191193
: T extends object
192-
? {
193-
[K in keyof T]: DeeplyAllowMatchers<T[K]> | AsymmetricMatcher<unknown>
194-
}
195-
: T
194+
? WithAsymmetricMatcher<T> | { [K in keyof T]: DeeplyAllowMatchers<T[K]> }
195+
: WithAsymmetricMatcher<T>
196196

197197
export interface JestAssertion<T = any> extends jest.Matchers<void, T>, CustomMatcher {
198198
/**

test/core/test/expect.test-d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,12 @@ test('expect.* allows asymmetrict mattchers with different types', () => {
111111
// https://github.com/DefinitelyTyped/DefinitelyTyped/pull/62831/files#diff-ff7b882e4a29e7fe0e348a6bdf8b11774d606eaa221009b166b01389576d921fR1237
112112
expect({ list: [1, 2, 3] }).toMatchObject({ list: expect.any(Array) })
113113
expect({ list: [1, 2, 3] }).toMatchObject<{ list: number[] }>({ list: expect.any(Array) })
114+
115+
// expect<T>
116+
// https://github.com/vitest-dev/vitest/issues/8081
117+
function expectMany<T>(value: ({ enabled: false } | { enabled: true; data: T })) {
118+
expect(value).toEqual(value)
119+
expect(value).toMatchObject(value)
120+
}
121+
expectMany({ enabled: true, data: 'ok' })
114122
})

0 commit comments

Comments
 (0)