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

mockClear() is not removing calledWith() from mocks #52

Closed tobiasvoelke closed 3 years ago

tobiasvoelke commented 3 years ago

Hi!

If I use mockClear() in combination with calledWith() it is still using the previous mocks:

import { mock, mockClear } from 'jest-mock-extended';

class Implementation {
  public doSomething(arg: string): string {
    return arg;
  }
}

describe('mockClear should clear calledWith', () => {
  let mockedClass = mock<Implementation>();
  afterEach(() => {
    mockClear(mockedClass);
  });

  it('should do sth', () => {
    mockedClass.doSomething.calledWith('hello').mockReturnValue('firstMock');
    expect(mockedClass.doSomething('hello')).toEqual('firstMock');
  });

  it('should do sth2', () => {
    mockedClass.doSomething.calledWith('hello').mockReturnValue('secondMock');
    expect(mockedClass.doSomething('hello')).toEqual('secondMock');
    // fails with:
    // Error: expect(received).toEqual(expected) // deep equality

    // Expected: "secondMock"
    // Received: "firstMock"
  });
});

Workaround for now is to re-create the mock instead of calling mockClear()

tobiasvoelke commented 3 years ago

Just realised I mixed up mockReset() and mockClear()