Skip to content

Commit f92e4c2

Browse files
committed
Add test for .thenDo()
1 parent 7ace2db commit f92e4c2

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/stubs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const configureMock = <TMock extends Mock>(
3333
const behaviorStack = createBehaviorStack<TMock>()
3434
const fallbackImplementation = getFallbackImplementation(mock)
3535

36-
function implementation(...args: ParametersOf<TMock>) {
36+
function implementation(this: ThisType<TMock>, ...args: ParametersOf<TMock>) {
3737
const behavior = behaviorStack.use(args)?.behavior ?? {
3838
type: BehaviorType.DO,
3939
callback: fallbackImplementation,

test/vitest-when.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,19 @@ describe('vitest-when', () => {
129129
expect(callback).toHaveBeenCalledTimes(1)
130130
})
131131

132+
it('should pass this to the callback', () => {
133+
const ok = Symbol('ok')
134+
const Spy = subject
135+
.when(vi.fn())
136+
.calledWith()
137+
.thenDo(function (this: unknown) {
138+
expect(this).toBeInstanceOf(Spy)
139+
return { ok }
140+
})
141+
142+
expect(new Spy()).toEqual({ ok })
143+
})
144+
132145
it('should return multiple values', () => {
133146
const spy = subject.when(vi.fn()).calledWith(1, 2, 3).thenReturn(4, 5, 6)
134147

0 commit comments

Comments
 (0)