joke icon indicating copy to clipboard operation
joke copied to clipboard

Housekeeping - ensure that mocks were actually called?

Open wh1t3cAt1k opened this issue 2 years ago • 2 comments

As the project changes, a lot of mocks can remain that are not even called in the scope of the test suite.

I wonder if in this project, we could somehow automatically detect whether a given mock was called at least once; if not, throw an error - this would help people keep their test suites lean and clean.

wh1t3cAt1k avatar May 31 '23 11:05 wh1t3cAt1k

I wonder if in this project, we could somehow automatically detect whether a given mock was called at least once;

But sometimes mocks exist to assert that they are not called like:

test('foobar should not call callback', () => {
  foobar(myMock);
  expect(myMock).not.toBeCalled();
});

Perhaps a better way to ensure that unused mocks don't go unnoticed is to use a linter that reports unused variables.

anilanar avatar Jun 01 '23 08:06 anilanar

@anilanar you have a point, but linter can only do so much... Imagine a convoluted mocking machinery where mocks get set up (so no "unused variables") but are never actually called. People maintain these set-ups without realising it's pointless work!

wh1t3cAt1k avatar Jun 15 '23 14:06 wh1t3cAt1k