cache-require-paths icon indicating copy to clipboard operation
cache-require-paths copied to clipboard

Does not play nice with mockery

Open brian-mann opened this issue 10 years ago • 2 comments

In my test code I'm registering a substitute module for electron.

When I enable cache-require-paths it will never use the substituted module, and instead error:

Error: Cannot find module 'electron'

require("cache-require-paths")

var mockery = require("mockery")
mockery.enable()
mockery.registerSubstitute("electron", "./electron_stub")

require("electron") // <-- bomb

This will work if I remove cache-require-paths.

brian-mann avatar Feb 24 '16 15:02 brian-mann

seems because the mockery changes the loading paths, it really affects the cache paths. Two questions

  • can you move loading cache-require-paths line AFTER registerSubstitute?
  • can you make a tiny unit test with local modules to show the problem, or is this only when using electron?

bahmutov avatar Feb 25 '16 15:02 bahmutov

  • I tried every combination I could think of by changing the order, but the problem still persists.
  • You can just use the code I pasted above as the unit test :-)

You don't have to have electron installed, since mockery should substitute it for ./electron/stub. All you'll have to do is create the stub file at that path and run the test.

brian-mann avatar Feb 25 '16 15:02 brian-mann