jest-typed-mock
Make unit tests great again!
jest-typed-mock brings type checking to the wild __mocks__
of Jest. Helping you maintain the correct mocks, matching the real files, they are going to mock.
This is like eslint, but for mocks.
Manual mocks are defined by writing a module in a __mocks__/ subdirectory immediately adjacent to the module. For example, to mock a module called user in the models directory, create a file called user.js and put it in the models/__mocks__ directory.
https://facebook.github.io/jest/docs/en/manual-mocks.html#content
Usage
Add to your package.json
"scripts":
In some cases (always?) you have to specify babel env.
Type safety
Rewiremock can provide a type safety to dependency mocking, but Jest do had it's own mocking (and sandboxing) system. As long rewiremock still able to work, even under the Jest management - it is now a quite good idea.
jest-typed-mock is not a library. It is just a small tool, which can check mocked
file against the real one.
Lets imagine - we have a.js
//a.js;;
and __mocks__/a.js
;;
It is ok. But next you change the real a.js
//a.js// function 1 is changed;// function 2 is removed
But your tests are still using the old __mocks__
, and they are still green.
jest-typed-mock just matches real files and mocks.
TS:
Property 'function2' is missing in type 'typeof "....a"'.
Flow:
property `function2` of exports of "/__mocks__/a.js". Property not found in const real = () => import('/a.js');
JS
As long there is no types
, could only test exported names as their types(object, number, function), and function arguments count.
Even this is quite helpful.
jest-typed-mock: mocked export "function2" does not exists in a.js
jest-typed-mock will also try to check function agains function, at least argument count, which can break a lot of things.
To bypass this checking use
jest-typed-mock exports
, or specify noFunctionCompare in API options
__dirname noFunctionCompare: true
Result
As result - you reduce a smell of mocking. Mocks becomes less fake, and tests becomes less flake.
Licence
MIT