sindresorhus / import-fresh

Import a module while bypassing the cache
MIT License
283 stars 24 forks source link

Not working with Jest #9

Closed sinedied closed 5 years ago

sinedied commented 5 years ago

When running unit tests with Jest, the importFresh function does not work properly, it always returns the same (cached?) module.

Might be due to the fact that Jest patch require with its own implementation :/

sindresorhus commented 5 years ago

Can you try out v3.0.0? It improves the logic for finding the module to clear.

sinedied commented 5 years ago

Just tried, same result. Jest have its own require implementation, that can also be overridden, so I'm not sure if there's anything that can be done here, unless I can force Jest to use the "real" require implementation :/

lonix1 commented 5 years ago

@sinedied Use jest.resetModules() before each test:

beforeEach(() => jest.resetModules());

(The implication is that you don't need this library when using Jest.)

When Jest 24 is released, there will be a way to cache bust specific modules instead of the whole thing.

sinedied commented 5 years ago

@lonix1 That's what I ended up doing, but as Jest doesn't go through the original require, it doesn't use this lib, making the unit test somewhat useless 😞 Still, it's not directly related to this library but rather than the way Jest load modules, so I'll close the issue.