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

rewiremock

Package Overview
Dependencies
Maintainers
1
Versions
71
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rewiremock - npm Package Compare versions

Comparing version 1.3.4 to 1.3.5

_tests/lib/c/bar.js

184

_tests/isolation.spec.js

@@ -9,95 +9,133 @@ import {expect} from 'chai';

describe('isolation ', () => {
it('should trigger error: ', () => {
addPlugin(nodePlugin);
rewiremock.enable();
rewiremock.isolation();
it('should trigger error: ', () => {
addPlugin(nodePlugin);
rewiremock.enable();
rewiremock.isolation();
try {
require('./lib/a/test.js');
expect("should not be called").to.be.equal(false);
} catch (e) {
try {
require('./lib/a/test.js');
expect("should not be called").to.be.equal(false);
} catch (e) {
}
rewiremock.disable();
rewiremock.clear();
_clearPlugins();
});
}
rewiremock.disable();
rewiremock.clear();
_clearPlugins();
});
it('work in isolation: ', () => {
addPlugin(nodePlugin);
rewiremock.passBy(/node_modules/);
rewiremock('./lib/a/foo')
.with(() => 'aa');
it('work in isolation: ', () => {
addPlugin(nodePlugin);
rewiremock.passBy(/node_modules/);
rewiremock('./lib/a/foo')
.with(() => 'aa');
rewiremock('./lib/a/../b/bar')
.with(() => 'bb');
rewiremock('./lib/a/../b/bar')
.with(() => 'bb');
rewiremock('./lib/a/../b/baz')
.with(() => 'cc');
rewiremock('./lib/a/../b/baz')
.with(() => 'cc');
rewiremock.enable();
rewiremock.isolation();
rewiremock.enable();
rewiremock.isolation();
const mockedBaz = require('./lib/a/test.js');
expect(mockedBaz()).to.be.equal('aabbcc');
rewiremock.disable();
rewiremock.clear();
_clearPlugins();
});
const mockedBaz = require('./lib/a/test.js');
expect(mockedBaz()).to.be.equal('aabbcc');
rewiremock.disable();
rewiremock.clear();
_clearPlugins();
});
it('should passby modules: ', () => {
addPlugin(nodePlugin);
rewiremock.passBy(/node_modules/);
rewiremock('./lib/a/foo')
.with(() => 'aa');
it('should passby modules: ', () => {
addPlugin(nodePlugin);
rewiremock.passBy(/node_modules/);
rewiremock('./lib/a/foo')
.with(() => 'aa');
rewiremock.passBy(module => module.indexOf('b/bar') >= 0);
rewiremock.passBy(/b\/baz/);
rewiremock.passBy(module => module.indexOf('b/bar') >= 0);
rewiremock.passBy(/b\/baz/);
rewiremock.enable();
rewiremock.isolation();
rewiremock.enable();
rewiremock.isolation();
const mockedBaz = require('./lib/a/test.js');
expect(mockedBaz()).to.be.equal('aabarbaz');
rewiremock.disable();
rewiremock.clear();
_clearPlugins();
});
const mockedBaz = require('./lib/a/test.js');
expect(mockedBaz()).to.be.equal('aabarbaz');
rewiremock.disable();
rewiremock.clear();
_clearPlugins();
});
it('should nest passby: ', () => {
addPlugin(nodePlugin);
rewiremock.passBy(/node_modules/);
rewiremock('./lib/a/foo')
.with(() => 'aa');
it('should nest passby: ', () => {
addPlugin(nodePlugin);
rewiremock.passBy(/node_modules/);
rewiremock('./lib/a/foo')
.with(() => 'aa');
rewiremock.passBy(/test.js/);
rewiremock.passBy(/test.js/);
rewiremock.enable();
rewiremock.isolation();
rewiremock.enable();
rewiremock.isolation();
const mockedBaz = require('./lib/a/test.js');
expect(mockedBaz()).to.be.equal('aabarbaz');
rewiremock.disable();
rewiremock.clear();
_clearPlugins();
const mockedBaz = require('./lib/a/test.js');
expect(mockedBaz()).to.be.equal('aabarbaz');
rewiremock.disable();
rewiremock.clear();
_clearPlugins();
});
it('should nest mocked module: ', () => {
addPlugin(nodePlugin);
rewiremock.passBy(/node_modules/);
rewiremock('./lib/c/bar')
.callThought();
rewiremock.enable();
rewiremock.isolation();
const mockedBaz = require('./lib/c/foo.js');
expect(mockedBaz()).to.be.equal('>+!');
rewiremock.disable();
rewiremock.clear();
_clearPlugins();
});
it('should nest mocked module with options: ', () => {
addPlugin(nodePlugin);
rewiremock.passBy(/node_modules/);
rewiremock('./lib/c/bar')
.callThought();
rewiremock.enable();
rewiremock.isolation({
noAutoPassBy: true
});
it('should inverse isolation by toBeUsed: ', () => {
rewiremock('./lib/a/foo')
.with(() => 'aa')
.toBeUsed();
try {
require('./lib/c/foo.js');
expect('should not be called').to.equal.false();
} catch(e) {
rewiremock.enable();
}
rewiremock.disable();
rewiremock.clear();
_clearPlugins();
});
const mockedBaz = require('./lib/a/test.js');
expect(mockedBaz()).to.be.equal('foobarbaz');
try {
rewiremock.disable();
expect('should not be called').to.equal.false();
} catch (e) {
it('should inverse isolation by toBeUsed: ', () => {
rewiremock('./lib/a/foo')
.with(() => 'aa')
.toBeUsed();
}
rewiremock.clear();
_clearPlugins();
});
rewiremock.enable();
const mockedBaz = require('./lib/a/test.js');
expect(mockedBaz()).to.be.equal('foobarbaz');
try {
rewiremock.disable();
expect('should not be called').to.equal.false();
} catch (e) {
}
rewiremock.clear();
_clearPlugins();
});
});

@@ -8,2 +8,4 @@ 'use strict';

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _module = require('module');

@@ -37,3 +39,6 @@

parentModule = _getScope.parentModule,
passBy = _getScope.passBy;
passBy = _getScope.passBy,
mockedModules = _getScope.mockedModules,
isolation = _getScope.isolation;
// was called from test

@@ -45,9 +50,11 @@

}
// if parent was in passlist - pass everythinh
// if parent is in the pass list - pass everything
var fileName = _module2.default._resolveFilename(request, module);
var m = module;
while (m) {
if (passBy.filter(patternMatch(fileName)).length) {
return true;
}
if (!isolation.noAutoPassBy && mockedModules[fileName] || // parent was mocked
passBy.filter(patternMatch(fileName)).length // parent is in pass list
) {
return true;
}
fileName = m.filename;

@@ -66,3 +73,4 @@ m = m.parent;

parentModule = _getScope2.parentModule,
mockedModules = _getScope2.mockedModules;
mockedModules = _getScope2.mockedModules,
isolation = _getScope2.isolation;

@@ -80,3 +88,3 @@ var baseRequest = _module2.default._resolveFilename(request, parent);

if ((0, _plugins.shouldMock)(mock, request, parent, parentModule)) {
// this file fill be not cached, but it`s opener - will. And we have to remeber it
// this file fill be not cached, but it`s opener - will. And we have to remember it
mockedModules[parent.filename] = true;

@@ -111,2 +119,9 @@ mock.usedAs = mock.usedAs || [];

if (mock.allowCallThought) {
if (typeof mock.original === 'function') {
if (_typeof(mock.value) === 'object' && Object.keys(mock.value).length === 0) {
return mockResult(request, mock.original);
} else {
throw new Error('rewiremock: trying to merge Functional base with CallThought mock at ' + request + '. Use overrideBy instead.');
}
}
return mockResult(request, Object.assign({}, mock.original, mock.value, {

@@ -122,5 +137,5 @@ __esModule: mock.original.__esModule

if ((0, _globals2.default)().isolation && !mockedModules[baseRequest]) {
if (isolation && !mockedModules[baseRequest]) {
if (!testPassby(request, parent)) {
throw new Error('mockModule: ' + request + ' in not listed as passby modules');
throw new Error('rewiremock: isolation breach by [' + request + ']. Requested from ', parent.filename);
}

@@ -127,0 +142,0 @@ }

@@ -78,5 +78,9 @@ 'use strict';

* Activates module isolation
* @param {Object} [options]
* @param {Boolean} [options.noAutoPassBy] includes mocked modules to a isolation scope. Usage with mock.callThought.
*/
mockModule.isolation = function () {
mockScope.isolation = true;
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
mockScope.isolation = Object.assign({}, options);
};

@@ -83,0 +87,0 @@

{
"name": "rewiremock",
"version": "1.3.4",
"version": "1.3.5",
"description": "Easy and es6 compatible di/mocking tool.",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -6,3 +6,4 @@ interface OverloadedModule {

original: Object
};
}
;

@@ -78,4 +79,5 @@ interface ModuleMock {

* Activates module isolation
* @param {Boolean} [options.noAutoPassBy] includes mocked modules to a isolation scope. Usage with mock.callThought.
*/
isolation();
isolation(options?: Object);

@@ -82,0 +84,0 @@ /**

@@ -16,3 +16,9 @@ import Module from 'module';

const testPassby = (request, module) => {
const {parentModule, passBy} = getScope();
const {
parentModule,
passBy,
mockedModules,
isolation
} = getScope();
// was called from test

@@ -22,7 +28,10 @@ if (module === parentModule || module == module.parent) {

}
// if parent was in passlist - pass everythinh
// if parent is in the pass list - pass everything
let fileName = Module._resolveFilename(request, module);
let m = module;
while (m) {
if (passBy.filter(patternMatch(fileName)).length) {
if (
(!isolation.noAutoPassBy && mockedModules[fileName]) || // parent was mocked
passBy.filter(patternMatch(fileName)).length // parent is in pass list
) {
return true;

@@ -45,3 +54,4 @@ }

mockedModules
mockedModules,
isolation
} = getScope();

@@ -60,3 +70,3 @@

if (shouldMock(mock, request, parent, parentModule)) {
// this file fill be not cached, but it`s opener - will. And we have to remeber it
// this file fill be not cached, but it`s opener - will. And we have to remember it
mockedModules[parent.filename] = true;

@@ -91,2 +101,13 @@ mock.usedAs = (mock.usedAs || []);

if (mock.allowCallThought) {
if(typeof(mock.original) === 'function') {
if (
typeof mock.value === 'object' &&
Object.keys(mock.value).length === 0
) {
return mockResult(request, mock.original);
} else {
throw new Error('rewiremock: trying to merge Functional base with CallThought mock at '
+ request + '. Use overrideBy instead.');
}
}
return mockResult(request, {

@@ -104,5 +125,5 @@ ...mock.original,

if (getScope().isolation && !mockedModules[baseRequest]) {
if (isolation && !mockedModules[baseRequest]) {
if (!testPassby(request, parent)) {
throw new Error('mockModule: ' + request + ' in not listed as passby modules');
throw new Error('rewiremock: isolation breach by [' + request + ']. Requested from ',parent.filename);
}

@@ -109,0 +130,0 @@ }

@@ -55,5 +55,7 @@ import Module from 'module';

* Activates module isolation
* @param {Object} [options]
* @param {Boolean} [options.noAutoPassBy] includes mocked modules to a isolation scope. Usage with mock.callThought.
*/
mockModule.isolation = () => {
mockScope.isolation = true;
mockModule.isolation = (options = {}) => {
mockScope.isolation = { ...options };
};

@@ -60,0 +62,0 @@

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