Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/spy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,8 @@ export function spyOn<T, M extends Classes<Required<T>> | Methods<Required<T>>>(
methodName: M
): Required<T>[M] extends { new (...args: infer A): infer R }
? MockInstance<(this: R, ...args: A) => R>
: T[M] extends Procedure
? MockInstance<T[M]>
: Required<T>[M] extends Procedure
? MockInstance<Required<T>[M]>
: never
export function spyOn<T, K extends keyof T>(
obj: T,
Expand Down
12 changes: 12 additions & 0 deletions test/core/test/jest-mock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,4 +608,16 @@ describe('jest mock compat layer', () => {
expect(spy.mock.calls).toEqual([])
})
})

it.fails('optional method type', () => {
interface Person {
greet: (name: string) => string
}
const person: Partial<Person> = {}
const spy = vi.spyOn(person, 'greet')
expect(spy).toBe(person.greet)
expect(person.greet?.('Alice')).toMatchInlineSnapshot()
spy.mockImplementation(() => 'mocked')
expect(person.greet?.('Bob')).toMatchInlineSnapshot()
})
})