Huge News!Announcing our $40M Series B led by Abstract Ventures.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.2 to 1.3.3

_tests/lib/a/foo2.js

41

_tests/mock.spec.js

@@ -103,2 +103,43 @@ import {expect} from 'chai';

it('should replace one module by another: ', () => {
return rewiremock.around( () => require('./lib/a/test.js'),
()=> {
addPlugin(relativePlugin);
rewiremock('./foo').by('./foo2');
})
.then( mocked => expect(mocked()).to.be.equal('FOObarbaz'));
});
it('should replace one module by autogenerated one: ', () => {
let toched = false;
return rewiremock.around( () => require('./lib/a/test.js'),
()=> {
addPlugin(relativePlugin);
rewiremock('./foo').by(({name}) => {
toched = name;
return () => "FOO";
});
expect(toched).to.be.false;
})
.then( mocked => {
expect(mocked()).to.be.equal('FOObarbaz');
expect(toched).to.be.equal('./foo');
});
});
it('should replace one module by autogenerated one with callThougth: ', () => {
return rewiremock.around( () => require('./lib/a/test.js'),
()=> {
addPlugin(relativePlugin);
rewiremock('./foo')
.callThought()
.by(({original}) => {
return () => "~"+original()+'~'
});
})
.then( mocked => expect(mocked()).to.be.equal('~foo~barbaz'));
});
});

28

lib/executor.js

@@ -83,15 +83,27 @@ 'use strict';

if (mock.allowCallThought) {
if (!mock.original) {
mock.original = originalLoader(request, parent, isMain);
}
}
if (mock.overrideBy) {
if (!mock.module) {
mock.module = originalLoader(mock.overrideBy, parent, isMain);
if (!mock.override) {
if (typeof mock.overrideBy === 'string') {
mock.override = originalLoader(mock.overrideBy, parent, isMain);
} else {
mock.override = mock.overrideBy({
name: request,
fullName: baseRequest,
parent: parent,
original: mock.original
});
}
}
return mockResult(request, mock.module);
return mockResult(request, mock.override);
}
if (mock.allowCallThought) {
if (!mock.module) {
mock.module = originalLoader(request, parent, isMain);
}
return mockResult(request, Object.assign({}, mock.module, mock.value, {
__esModule: mock.module.__esModule
return mockResult(request, Object.assign({}, mock.original, mock.value, {
__esModule: mock.original.__esModule
}));

@@ -98,0 +110,0 @@ }

@@ -90,3 +90,3 @@ 'use strict';

* Overriding export of one module by another
* @param {String} name
* @param {String|Function} newTarget
* @return {ModuleMock}

@@ -97,4 +97,8 @@ */

key: 'by',
value: function by(name) {
this.mock.overrideBy = (0, _plugins.convertName)(name, (0, _globals2.default)().parentModule);
value: function by(newTarget) {
if (typeof newTarget == 'string') {
this.mock.overrideBy = (0, _plugins.convertName)(newTarget, (0, _globals2.default)().parentModule);
} else {
this.mock.overrideBy = newTarget;
}
return this;

@@ -101,0 +105,0 @@ }

@@ -7,6 +7,2 @@ 'use strict';

var _globals = require('../globals');
var _globals2 = _interopRequireDefault(_globals);
var _common = require('./_common');

@@ -13,0 +9,0 @@

{
"name": "rewiremock",
"version": "1.3.2",
"version": "1.3.3",
"description": "Easy and es6 compatible mocking tool",

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

@@ -0,1 +1,8 @@

interface OverloadedModule {
name: String,
fileName: String,
parent: Object,
original: Object
};
interface ModuleMock {

@@ -26,2 +33,6 @@ /**

by(module: string): ModuleMock,
/**
* Overriding export of one module by something generated by a function
*/
by(module: (OverloadedModule) => Object): ModuleMock,

@@ -28,0 +39,0 @@ enable(): ModuleMock,

@@ -56,3 +56,3 @@ import Module from 'module';

if (mock) {
if(shouldMock(mock, request, parent, parentModule)) {
if (shouldMock(mock, request, parent, parentModule)) {
// this file fill be not cached, but it`s opener - will. And we have to remeber it

@@ -65,17 +65,29 @@ mockedModules[parent.filename] = true;

if (mock.allowCallThought) {
if (!mock.original) {
mock.original = originalLoader(request, parent, isMain);
}
}
if (mock.overrideBy) {
if (!mock.module) {
mock.module = originalLoader(mock.overrideBy, parent, isMain)
if (!mock.override) {
if (typeof mock.overrideBy === 'string') {
mock.override = originalLoader(mock.overrideBy, parent, isMain)
} else {
mock.override = mock.overrideBy({
name: request,
fullName: baseRequest,
parent: parent,
original: mock.original
});
}
}
return mockResult(request, mock.module);
return mockResult(request, mock.override);
}
if (mock.allowCallThought) {
if (!mock.module) {
mock.module = originalLoader(request, parent, isMain);
}
return mockResult(request, {
...mock.module,
...mock.original,
...mock.value,
__esModule: mock.module.__esModule
__esModule: mock.original.__esModule
});

@@ -82,0 +94,0 @@ }

@@ -58,7 +58,11 @@ import {convertName} from './plugins';

* Overriding export of one module by another
* @param {String} name
* @param {String|Function} newTarget
* @return {ModuleMock}
*/
by(name) {
this.mock.overrideBy = convertName(name, getScope().parentModule);
by(newTarget) {
if (typeof newTarget == 'string') {
this.mock.overrideBy = convertName(newTarget, getScope().parentModule);
} else {
this.mock.overrideBy = newTarget;
}
return this;

@@ -65,0 +69,0 @@ }

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

import getScope from '../globals';
import createPlugin from './_common';

@@ -3,0 +2,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