marchaos / jest-mock-extended

Type safe mocking extensions for Jest https://www.npmjs.com/package/jest-mock-extended
MIT License
828 stars 57 forks source link

calledWith return value redefinition dont work #53

Closed ayartsev7 closed 3 years ago

ayartsev7 commented 3 years ago

Hi!

I use jest-mock-extended v1.0.10 + jest v24.9.0 Failing to change return value of calledWith for the same argument. Here is a minimal example:

import { mockDeep } from 'jest-mock-extended';

interface A {
  f: (s: string) => string;
}

const m = mockDeep<A>();
const value = 'value';
m.f.calledWith(value).mockReturnValue('result 1');
m.f.calledWith(value).mockReturnValue('result 2');

test('', () => {
  expect(m.f(value)).toBe('result 2');
});

Test fails, m.f(value) returns 'result 1'. Is this a bug?

marchaos commented 3 years ago

Can you not use

m.f.calledWith(value).mockReturnValueOnce('result 1').mockReturnValueOnce('result 2');

?

ayartsev7 commented 3 years ago

I've just provided the minimal test. In my real test suite I typically setup calledWith(value).mockReturnValue in beforeEach with some custom value for all tests and I want to override this returned value for a single test.

With jest I can do this, the following test passes

const f = jest.fn();
f.mockReturnValue('result 1');
f.mockReturnValue('result 2');

test('', () => {
  expect(f()).toBe('result 2');
});

so I expected jest-mock-extended to behave in the same way.