Skip to content

Simple API for capturing and resetting Node's require() cache state

License

Notifications You must be signed in to change notification settings

hexmarkrecords/resnap

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

resnap

Simple API for capturing and restoring Node's require() cache state.

Usage

const resnap = require('resnap')

const restore = resnap(); // Capture require() cache state

// ... require() some modules, run some code, etc...

restore();  // Restore previous cache state

Example: Resetting Mocha test state

In the Mocha test file below, foo1, foo2, and foo3 are all distinct instances of the 'foo' module due to beforeEach(resetCache) clearing the require() cache state before each test.

const resetCache = require('resnap')(); // Capture "clean" cache state

describe(__filename, function() {
  beforeEach(resetCache);

  it('test 1', function(done) {
    const foo1 = require('./foo'); // Loads 'foo' module from disk
    // ...
    done();
  });

  it('test 2', function(done) {
    const foo2 = require('./foo'); // Loads 'foo' from disk (yes, again!)
    // ...
    done();
  });

  it('test 3', function(done) {
    const foo3 = require('./foo'); // Loads 'foo' from disk (yes, again!)
    // ...
    done();
  });
});

About

Simple API for capturing and resetting Node's require() cache state

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%