modulejs icon indicating copy to clipboard operation
modulejs copied to clipboard

Possibility to "re-define" the module?

Open VilemP opened this issue 9 years ago • 0 comments

Hi Lars!

Thanks for modulejs! I am a happy user. Recently though, I have come accross a problem in my test environment. I have a module, which is a "shared state" between other modules. (Ugly, I know... It is my implementation of something like "event bus" and so far I did not come with a good idea how to publish events on bus and listen to them without sharing the bus object)

Well, it turns out I would like to inject a mock of the bus so that I have more control over what is happening, most specifically I want to make a bus, which is normally asynchronous, synchronous.

I know I can provide a different implementation of the dependency in third argument of .require method, but that does only override it in the module under the test. If that module happens to use another module, which in turns uses the module I want to override, it won't be overriden and I end up with two different instances of what should be one.

Maybe it is better to describe it using a little diagram:

A  --> B  (module A depends on B)
A --> C

B --> C

If I now use

modulejs.require('A', {
 'C' : D
});

I will end up with:

A(B,D)
B(C)

while I want

A(B,D)
B(D)

I could of course require also module B this way:

modulejs.require ('B', {
  'C' : D
});

But that makes it overly complicated as I am exposing dependencies of dependencies of the module under the test. That seems to be very fragile.

So, I was wondering whether you though of any way, how to (temporarily) be able to override a module definition that would spread across ALL modules? Or, is there a better way to overcome that?

VilemP avatar Dec 07 '16 18:12 VilemP