-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add function that verifies that all when mocks are called #10
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
import { assert } from 'vitest' | ||
import { configureStub } from './stubs.ts' | ||
import type { WhenOptions } from './behaviors.ts' | ||
import type { BehaviorStack, WhenOptions } from './behaviors.ts' | ||
import type { AnyFunction } from './types.ts' | ||
|
||
export type { WhenOptions } from './behaviors.ts' | ||
export * from './errors.ts' | ||
|
||
export const behaviorStackRegistry = new Set<BehaviorStack<AnyFunction>>() | ||
|
||
export interface StubWrapper<TFunc extends AnyFunction> { | ||
calledWith<TArgs extends Parameters<TFunc>>( | ||
...args: TArgs | ||
|
@@ -25,6 +28,8 @@ export const when = <TFunc extends AnyFunction>( | |
): StubWrapper<TFunc> => { | ||
const behaviorStack = configureStub(spy) | ||
|
||
behaviorStackRegistry.add(behaviorStack) | ||
|
||
return { | ||
calledWith: (...args) => { | ||
const behaviors = behaviorStack.bindArgs(args, options) | ||
|
@@ -39,3 +44,10 @@ export const when = <TFunc extends AnyFunction>( | |
}, | ||
} | ||
} | ||
|
||
export const verifyAllWhenMocksCalled = () => { | ||
const uncalledMocks = [...behaviorStackRegistry].flatMap((behaviorStack) => { | ||
return behaviorStack.get().filter(behavior => behavior.times && behavior.times > 0) | ||
}) | ||
assert.equal(uncalledMocks.length,0, `Failed verifyAllWhenMocksCalled: ${uncalledMocks.length} mock(s) not called:`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this assertion message is very helpful, and I think that shows a technical limitation to trying to include this kind of functionality inside of this library. I also think it miscommunicates to the user a little; there is a 1 to many relationship between |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This introduces global state into the library, which requires cleanup. I don't think I want that added complexity in this library. I think it would be confusing to require both
vi.resetAllMocks
and some otherresetAllWhenMocks