How to implement a dependency mocking library for JS

by Olegzandr Denman   Last Updated October 25, 2017 21:05 PM

I am curious about the various ways to implement a mocking library in JS, for example, the common use case of mocking network calls when testing. Some JS code is based on dependency-injection, such as Angular 1.x, but most JS code is not based on DI.

My question is - if you aren't using a full DI library, is the only way to mock dependencies in JS-land to monkey-patch the require() function?

If you use a library like Angular1.x, it uses DI and it can inject different versions of a dependency. But for most JS applications which don't use a complete DI framework, require is the primary way dependencies are sourced.

So I don't see any other way to mock dependencies, other than to monkey-patch the require function, when in "test mode". But I am wondering what options might exist, maybe I am overlooking something.



Related Questions




Injecting collections as constructor dependencies

Updated April 29, 2015 21:02 PM

Using Spring in Java Project

Updated February 29, 2016 01:02 AM