Skip to content

Instantly share code, notes, and snippets.

@aackerman
Last active August 29, 2015 14:27
Show Gist options
  • Select an option

  • Save aackerman/bd551495240510056bff to your computer and use it in GitHub Desktop.

Select an option

Save aackerman/bd551495240510056bff to your computer and use it in GitHub Desktop.
Resetting the webpack module cache
// Necessary for the karma-webpack alternative use-case https://github.com/webpack/karma-webpack#alternative-usage
//
// With the alternative usage setup your tests will be bundled with all other modules and tests.
// Each test will not have its own bundle of modules and tests have to handle cleaning up module state in between tests.
//
// This ends up being much better in the long run because each test can have a large number of deps and a single test
// may end up being very large and using a lot of memory and eventually cause OOM exceptions in the default usage setup.
//
// This solves that problem
jasmine.getEnv().topSuite().afterEach({
fn: () => { resetModuleCache(); }
});
function resetModuleCache() {
// assuming this module is compiled by webpack the 'magic' webpack require will be available
// and will expose the module cache as a property on the require method
let moduleCache = __webpack_require__.c;
for (let id in moduleCache) {
delete moduleCache[id];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment