How to remove module after "require" in node.js?
Spent some time trying to clear cache in Jest tests for Vuex store with no luck. Seems like Jest has its own mechanism that doesn't need manual call to delete require.cache.
beforeEach(() => {
jest.resetModules();
});
And tests:
let store;
it("1", () => {
process.env.something = true;
store = require("@/src/store.index");
});
it("2", () => {
process.env.something = false;
store = require("@/src/store.index");
});
Both stores will be different modules.
You can use this to delete its entry in the cache:
delete require.cache[require.resolve('./b.js')]
require.resolve()
will figure out the full path of ./b.js
, which is used as a cache key.