Registry / testing / resnap

resnap

JSON →
library1.0.1jsnpmunverified

Version 1.0.1. Lightweight utility for capturing and restoring Node.js require() cache state. Enables module-level isolation in tests by clearing cached modules and ensuring fresh loads. Simpler than alternatives like decache as it provides a single snapshot/restore mechanism. Stable with no recent releases; suitable for Node.js environments where ESM is not required.

npm install resnap
INSTALL
IMPORT
SIG · RESNAP
R
resnap
testingjavascriptv1.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import resnap from 'resnap'
const { resnap } = require('resnap')
Package only exports a single function. Use default import in ESM. For CJS, use require('resnap') directly.
restore
import resnap from 'resnap'; const restore = resnap()
const { restore } = require('resnap')()
The exported function returns a restore function. Do not destructure the import; call it.
commonjs require
const resnap = require('resnap')
const resnap = require('resnap').resnap
For CommonJS, import the module directly. The function is the default export.

Demonstrates capturing require cache state, loading modules, then restoring to clear new cache entries.

// Capture cache state const resnap = require('resnap'); const restore = resnap(); // Load some modules const fs = require('fs'); const path = require('path'); // Do work with modules... // Restore cache to previous state, clearing any modules loaded after capture restore(); // Now require('fs') will load the module again from scratch if called console.log('Cache restored');
Debug
Known issues
gotchaCalling restore() multiple times may cause unexpected behavior. Each restore call clears modules loaded since last snapshot.
fix
Call resnap() again to create a new snapshot before restoring multiple times.
affects: all
gotchaDoes not work with ES modules (ESM). Only affects CommonJS require() cache.
fix
Use for CommonJS modules only. For ESM, consider other approaches like import() with URL manipulation.
affects: all
gotchaRestoring cache does not reload built-in modules (like 'fs') from disk; they are always cached by Node internally.
fix
This is expected behavior; built-in modules are never removed from require cache by resnap.
affects: all
Errors
Common errors & fixes
TypeError: resnap is not a function
Using require with destructuring when the module exports a single function directly.
fix
Use const resnap = require('resnap'); instead of const { resnap } = require('resnap');
ReferenceError: restore is not defined
Forgetting to call resnap() to get the restore function.
fix
Call const restore = resnap(); first, then use restore() later.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
packageresnap
resnap — npm install resnap · libregistry