From b181b8aaf3d91302e4fcaaea6f5979499d50ee54 Mon Sep 17 00:00:00 2001 From: VonRehberg Date: Mon, 12 Aug 2024 15:57:40 +0200 Subject: [PATCH] fix(ts-jest): fixed nullable mocks issue (#759) closes #757 Co-authored-by: Christian Jeschke --- packages/testing/ts-jest/src/mocks.spec.ts | 16 ++++++++++++++++ packages/testing/ts-jest/src/mocks.ts | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/testing/ts-jest/src/mocks.spec.ts b/packages/testing/ts-jest/src/mocks.spec.ts index 682bade0c..3759a315a 100644 --- a/packages/testing/ts-jest/src/mocks.spec.ts +++ b/packages/testing/ts-jest/src/mocks.spec.ts @@ -157,6 +157,22 @@ describe('Mocks', () => { expect(test.base).toEqual(base); }); + + it('should accept mocks returning nullables', async () => { + interface Test { + foo(): number | undefined; + } + + const mock = createMock(); + mock.foo.mockImplementation(() => { + return 0; + }); + expect(mock.foo()).toEqual(0); + mock.foo.mockImplementation(() => { + return undefined; + }); + expect(mock.foo()).toEqual(undefined); + }); }); describe('auto mocked', () => { diff --git a/packages/testing/ts-jest/src/mocks.ts b/packages/testing/ts-jest/src/mocks.ts index 36f1001b7..99c1d22c2 100644 --- a/packages/testing/ts-jest/src/mocks.ts +++ b/packages/testing/ts-jest/src/mocks.ts @@ -30,7 +30,7 @@ const createRecursiveMockProxy = (name: string) => { return new Proxy(t, { apply: (target, thisArg, argsArray) => { const result = Reflect.apply(target, thisArg, argsArray); - if (result) { + if (target.getMockImplementation() || result) { return result; } else { if (!cache.has('__apply')) {