Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>EDIT:</strong></p> <p>I stand corrected. As <a href="https://stackoverflow.com/users/551548/seppo0010">seppo0010</a> has pointed out, you can force a reload by deleting the cached module from <code>require.cache</code>: <a href="http://nodejs.org/docs/latest/api/globals.html#globals_require_cache" rel="nofollow noreferrer">http://nodejs.org/docs/latest/api/globals.html#globals_require_cache</a></p> <p>That said, I would still recommend against doing this for the reasons mentioned below. Then again, if you are <em>only</em> doing it in your unit test layer, you may be able to squeak by without any infinite dependency chains.</p> <hr> <p><strong><em>original answer</em>:</strong></p> <p>No, there is really no way to do this. Also from the documentation:</p> <blockquote> <p>Multiple calls to require('foo') may not cause the module code to be executed multiple times. This is an important feature. With it, "partially done" objects can be returned, thus allowing transitive dependencies to be loaded even when they would cause cycles.</p> <p>If you want to have a module execute code multiple times, then export a function, and call that function.</p> </blockquote> <p>Two points here:</p> <ol> <li><p>The reason this is necessary is to allow cycles to be resolved. You can see an example of this here: <a href="http://nodejs.org/docs/latest/api/modules.html#modules_cycles" rel="nofollow noreferrer">http://nodejs.org/docs/latest/api/modules.html#modules_cycles</a>. If you could invalidate the cache somehow, you could cause an infinite loop due to circular dependencies. Even if you can be reasonably confident that your application code won't cause this, it could happen in any libraries you use.</p></li> <li><p>As the documentation points out, you can just wrap the functionality in a function that you can call in each test. This is generally a pretty good design pattern as well.</p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload