I am looking to get some input from some more experienced testers than what I am. :)
I am trying to make my node modules testable, allowing for dependency spying/stubbing/mocking without the need to use a magic library such as rewire or mockery to intercept my module import/require statements.
I was thinking of using an approach whereby I would create a factory function within each module. The factory function accepts the required dependencies for the module to export. The default export would call the factory function using the standard import statements from the module. I would also export the factory function itself allowing for my tests to inject whatever dependencies they like.
Here is an example (which is completely contrived and simplified to highlight the design approach):
import { foo } from './utils/foo'; // We export our factory, exposing it to consumers. export const factory = (dependencies = {}) => { const { $foo = foo // defaults to standard imp if none provided. } = dependencies; return function bar() { return $foo(); } } // The default implementation, which would end up using default deps. export default factory(); Thoughts? Is this too verbose? Is there a better mechanism of achieving the same? I am slightly concerned about the boiler plate additions that I will be adding to my project, but perhaps the trade off is worth it.