{"action":"created","issue":{"url":"https://api.github.com/repos/webpack/webpack/issues/894","labels_url":"https://api.github.com/repos/webpack/webpack/issues/894/labels{/name}","comments_url":"https://api.github.com/repos/webpack/webpack/issues/894/comments","events_url":"https://api.github.com/repos/webpack/webpack/issues/894/events","html_url":"https://github.com/webpack/webpack/issues/894","id":62235272,"number":894,"title":"Clearing require.cache for testing","user":{"login":"kentor","id":240493,"avatar_url":"https://avatars.githubusercontent.com/u/240493?v=3","gravatar_id":"","url":"https://api.github.com/users/kentor","html_url":"https://github.com/kentor","followers_url":"https://api.github.com/users/kentor/followers","following_url":"https://api.github.com/users/kentor/following{/other_user}","gists_url":"https://api.github.com/users/kentor/gists{/gist_id}","starred_url":"https://api.github.com/users/kentor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kentor/subscriptions","organizations_url":"https://api.github.com/users/kentor/orgs","repos_url":"https://api.github.com/users/kentor/repos","events_url":"https://api.github.com/users/kentor/events{/privacy}","received_events_url":"https://api.github.com/users/kentor/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-03-16T23:22:23Z","updated_at":"2015-12-08T11:34:25Z","closed_at":"2015-03-17T05:52:57Z","body":"I'm using karma + jasmine + webpack to test my react app, following https://www.codementor.io/reactjs/tutorial/test-reactjs-components-karma-webpack.\r\n\r\nSince my stores are singletons, I am clearing the module cache in a global `beforeEach` as to reset the state of the store and any other related module (actions/dispatcher/etc):\r\n\r\n```javascript\r\nbeforeEach(() => {\r\n  var cache = require.cache;\r\n  for (var moduleId in cache) {\r\n    delete cache[moduleId];\r\n  }\r\n});\r\n```\r\n\r\nThis works, but as an optimization I want to clear only the modules from my own project, not any of the modules installed in `node_modules`. Is there a way to do this currently?"},"comment":{"url":"https://api.github.com/repos/webpack/webpack/issues/comments/162856905","html_url":"https://github.com/webpack/webpack/issues/894#issuecomment-162856905","issue_url":"https://api.github.com/repos/webpack/webpack/issues/894","id":162856905,"user":{"login":"ponelat","id":8438485,"avatar_url":"https://avatars.githubusercontent.com/u/8438485?v=3","gravatar_id":"","url":"https://api.github.com/users/ponelat","html_url":"https://github.com/ponelat","followers_url":"https://api.github.com/users/ponelat/followers","following_url":"https://api.github.com/users/ponelat/following{/other_user}","gists_url":"https://api.github.com/users/ponelat/gists{/gist_id}","starred_url":"https://api.github.com/users/ponelat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ponelat/subscriptions","organizations_url":"https://api.github.com/users/ponelat/orgs","repos_url":"https://api.github.com/users/ponelat/repos","events_url":"https://api.github.com/users/ponelat/events{/privacy}","received_events_url":"https://api.github.com/users/ponelat/received_events","type":"User","site_admin":false},"created_at":"2015-12-08T11:34:25Z","updated_at":"2015-12-08T11:34:25Z","body":"@fixpunkt thanks for that, so here is what solved my case...\r\n\r\nUse any method to clear cache, such that `delete require.cache[id]` happens.\r\n`import` only works if placed at the top of the document, and it'll contain a reference to the original exported object, which can get mutated.\r\nSo we need to call `require('something')` during runtime, to get a fresh obj.\r\n\r\nexample:\r\n```js\r\nvar SomeObj;\r\n\r\ndescribe('Something', function() {\r\n  beforeEach(function() {\r\n    delete require.cache[require.resolve('./some-obj')] // Clear the cache, this can be done in a way described in the example repo. Does not have to happen here\r\n\r\n    SomeObj = require('some-obj') // Update our reference, this needs to happen here\r\n  })\r\n  it('should work', function() {\r\n     expect(SomeObj.one).toEqual(1)\r\n     SomeObj.one = 2; // Mutate object, unless we bust the cache, this is a global mutation - scary!\r\n  })\r\n})\r\n```\r\n\r\n> Note: this will work with karma + webpack bundles."}}