Skip to content

Commit

Permalink
feat: replace any with unknown for default types
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Apr 23, 2024
1 parent 2363d57 commit 7b59e10
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
10 changes: 5 additions & 5 deletions packages/spy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ export interface MockContext<T extends Procedure> {
lastCall: Parameters<T> | undefined
}

// TODO: use `(...args: unknown[]) => unknown` for stricter default like jest?
type Procedure = (...args: any[]) => any
type UnknownProcedure = (...args: unknown[]) => unknown

type Methods<T> = keyof {
[K in keyof T as T[K] extends Procedure ? K : never]: T[K];
Expand All @@ -117,7 +117,7 @@ Jest uses the latter for `MockInstance.mockImplementation` etc... and it allows
const boolFn: Jest.Mock<() => boolean> = jest.fn<() => true>(() => true)
*/
/* eslint-disable ts/method-signature-style */
export interface MockInstance<T extends Procedure = Procedure> {
export interface MockInstance<T extends Procedure = UnknownProcedure> {
/**
* Use it to return the name given to mock with method `.mockName(name)`.
*/
Expand Down Expand Up @@ -249,14 +249,14 @@ export interface MockInstance<T extends Procedure = Procedure> {
}
/* eslint-enable ts/method-signature-style */

export interface Mock<T extends Procedure = Procedure> extends MockInstance<T> {
export interface Mock<T extends Procedure = UnknownProcedure> extends MockInstance<T> {
new (...args: Parameters<T>): ReturnType<T>
(...args: Parameters<T>): ReturnType<T>
}

type PartialMaybePromise<T> = T extends Promise<Awaited<T>> ? Promise<Partial<Awaited<T>>> : Partial<T>

export interface PartialMock<T extends Procedure = Procedure> extends MockInstance<(...args: Parameters<T>) => PartialMaybePromise<ReturnType<T>>> {
export interface PartialMock<T extends Procedure = UnknownProcedure> extends MockInstance<(...args: Parameters<T>) => PartialMaybePromise<ReturnType<T>>> {
new (...args: Parameters<T>): ReturnType<T>
(...args: Parameters<T>): ReturnType<T>
}
Expand Down Expand Up @@ -514,7 +514,7 @@ function enhanceSpy<T extends Procedure>(
return stub as any
}

export function fn<T extends Procedure = Procedure>(
export function fn<T extends Procedure = UnknownProcedure>(
implementation?: T,
): Mock<T> {
const enhancedSpy = enhanceSpy(tinyspy.internalSpyOn({ spy: implementation || (() => {}) }, 'spy'))
Expand Down
2 changes: 1 addition & 1 deletion test/core/test/spy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('spyOn', () => {

test('infers a class correctly', () => {
vi.spyOn(mock, 'HelloWorld').mockImplementationOnce(() => {
const Mock = vi.fn()
const Mock = vi.fn<any>()
Mock.prototype.hello = vi.fn(() => 'hello world')
return new Mock()
})
Expand Down
13 changes: 11 additions & 2 deletions test/core/test/vi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ describe('testing vi utils', () => {
})
expectType<MockedFunction<() => boolean>>(vi.fn(() => true))
expectType<MockedFunction<() => boolean>>(vi.fn())

expectType<MockedFunction<() => boolean>>(vi.fn<() => boolean>(() => true))
expectType<Mock<() => boolean>>(vi.fn<() => boolean>(() => true))
expectType<() => boolean>(vi.fn(() => true))

// @ts-expect-error default unkonwn
expectType<(v: number) => boolean>(vi.fn())

expectType<(v: number) => boolean>(vi.fn<any>())
})

test('vi partial mocked', () => {
Expand All @@ -57,7 +66,7 @@ describe('testing vi utils', () => {
})

vi.mocked(mockFactory, { partial: true, deep: false }).mockReturnValue({
bar: vi.fn(),
bar: vi.fn<FooBar['bar']>(),
})

vi.mocked(mockFactory, { partial: true, deep: true }).mockReturnValue({
Expand All @@ -71,7 +80,7 @@ describe('testing vi utils', () => {
})

vi.mocked(mockFactoryAsync, { partial: true, deep: false }).mockResolvedValue({
bar: vi.fn(),
bar: vi.fn<FooBar['bar']>(),
})

vi.mocked(mockFactoryAsync, { partial: true, deep: true }).mockResolvedValue({
Expand Down

0 comments on commit 7b59e10

Please sign in to comment.