Skip to content

Commit

Permalink
fix(ts-jest): fixed nullable mocks issue (#759)
Browse files Browse the repository at this point in the history
closes #757

Co-authored-by: Christian Jeschke <[email protected]>
  • Loading branch information
VonRehberg and Christian Jeschke authored Aug 12, 2024
1 parent bf0f009 commit b181b8a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 16 additions & 0 deletions packages/testing/ts-jest/src/mocks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Test>();
mock.foo.mockImplementation(() => {
return 0;
});
expect(mock.foo()).toEqual(0);
mock.foo.mockImplementation(() => {
return undefined;
});
expect(mock.foo()).toEqual(undefined);
});
});

describe('auto mocked', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/testing/ts-jest/src/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down

0 comments on commit b181b8a

Please sign in to comment.