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.
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.
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()...
i view this as the strength of dynamic language like JS or Ruby rather than a weakness: look at all the busywork a language like Java would otherwise inflict on us that we’re able to avoid. it takes a single syntactic lint rule to enforce type safe use of these module mock APIs (enforce `testlib.mock<typeof import(P)>(P, …)`), and only about 100 lines of one time setup to add mock helpers in vitest to mock a specific export rather than the whole module. There are also lint rules that prevent pretty much all scoping problem misuse of hoisted apis like testlib.mock.
Your course should mention these techniques! Without them tests are unsound!
We do some dependency injection of our framework-level service objects like ServerApiService or RouterService or RecordCacheService (usually with a subclass of the actual implementation) but it hardly needs to be ubiquitous or typical for product developer code.