Let's say I have a RequireJS module and there is only one instance in my application (let's say it does something asynchronous and has callbacks):
and I want a unit test instance of this module, I found that I am creating the module as follows:
This gives me something that is not attached to the state, which I can then tap out of my unit tests, so in my unit tests I never refer to modules/myModuleInstance , only the modules/myModule that I create for each test every time. The application then references modules/myModuleInstance .
It looks like an anti-pattern. I hate having the extra part of a βmodule instanceβ. I know about setup and deletion methods in unit tests and I can support this instance this way, but seeing what can happen with C # when trying to unit test massive singlets, messing with the state between unit tests is what I really I want to avoid, especially with a dynamic language.
What do people usually do in this case?
source share