Skip to content

After require, delete this module from the cache#3

Open
mlrawlings wants to merge 1 commit intomlmorg:masterfrom
mlrawlings:master
Open

After require, delete this module from the cache#3
mlrawlings wants to merge 1 commit intomlmorg:masterfrom
mlrawlings:master

Conversation

@mlrawlings
Copy link

Because of caching, mockit's module.parent always refers to the first file from which it is called. That kinda works out if all of your test files are in the same directory, since the path used to resolve things will be the same. I like to keep my tests in the same directory as the file they're testing and that means that the require paths don't match up.

/lib
   /model
       /user.js
       /user.test.js
   /controller
       /login.js
       /login.test.js

In the example directory structure above, if both user.test.js and login.test.js use mockit, when the user test calls require('./user') it'll work just fine because it runs first, but then when the login test calls require('./login') an error will be thrown because it's actually looking for the file at /lib/model/login.js due to module.parent still referring to user.test.js.

Deleting mockit from require.cache means module.parent actually refers the file that just required it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant