3 ms·
You wouldn't inject Moment though, you'd inject an instance of a class (`DateManager` or whatever). You can absolutely write a mock for it that implement the sa
by williamdclt 5y ago
You wouldn't inject Moment though, you'd inject an instance of a class (`DateManager` or whatever). You can absolutely write a mock for it that implement the same interface to enforce the same contract (`class DateManagerMock implements DateManager`), and use Jest mocking.
Is it simpler/cleaner than DI? Arguable
- lhorie 5y agoWhat you inject is up to you. In Angular.js you could inject setTimeout's interface. I've injected fetch (for automatic anti-CSRF header inclusion). Etc. Facades are good if you're not sure if the API/semantics are set in stone. For moment.js, one typically only cares to mock specific calls related to things like current time, so it often makes more sense to refactor towards turning currentness into an explicit input, either by mocking the hard-coded moment.js call inside a convoluted unit, or more ideally, by making the value an input argument by design in the first place. The latter is harder to get to if you already have the code written, so mocking moment.js calls could be an acceptable alternative. You don't need to bring the whole OOP factory manager baggage if you just want simple contracts.