From 7ace2db4dc2cf0f5b99e14089f1b76857295ccd5 Mon Sep 17 00:00:00 2001 From: Matthew Pietz Date: Fri, 24 Oct 2025 15:22:03 -0700 Subject: [PATCH 1/3] Use `function` signature to support Vitest v4 --- src/stubs.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/stubs.ts b/src/stubs.ts index 17c1a59..b913893 100644 --- a/src/stubs.ts +++ b/src/stubs.ts @@ -33,7 +33,7 @@ export const configureMock = ( const behaviorStack = createBehaviorStack() const fallbackImplementation = getFallbackImplementation(mock) - const implementation = (...args: ParametersOf) => { + function implementation(...args: ParametersOf) { const behavior = behaviorStack.use(args)?.behavior ?? { type: BehaviorType.DO, callback: fallbackImplementation, @@ -59,7 +59,7 @@ export const configureMock = ( case BehaviorType.DO: { // eslint-disable-next-line @typescript-eslint/no-unsafe-return - return behavior.callback?.(...args) + return behavior.callback?.call(this, ...args) } } } From f92e4c265ba93704c2ead6c608b4e2046b8326ff Mon Sep 17 00:00:00 2001 From: Matthew Pietz Date: Sat, 25 Oct 2025 10:44:06 -0700 Subject: [PATCH 2/3] Add test for `.thenDo()` --- src/stubs.ts | 2 +- test/vitest-when.test.ts | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/stubs.ts b/src/stubs.ts index b913893..da092a9 100644 --- a/src/stubs.ts +++ b/src/stubs.ts @@ -33,7 +33,7 @@ export const configureMock = ( const behaviorStack = createBehaviorStack() const fallbackImplementation = getFallbackImplementation(mock) - function implementation(...args: ParametersOf) { + function implementation(this: ThisType, ...args: ParametersOf) { const behavior = behaviorStack.use(args)?.behavior ?? { type: BehaviorType.DO, callback: fallbackImplementation, diff --git a/test/vitest-when.test.ts b/test/vitest-when.test.ts index 063df06..786378c 100644 --- a/test/vitest-when.test.ts +++ b/test/vitest-when.test.ts @@ -129,6 +129,19 @@ describe('vitest-when', () => { expect(callback).toHaveBeenCalledTimes(1) }) + it('should pass this to the callback', () => { + const ok = Symbol('ok') + const Spy = subject + .when(vi.fn()) + .calledWith() + .thenDo(function (this: unknown) { + expect(this).toBeInstanceOf(Spy) + return { ok } + }) + + expect(new Spy()).toEqual({ ok }) + }) + it('should return multiple values', () => { const spy = subject.when(vi.fn()).calledWith(1, 2, 3).thenReturn(4, 5, 6) From 639c131e1a82fa947ba598e84ea567a41718ddb3 Mon Sep 17 00:00:00 2001 From: Michael Cousins Date: Sun, 26 Oct 2025 23:25:56 -0400 Subject: [PATCH 3/3] fixup: make tests more focused on the end result --- test/vitest-when.test.ts | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/test/vitest-when.test.ts b/test/vitest-when.test.ts index 786378c..c04758d 100644 --- a/test/vitest-when.test.ts +++ b/test/vitest-when.test.ts @@ -1,6 +1,7 @@ import { describe, expect, it, vi } from 'vitest' import * as subject from '../src/vitest-when.ts' +import { SimpleClass } from './fixtures.ts' declare module 'vitest' { interface AsymmetricMatchersContaining { @@ -129,17 +130,24 @@ describe('vitest-when', () => { expect(callback).toHaveBeenCalledTimes(1) }) - it('should pass this to the callback', () => { - const ok = Symbol('ok') + it('should mock a constructor via thenDo', () => { const Spy = subject - .when(vi.fn()) - .calledWith() - .thenDo(function (this: unknown) { - expect(this).toBeInstanceOf(Spy) - return { ok } + .when(vi.fn()) + .calledWith(42) + .thenDo(function (this: SimpleClass) { + this.simpleMethod = () => 'hello' }) - expect(new Spy()).toEqual({ ok }) + expect(new Spy(42).simpleMethod()).toBe('hello') + }) + + it('should mock a constructor via thenReturn', () => { + const Spy = subject + .when(vi.fn()) + .calledWith(42) + .thenReturn({ simpleMethod: () => 'hello' }) + + expect(new Spy(42).simpleMethod()).toBe('hello') }) it('should return multiple values', () => {