mock-require
Advanced tools
Comparing version 1.2.1 to 1.3.0
@@ -36,2 +36,8 @@ var Module = require('module') | ||
function reRequire(path) { | ||
var module = getFullPath(path, callerId.getData().filePath); | ||
delete require.cache[require.resolve(module)]; | ||
return require(module); | ||
} | ||
function getFullPath(path, calledFrom) { | ||
@@ -70,1 +76,2 @@ var resolvedPath; | ||
module.exports.stopAll = stopMockingAll; | ||
module.exports.reRequire = reRequire; |
{ | ||
"name": "mock-require", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "Simple, intuitive mocking of Node.js modules.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -110,2 +110,28 @@ # mock-require | ||
### `mock.reRequire(path)` | ||
__path__: `String` | ||
The file whose cache you want to refresh. This is useful if you're trying to mock a dependency for a file that has already been required elsewhere (possibly in another test file). Normally, Node.js will cache this file, so any mocks that you apply afterwards will have no effect. `reRequire` clears the cache and allows your mock to work. | ||
```javascript | ||
var fs = require('fs'); | ||
var fileToTest = require('./fileToTest'); | ||
mock('fs', {}); // fileToTest is still using the unmocked fs module | ||
fileToTest = mock.reRequire('./fileToTest'); // fileToTest is now using your mock | ||
``` | ||
Note that if the file you are testing requires dependencies that in turn require the mock, those dependencies will still have the unmocked version. You may want to `reRequire` all of your dependencies to ensure that your mock is always being used. | ||
```javascript | ||
var fs = require('fs'); | ||
var otherDep = require('./otherDep') // requires fs as a dependency | ||
var fileToTest = require('./fileToTest'); // requires fs and otherDep as a dependency | ||
mock('fs', {}); // fileToTest and otherDep are still using the unmocked fs module | ||
otherDep = mock.reRequire('./otherDep'); // do this to make sure fs is being mocked consistently | ||
fileToTest = mock.reRequire('./fileToTest'); | ||
``` | ||
## Test | ||
@@ -112,0 +138,0 @@ |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
13032
225
141
12