albertogasparin / react-magnetic-di

Dependency injection and replacement for Javascript and React components/hooks
MIT License
131 stars 7 forks source link

Allow module mocking via jest.mock #96

Open albertogasparin opened 1 week ago

albertogasparin commented 1 week ago

New babel options:

// Mock injectables imports to improve test performance
// Currently supports only jest
mockModules: 'jest',
// Automatically mock injectables imports (needs mockModules set)
// For instance mock all injectables imports that are 1st party @app/foo
defaultMockedModules: ['@app/'],

The former, together with a new injectable option called module, allows the module of the imported variable to be mocked by jest (via jest.mock).

const modalDi = injectable(Modal, () => null, { module: true });

The latter, is an experimental auto-mock behaviour, where we can automatically add jest.mock calls for all packages with an injectable defined. Should be useful to test the impact at scale (but lots of tests might fail)

theKashey commented 6 days ago

So nothing will happen out of the box. Only if you specify {module: true} Then it will be auto-injection of jest.mock aiding speed concerns 👍

No magic, everything debuggable and explicit 🚀