require-cache-control

Control require cache, take snapshots and restore. Useful when testing code.


Keywords
require, cache, control, snapshot, restore
License
MIT
Install
npm install require-cache-control@0.1.1

Documentation

require-cache-control

Build status Coverage

Let's you take a snapshot of the current require cache and restore it at will. This is useful if you want to test some files repeatadly in a single process.

Imagine you have a gulp process running and your testing plugin uses require() to test the files. After the first require, you won't be able to re-test your files because of the require cache.

This is the case with gulp-tape, it uses require-uncached to freshly load the the test files to work around this problem. The problem is require-uncached does not take care of subsequent requires. It only freshly loads the file that is given to it, if the file itself requires other files, they will be cached and won't be fresh.

Install


npm install require-cache-control

Usage


const rcc = require('require-cache-control');

// Assuming `tests` is a collection containing file names
for (let test of tests) {
  // Capture the state of the require cache§
  let snapshot = rcc.snapshot();
  require(test);
  // Restore to the captured state
  // `test` itself and all the require()s made by it will be removed from cache
  rcc.restore(snapshot); 
}