You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
$ npm run test
new instance of brake
new instance of car
new instance of car
new instance of car
As you can see the car is no longer being cached and therefore creates an object every time require is called.
In my personal test I need to have only one car object because another js require it, but when I change the behavior with td.replace(car, 'stop') nothing happen. The other js call require('./car') and then a new object is created.
The text was updated successfully, but these errors were encountered:
Yes, this is by design. (Read quibble's source for more details). There's no sensible other way to fake require without bypassing the module cache. If we allowed tainted subjects (and because they have been handed fake things, they are tainted) into the module cache, we'd be asking for trouble in the event that anything else required them asynchronously or later on, or if multiple fake configurations were made via different calls to td.replace.
After you call td.reset(), the cache will be restored such that if a real car is already cached, that singleton will continue to be returned and it it's not already cached, the real one will be loaded and then cached as it would normally.
I have the following code
brake.js
car.js
test.js
And the result is
$ npm run test new instance of brake new instance of car new instance of car new instance of car
As you can see the
car
is no longer being cached and therefore creates an object every timerequire
is called.In my personal test I need to have only one
car
object because another js require it, but when I change the behavior withtd.replace(car, 'stop')
nothing happen. The other js callrequire('./car')
and then a new object is created.The text was updated successfully, but these errors were encountered: