Skip to content

Commit

Permalink
fix(types): fix PartialMock with async TReturns (#3462)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghry5 committed May 29, 2023
1 parent cd5d58b commit b664d42
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/spy/src/index.ts
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
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

0 comments on commit b664d42

Please sign in to comment.