Skip to content

Commit 8592cdb

Browse files
committed
Adjust when type to support ignoreExtraArgs
1 parent 2029a3b commit 8592cdb

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

src/behaviors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import type {
99
WithMatchers,
1010
} from './types.ts'
1111

12-
export interface WhenOptions {
13-
ignoreExtraArgs?: boolean
12+
export interface WhenOptions<TIgnore extends boolean = boolean> {
13+
ignoreExtraArgs?: TIgnore
1414
times?: number
1515
}
1616

src/vitest-when.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ export interface StubWrapper<TFunc extends AnyMockable> {
2222
): Stub<TFunc>
2323
}
2424

25+
export interface StubWrapperFlexible<TFunc extends AnyMockable> {
26+
calledWith<TArgs extends ParametersOf<TFunc>>(
27+
...args: Partial<WithMatchers<TArgs>>
28+
): Stub<TFunc>
29+
}
30+
2531
export interface Stub<TFunc extends AnyMockable> {
2632
thenReturn: (...values: ReturnTypeOf<TFunc>[]) => Mock<TFunc>
2733
thenResolve: (...values: Awaited<ReturnTypeOf<TFunc>>[]) => Mock<TFunc>
@@ -30,10 +36,18 @@ export interface Stub<TFunc extends AnyMockable> {
3036
thenDo: (...callbacks: AsFunction<TFunc>[]) => Mock<TFunc>
3137
}
3238

33-
export const when = <TFunc extends AnyMockable>(
39+
export function when<TFunc extends AnyMockable>(
40+
mock: TFunc | MockInstance<TFunc>,
41+
options: WhenOptions<true>,
42+
): StubWrapperFlexible<NormalizeMockable<TFunc>>
43+
export function when<TFunc extends AnyMockable>(
44+
mock: TFunc | MockInstance<TFunc>,
45+
options?: WhenOptions<false>,
46+
): StubWrapper<NormalizeMockable<TFunc>>
47+
export function when<TFunc extends AnyMockable>(
3448
mock: TFunc | MockInstance<TFunc>,
3549
options: WhenOptions = {},
36-
): StubWrapper<NormalizeMockable<TFunc>> => {
50+
): StubWrapper<NormalizeMockable<TFunc>> {
3751
const validatedMock = validateMock(mock)
3852
const behaviorStack = configureMock(validatedMock)
3953
const result = asMock(validatedMock)

test/fixtures.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ export function overloaded(input?: number): string | boolean {
3131
throw new Error(`overloaded(${input})`)
3232
}
3333

34+
export function extraArguments(first: number, second: number): string {
35+
throw new Error(`extraArguments(${first}, ${second})`)
36+
}
37+
3438
export class SimpleClass {
3539
constructor(input: number) {
3640
throw new Error(`SimpleClass(${input})`)

test/typing.test-d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import * as subject from '../src/vitest-when.ts'
1717
import {
1818
complex,
19+
extraArguments,
1920
generic,
2021
overloaded,
2122
simple,
@@ -45,6 +46,17 @@ describe('vitest-when type signatures', () => {
4546
>()
4647
})
4748

49+
it('should handle fewer than required arguments', () => {
50+
const result = subject
51+
.when(extraArguments, { ignoreExtraArgs: true })
52+
.calledWith(1)
53+
.thenReturn('hello')
54+
55+
expectTypeOf(result).toEqualTypeOf<
56+
MockedFunction<(input: number, second: number) => string>
57+
>()
58+
})
59+
4860
it('returns mock type for then resolve', () => {
4961
const result = subject.when(simpleAsync).calledWith(1).thenResolve('hello')
5062

0 commit comments

Comments
 (0)