News for hackers. Stuff that matters.
Re:Why and how to use service injection in Node.js by williamdclt on Sunday October 31, 2021 @01:18PM (#29058546)
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

Re:Why and how to use service injection in Node.js by lhorie on Sunday October 31, 2021 @01:56PM (#29058865)
What 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.