Socket
Socket
Sign inDemoInstall

mock-require

Package Overview
Dependencies
2
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.2.1 to 1.3.0

7

index.js

@@ -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;

2

package.json
{
"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 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc