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.6 to 1.3.7

246

_tests/mock.spec.js

@@ -10,25 +10,4 @@ import {expect} from 'chai';

describe('rewiremock ', () => {
it('should not overload: ', () => {
rewiremock('./lib/a/foo')
.with(() => 'aa');
rewiremock('./lib/a/../b/bar')
.with(() => 'bb');
rewiremock('./lib/a/../b/baz')
.with(() => 'cc');
const unmockedBaz = require('./lib/a/test.js');
expect(unmockedBaz()).to.be.equal('foobarbaz');
rewiremock.enable();
const mockedBaz = require('./lib/a/test.js');
expect(mockedBaz()).to.be.equal('foobarbaz');
rewiremock.disable();
});
it('should overload with node plugin: ', () => {
rewiremock.inScope(() => {
addPlugin(nodePlugin);
describe('overloads ', () => {
it('should not overload: ', () => {
rewiremock('./lib/a/foo')

@@ -49,116 +28,169 @@ .with(() => 'aa');

const mockedBaz = require('./lib/a/test.js');
expect(mockedBaz()).to.be.equal('aabbcc');
expect(mockedBaz()).to.be.equal('foobarbaz');
rewiremock.disable();
});
});
it('should overload with relative plugin: ', () => {
rewiremock.inScope(() => {
addPlugin(relativePlugin);
rewiremock('./foo')
.with(() => 'aa');
it('should overload with node plugin: ', () => {
rewiremock.inScope(() => {
addPlugin(nodePlugin);
rewiremock('./lib/a/foo')
.with(() => 'aa');
rewiremock('../b/bar')
.with(() => 'bb');
rewiremock('./lib/a/../b/bar')
.with(() => 'bb');
rewiremock('../b/baz')
.with(() => 'cc');
rewiremock('./lib/a/../b/baz')
.with(() => 'cc');
const unmockedBaz = require('./lib/a/test.js');
expect(unmockedBaz()).to.be.equal('foobarbaz');
const unmockedBaz = require('./lib/a/test.js');
expect(unmockedBaz()).to.be.equal('foobarbaz');
rewiremock.enable();
rewiremock.enable();
const mockedBaz = require('./lib/a/test.js');
expect(mockedBaz()).to.be.equal('aabbcc');
rewiremock.disable();
const mockedBaz = require('./lib/a/test.js');
expect(mockedBaz()).to.be.equal('aabbcc');
rewiremock.disable();
});
});
});
it('should overload with webpack alias plugin: ', () => {
rewiremock.inScope(() => {
configureWebpackAlias('_tests/webpack.config.js');
it('should overload with relative plugin: ', () => {
rewiremock.inScope(() => {
addPlugin(relativePlugin);
rewiremock('./foo')
.with(() => 'aa');
addPlugin(aliasPlugin);
rewiremock('my-absolute-test-lib/foo')
.with(() => 'aa');
rewiremock('../b/bar')
.with(() => 'bb');
rewiremock('same-folder-lib/bar')
.with(() => 'bb');
rewiremock('../b/baz')
.with(() => 'cc');
rewiremock('../b/baz')
.with(() => 'cc');
const unmockedBaz = require('./lib/a/test.js');
expect(unmockedBaz()).to.be.equal('foobarbaz');
const unmockedBaz = require('./lib/a/test.js');
expect(unmockedBaz()).to.be.equal('foobarbaz');
rewiremock.enable();
rewiremock.enable();
const mockedBaz = require('./lib/a/test.js');
expect(mockedBaz()).to.be.equal('aabbcc');
rewiremock.disable();
});
});
const mockedBaz = require('./lib/a/test.js');
expect(mockedBaz()).to.be.equal('aabbcc');
rewiremock.disable();
it('should overload with webpack alias plugin: ', () => {
rewiremock.inScope(() => {
configureWebpackAlias('_tests/webpack.config.js');
addPlugin(aliasPlugin);
rewiremock('my-absolute-test-lib/foo')
.with(() => 'aa');
rewiremock('same-folder-lib/bar')
.with(() => 'bb');
rewiremock('../b/baz')
.with(() => 'cc');
const unmockedBaz = require('./lib/a/test.js');
expect(unmockedBaz()).to.be.equal('foobarbaz');
rewiremock.enable();
const mockedBaz = require('./lib/a/test.js');
expect(mockedBaz()).to.be.equal('aabbcc');
rewiremock.disable();
});
});
});
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'));
});
describe('mocking ', () => {
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);
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";
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');
});
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);
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'));
rewiremock('./foo')
.callThought()
.by(({original}) => {
return () => "~" + original() + '~'
});
})
.then(mocked => expect(mocked()).to.be.equal('~foo~barbaz'));
});
});
it('should mock any level entity: ', () => {
return rewiremock.around(() => require('./lib/c/barbaz.js'),
() => {
rewiremock('./baz')
.with(() => 'mock');
})
.then(mocked => expect(mocked()).to.be.equal('>+mockmock'));
describe('direct child ', () => {
it('should mock any level entity: ', () => {
return rewiremock.around(() => require('./lib/c/barbaz.js'),
() => {
rewiremock('./baz')
.with(() => 'mock');
})
.then(mocked => expect(mocked()).to.be.equal('>+mockmock'));
});
it('should mock top level entity only: ', () => {
return rewiremock.around(() => require('./lib/c/barbaz.js'),
() => {
//addPlugin(nodePlugin);
rewiremock('./baz')
.with(() => 'mock')
.directChildOnly();
})
.then(mocked => expect(mocked()).to.be.equal('>+!mock'));
});
});
it('should mock top level entity only: ', () => {
return rewiremock.around(() => require('./lib/c/barbaz.js'),
() => {
//addPlugin(nodePlugin);
rewiremock('./baz')
.with(() => 'mock')
.directChildOnly();
})
.then(mocked => expect(mocked()).to.be.equal('>+!mock'));
describe('called from a mock ', () => {
it('should mock top level entity only due to mocked parent: ', () => {
return rewiremock.around(() => require('./lib/c/barbaz.js'),
() => {
//addPlugin(nodePlugin);
rewiremock('./baz')
.with(() => 'mock')
.calledFromMock();
})
.then(mocked => expect(mocked()).to.be.equal('>+!mock'));
});
it('should mock all due to callthought mocked : ', () => {
return rewiremock.around(() => require('./lib/c/barbaz.js'),
() => {
//addPlugin(nodePlugin);
rewiremock('./baz')
.with(() => 'mock')
.calledFromMock();
rewiremock('./bar')
.callThought();
})
.then(mocked => expect(mocked()).to.be.equal('>+mockmock'));
});
});
});

@@ -129,2 +129,14 @@ 'use strict';

}, {
key: 'calledFromMock',
value: function calledFromMock() {
this.mock.flag_toBeCalledFromMock = true;
return this;
}
}, {
key: 'calledFromAnywhere',
value: function calledFromAnywhere() {
this.mock.flag_toBeCalledFromMock = false;
return this;
}
}, {
key: 'toBeUsed',

@@ -131,0 +143,0 @@ value: function toBeUsed() {

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

var _globals = require('../globals');
var _globals2 = _interopRequireDefault(_globals);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -18,2 +22,8 @@

}
if (mock.flag_toBeCalledFromMock) {
var _getScope = (0, _globals2.default)(),
mockedModules = _getScope.mockedModules;
return parent.filename in mockedModules ? _common.PASS : _common.NO;
}
};

@@ -20,0 +30,0 @@

{
"name": "rewiremock",
"version": "1.3.6",
"description": "Easy and es6 compatible di/mocking tool.",
"version": "1.3.7",
"description": "Easy and es6 compatible dependency mocking tool.",
"main": "lib/index.js",

@@ -6,0 +6,0 @@ "module": "src/index.js",

@@ -47,3 +47,9 @@ interface OverloadedModule {

atAnyPlace(): ModuleMock;
/**
* mocks only if parent were mocked
*/
calledFromMock(): ModuleMock;
calledFromAnywhere(): ModuleMock;
/**
* Force mock to be used, or throw an error otherwise

@@ -50,0 +56,0 @@ */

@@ -90,2 +90,12 @@ import {convertName} from './plugins';

calledFromMock() {
this.mock.flag_toBeCalledFromMock = true;
return this;
}
calledFromAnywhere() {
this.mock.flag_toBeCalledFromMock = false;
return this;
}
toBeUsed() {

@@ -92,0 +102,0 @@ this.mock.flag_toBeUsed = true;

import createPlugin, { PASS, NO } from './_common';
import getScope from '../globals';

@@ -7,2 +8,6 @@ const shouldMock = (mock, request, parent, topModule) => {

}
if(mock.flag_toBeCalledFromMock){
const { mockedModules } = getScope();
return parent.filename in mockedModules ? PASS : NO;
}
};

@@ -9,0 +14,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