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
10 changes: 8 additions & 2 deletions packages/spy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,17 @@ export function createMockInstance(options: MockInstanceOption = {}): Mock<Proce
}

mock.mockReturnValue = function mockReturnValue(value) {
return mock.mockImplementation(() => value)
// eslint-disable-next-line prefer-arrow-callback
return mock.mockImplementation(function () {
return value
})
}

mock.mockReturnValueOnce = function mockReturnValueOnce(value) {
return mock.mockImplementationOnce(() => value)
// eslint-disable-next-line prefer-arrow-callback
return mock.mockImplementationOnce(function () {
return value
})
}

mock.mockResolvedValue = function mockResolvedValue(value) {
Expand Down
2 changes: 1 addition & 1 deletion packages/spy/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export type MockParameters<T extends Procedure | Constructable> = T extends Cons
? Parameters<T> : never

export type MockReturnType<T extends Procedure | Constructable> = T extends Constructable
? void
? InstanceType<T>
: T extends Procedure
? ReturnType<T> : never

Expand Down
4 changes: 2 additions & 2 deletions test/core/test/mocking/vi-fn.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ test('spy.mock when implementation is a class', () => {
const Mock = vi.fn(Klass)

expectTypeOf(Mock.mock.calls).toEqualTypeOf<[a: string, b?: number][]>()
expectTypeOf(Mock.mock.results).toEqualTypeOf<MockResult<void>[]>()
expectTypeOf(Mock.mock.results).toEqualTypeOf<MockResult<Klass>[]>()
expectTypeOf(Mock.mock.contexts).toEqualTypeOf<Klass[]>()
expectTypeOf(Mock.mock.instances).toEqualTypeOf<Klass[]>()
expectTypeOf(Mock.mock.invocationCallOrder).toEqualTypeOf<number[]>()
expectTypeOf(Mock.mock.settledResults).toEqualTypeOf<MockSettledResult<void>[]>()
expectTypeOf(Mock.mock.settledResults).toEqualTypeOf<MockSettledResult<Klass>[]>()
expectTypeOf(Mock.mock.lastCall).toEqualTypeOf<[a: string, b?: number] | undefined>()

// static properties are defined
Expand Down
7 changes: 7 additions & 0 deletions test/core/test/spy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ describe('spyOn', () => {

expect(hw.hello()).toEqual('hello world')
})

test('mock return value for constructor', () => {
vi.spyOn(mock, 'HelloWorld').mockReturnValueOnce({ hello: () => 'hello world' })

const mockedHelloWorld = new mock.HelloWorld()
expect(mockedHelloWorld.hello()).toEqual('hello world')
})
})
Loading