Skip to content
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
12 changes: 12 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,17 @@ export default defineConfig(
'unicorn/no-array-reverse': 'off',
},
},
{
files: ['src/types.ts'],
rules: {
'@typescript-eslint/no-explicit-any': 'off',
},
},
{
files: ['test/typing.test-d.ts'],
rules: {
'@typescript-eslint/no-unsafe-argument': 'off',
},
},
globalIgnores(['**/coverage/**', '**/dist/**']),
)
7 changes: 0 additions & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/** Common type definitions. */
import type { AsymmetricMatcher } from '@vitest/expect'
import type { MockedClass, MockedFunction } from 'vitest'

/** Any function. */
Expand Down Expand Up @@ -81,11 +79,6 @@ export type ConstructorImplementation<
| (new (...args: ConstructorParameters<TFunc>) => InstanceType<TFunc>)
| ((this: InstanceType<TFunc>, ...args: ConstructorParameters<TFunc>) => void)

/** Accept a value or an AsymmetricMatcher in an arguments array */
export type WithMatchers<T extends unknown[]> = {
[K in keyof T]: AsymmetricMatcher<unknown> | T[K]
}

/** A mocked function or constructor. */
export type Mock<TFunc extends AnyMockable> = TFunc extends AnyConstructor
? MockedClass<TFunc>
Expand Down
5 changes: 1 addition & 4 deletions src/vitest-when.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type {
NormalizeMockable,
ParametersOf,
ReturnTypeOf,
WithMatchers,
} from './types.ts'

export { type Behavior, BehaviorType, type WhenOptions } from './behaviors.ts'
Expand All @@ -21,9 +20,7 @@ export interface StubWrapper<
TFunc extends AnyMockable,
TOptions extends WhenOptions | undefined,
> {
calledWith<TArgs extends ArgumentsSpec<ParametersOf<TFunc>, TOptions>>(
...args: WithMatchers<TArgs>
): Stub<TFunc>
calledWith(...args: ArgumentsSpec<ParametersOf<TFunc>, TOptions>): Stub<TFunc>
}

export interface Stub<TFunc extends AnyMockable> {
Expand Down
8 changes: 8 additions & 0 deletions test/typing.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ describe('vitest-when type signatures', () => {
subject
.when(multipleArgs, { ignoreExtraArgs: true })
.calledWith(expect.any(Number), expect.any(String), expect.any(Boolean))

subject.when(multipleArgs, { ignoreExtraArgs: true }).calledWith(
// @ts-expect-error: too many arguments
expect.anything(),
expect.anything(),
expect.anything(),
expect.anything(),
)
})

it('returns mock type for then resolve', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/vitest-when.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ describe('vitest-when', () => {
})

it.each([
{ stubArgs: [] as unknown[], callArgs: [] as unknown[] },
{ stubArgs: [], callArgs: [] },
{ stubArgs: [], callArgs: ['a'] },
{ stubArgs: [], callArgs: ['a', 'b'] },
{ stubArgs: ['a'], callArgs: ['a'] },
Expand All @@ -300,7 +300,7 @@ describe('vitest-when', () => {
)

it.each([
{ stubArgs: ['a'] as unknown[], callArgs: ['b'] as unknown[] },
{ stubArgs: ['a'], callArgs: ['b'] },
{ stubArgs: [undefined], callArgs: [] },
])(
'does not match call $callArgs against stub $stubArgs with ignoreExtraArgs',
Expand Down