logoalt Hacker News

halflifetoday at 10:48 AM3 repliesview on HN

Jest mocks allow developers to write bad code. Instead of separating concerns with DI, jest mocks overwrite the importing mechanism without any type safety. Also, if you wish to migrate to native node test runner, the mocks lock you into jest.

Use proper mocks with ts mockito and it will force you to write better code.


Replies

anon7000today at 4:24 PM

You can get some type safety with jest mocks with jest.mocked

Anyways I mostly agree, it’s very annoying to use mocks in jest even past the code smell. But I also find certain DI codebases to be highly annoying too. Exceptionally verbose for what it actually accomplishes at times.

howToTestFEtoday at 10:56 AM

100%. its a horrible code smell, and AI loves to jest.mock (or vi.mock()). (The most recent blog post on my site covers this as one of the things to look out for in AI generated tests...)

in JS (with Jest/Vitest) it is far too easy to call mock()...

show 1 reply