New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

mockire

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mockire - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

9

index.js

@@ -49,2 +49,8 @@ const Mod = require('module');

function clearRequireCache() {
Object.keys(require.cache).forEach((key) => {
delete require.cache[key];
});
}
init();

@@ -57,3 +63,4 @@

reset,
init
init,
clearRequireCache
}

31

index.unit.spec.js
const assert = require('assert');
const { match, clearStubs, exact, reset, init } = require('./index');
const { match, clearStubs, exact, reset, init, clearRequireCache } = require('./index');

@@ -19,3 +19,3 @@ describe('Unit tests', () => {

assert.deepEqual(require('./some/path/env.json'), fixtures.requiredMock);
assert.deepStrictEqual(require('./some/path/env.json'), fixtures.requiredMock);

@@ -27,3 +27,3 @@ done();

assert.deepEqual(require('./some/path/env.json'), fixtures.requiredMock);
assert.deepStrictEqual(require('./some/path/env.json'), fixtures.requiredMock);

@@ -35,3 +35,3 @@ done();

assert.deepEqual(require('./some/path/env.json'), fixtures.requiredMock);
assert.deepStrictEqual(require('./some/path/env.json'), fixtures.requiredMock);

@@ -46,4 +46,4 @@ done();

assert.deepEqual(require('./some/path/other/env.json'), otherMock);
assert.deepEqual(require('./some/path/env.json'), fixtures.requiredMock);
assert.deepStrictEqual(require('./some/path/other/env.json'), otherMock);
assert.deepStrictEqual(require('./some/path/env.json'), fixtures.requiredMock);

@@ -68,3 +68,3 @@ done();

assert.deepEqual(require('./some/path/env.json'), fixtures.requiredMock);
assert.deepStrictEqual(require('./some/path/env.json'), fixtures.requiredMock);

@@ -89,3 +89,3 @@ done();

assert.deepEqual(require('./some/path/env.json'), fixtures.requiredMock);
assert.deepStrictEqual(require('./some/path/env.json'), fixtures.requiredMock);

@@ -107,3 +107,3 @@ reset();

assert.deepEqual(require('./some/path/env.json'), fixtures.requiredMock);
assert.deepStrictEqual(require('./some/path/env.json'), fixtures.requiredMock);

@@ -121,3 +121,3 @@ reset();

assert.deepEqual(require('./some/path/env.json'), fixtures.requiredMock);
assert.deepStrictEqual(require('./some/path/env.json'), fixtures.requiredMock);

@@ -132,3 +132,3 @@ done();

assert.deepEqual(require('./some/path/env.json'), fixtures.requiredMock);
assert.deepStrictEqual(require('./some/path/env.json'), fixtures.requiredMock);

@@ -145,2 +145,11 @@ clearStubs();

});
describe('clearRequireCache', () => {
it('must be able to clear the require cache', (done) => {
assert.strictEqual(Object.keys(require.cache).length > 0, true)
clearRequireCache();
assert.strictEqual(Object.keys(require.cache).length === 0, true)
done()
});
});
});
{
"name": "mockire",
"version": "1.0.0",
"version": "1.0.1",
"main": "index.js",
"license": "MIT",
"keywords": ["require", "stub", "test", "unit-tests", "dev"],
"repository": {
"type": "git",
"url": "https://github.com/victorsferreira/mockire"
},
"scripts": {

@@ -7,0 +12,0 @@ "test": "clear; mocha **/*.spec.js"

@@ -1,2 +0,2 @@

This library allows developers to stub the `require` built in method.
This library allows developers to stub and clear the `require` built-in method.

@@ -9,6 +9,45 @@ The Mockire interceptor is initialized as soon as the module is required.

- `match(pattern, value)`: match the module path against a regular expression
- `exact(input, value)`: expects the provided path to be exactly as required
- `clearStubs()`: clear all stubs
- `reset()`: remove the monkey patch
- `init()`: initialize the interceptor.
- `match(pattern, value)`: matches the module path against a regular expression.
- `exact(input, value)`: expects the provided path to be exactly as required.
- `clearStubs()`: clears all stubs.
- `reset()`: removes the monkey patch.
- `init()`: initializes the interceptor.
- `clearRequireCache()`: clears all modules stored in the require cache. Modules will be loaded again next time `require` is invoked.
## Use cases:
# Clearing cache before tests start
```
const { clearRequireCache } = require('mockire');
let server;
describe('Unit tests', () => {
before((done) => {
clearRequireCache();
server = require('./MyCustomServer');
})
}
```
# Loads fake dependencies
```
const { match } = require('mockire');
match('MyProjectDirectory/config/env/*', { port: 8000, db: { host: 'localhost', port: '27017' } });
const server = require('./MyCustomServer');
describe('Unit tests', () => {
before((done) => {
server.start()
})
}
```
# Creates virtual dependency
```
const { exact, clearStubs } = require('mockire');
if (!fs.existsSync('./path/customLib)) {
exact('./path/customLib', { something });
}
const lib = require('./path/customLib');
await lib.foo();
clearStubs();
```
SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc