Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): fix PartialMock with async TReturns #3462

Merged
merged 1 commit into from
May 29, 2023
Merged
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
2 changes: 1 addition & 1 deletion packages/spy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export interface Mock<TArgs extends any[] = any, TReturns = any> extends SpyInst
new (...args: TArgs): TReturns
(...args: TArgs): TReturns
}
export interface PartialMock<TArgs extends any[] = any, TReturns = any> extends SpyInstance<TArgs, Partial<TReturns>> {
export interface PartialMock<TArgs extends any[] = any, TReturns = any> extends SpyInstance<TArgs, TReturns extends Promise<Awaited<TReturns>> ? Promise<Partial<Awaited<TReturns>>> : Partial<TReturns>> {
new (...args: TArgs): TReturns
(...args: TArgs): TReturns
}
Expand Down
16 changes: 16 additions & 0 deletions test/core/test/vi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ describe('testing vi utils', () => {
vi.mocked(mockFactory, { partial: true, deep: true }).mockReturnValue({
baz: 'baz',
})

type FooBarAsyncFactory = () => Promise<FooBar>

const mockFactoryAsync: FooBarAsyncFactory = vi.fn()

vi.mocked(mockFactoryAsync, { partial: true }).mockResolvedValue({
foo: vi.fn(),
})

vi.mocked(mockFactoryAsync, { partial: true, deep: false }).mockResolvedValue({
bar: vi.fn(),
})

vi.mocked(mockFactoryAsync, { partial: true, deep: true }).mockResolvedValue({
baz: 'baz',
})
})

test('can change config', () => {
Expand Down