rewiremock
Advanced tools
Comparing version 3.4.3 to 3.5.0
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; }; | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
import { relative } from 'path'; | ||
@@ -7,3 +9,3 @@ import Module, { originalLoader } from './module'; | ||
import { getMock } from './mocks'; | ||
import getScope, { collectScopeVariable, getScopeVariable } from './globals'; | ||
import getScope, { collectScopeVariable, getScopeOption, getScopeVariable } from './globals'; | ||
import { moduleCompare, pickModuleName, getModuleName, getModuleParent } from './module'; | ||
@@ -85,5 +87,40 @@ import asyncModules from './asyncModules'; | ||
function standardStubFactory(name, object, deeperMock) { | ||
if (typeof object === 'function') { | ||
return function () {}; | ||
} | ||
if ((typeof object === 'undefined' ? 'undefined' : _typeof(object)) === 'object') { | ||
return deeperMock(deeperMock); | ||
} | ||
return object; | ||
} | ||
function mockThought(stubFactory, mockOriginal) { | ||
var name = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ''; | ||
if (typeof mockOriginal === 'function') { | ||
return stubFactory(name || 'default', mockOriginal); | ||
} | ||
if ((typeof mockOriginal === 'undefined' ? 'undefined' : _typeof(mockOriginal)) === 'object') { | ||
var deeperMock = function deeperMock(key, value) { | ||
return mockThought(stubFactory, value, name ? name + '.' + key : key); | ||
}; | ||
if (Array.isArray(mockOriginal)) { | ||
return mockOriginal.map(function (x, i) { | ||
return deeperMock(i, x); | ||
}); | ||
} else { | ||
return Object.keys(mockOriginal).map(function (key) { | ||
return { key: key, value: deeperMock(key, mockOriginal[key]) }; | ||
}).reduce(function (acc, x) { | ||
return Object.assign(acc, _defineProperty({}, x.key, x.value)); | ||
}, {}); | ||
} | ||
} | ||
return mockOriginal; | ||
} | ||
function monkeyPatchPath(addr) { | ||
var path = addr.split('/'); | ||
if (path[0] == '..') { | ||
if (path[0] === '..') { | ||
path[0] = '.'; | ||
@@ -140,2 +177,8 @@ return path.join('/'); | ||
if (mock.mockThrough) { | ||
var factory = mock.mockThrough === true ? getScopeOption('stubFactory') : mock.mockThrough; | ||
mock.override = mockThought(factory || standardStubFactory, mock.original); | ||
return mockResult(request, mock, mock.override); | ||
} | ||
if (mock.overrideBy) { | ||
@@ -142,0 +185,0 @@ if (!mock.override) { |
@@ -21,2 +21,14 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
export var getScopeOption = function getScopeOption(name) { | ||
var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : currentScope; | ||
if (name in scope.options) { | ||
return scope.options[name]; | ||
} | ||
if (scope.parentScope) { | ||
return getScopeOption(name, scope.parentScope); | ||
} | ||
return undefined; | ||
}; | ||
export var collectScopeVariable = function collectScopeVariable(name) { | ||
@@ -23,0 +35,0 @@ var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : currentScope; |
@@ -45,2 +45,3 @@ import path from 'path'; | ||
export { addPlugin, removePlugin, plugins }; | ||
export default API.mockModule; |
@@ -51,3 +51,3 @@ 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; }; | ||
* @name ModuleMock.mockThrough | ||
* @param {Function} [mockFactory=sinon.stub] - mock constructor | ||
* @param {Function} [mockFactory=() => emptyFunction] - mock constructor | ||
* @return {ModuleMock} | ||
@@ -54,0 +54,0 @@ */ |
@@ -14,9 +14,9 @@ var _this = this; | ||
var scope = function scope() { | ||
return setScope(mockScope); | ||
return setScope(mockScope); | ||
}; | ||
var updateScope = function updateScope() { | ||
var parentScope = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; | ||
var parentScope = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; | ||
mockScope = createScope(parentScope, parentModule); | ||
scope(); | ||
mockScope = createScope(parentScope, parentModule); | ||
scope(); | ||
}; | ||
@@ -34,24 +34,24 @@ | ||
function mockModule(moduleName) { | ||
scope(); | ||
if (typeof moduleName === 'function') { | ||
return onMockCreate(new ModuleMock(getAsyncMock(moduleName, parentModule))); | ||
} else { | ||
var name = convertName(moduleName, parentModule); | ||
resetMock(name); | ||
return onMockCreate(new ModuleMock(getMock(name))); | ||
} | ||
scope(); | ||
if (typeof moduleName === 'function') { | ||
return onMockCreate(new ModuleMock(getAsyncMock(moduleName, parentModule))); | ||
} else { | ||
var name = convertName(moduleName, parentModule); | ||
resetMock(name); | ||
return onMockCreate(new ModuleMock(getMock(name))); | ||
} | ||
} | ||
mockModule.getMock = function (module) { | ||
var moduleName = module; | ||
if (typeof moduleName === 'function') { | ||
moduleName = getAsyncModuleName(moduleName, parentModule); | ||
} else { | ||
moduleName = convertName(moduleName, parentModule); | ||
} | ||
var mock = getMock(moduleName); | ||
if (mock) { | ||
return new ModuleMock(mock); | ||
} | ||
return null; | ||
var moduleName = module; | ||
if (typeof moduleName === 'function') { | ||
moduleName = getAsyncModuleName(moduleName, parentModule); | ||
} else { | ||
moduleName = convertName(moduleName, parentModule); | ||
} | ||
var mock = getMock(moduleName); | ||
if (mock) { | ||
return new ModuleMock(mock); | ||
} | ||
return null; | ||
}; | ||
@@ -65,4 +65,4 @@ | ||
mockModule.resolve = function (module) { | ||
scope(); | ||
return convertName(module, parentModule); | ||
scope(); | ||
return convertName(module, parentModule); | ||
}; | ||
@@ -78,6 +78,6 @@ | ||
mockModule.isolation = function () { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
mockScope.isolation = Object.assign({}, options); | ||
return mockModule; | ||
mockScope.isolation = Object.assign({}, options); | ||
return mockModule; | ||
}; | ||
@@ -89,8 +89,8 @@ | ||
mockModule.withoutIsolation = function () { | ||
mockScope.isolation = false; | ||
return mockModule; | ||
mockScope.isolation = false; | ||
return mockModule; | ||
}; | ||
mockModule.forceCacheClear = function (mode) { | ||
mockScope.forceCacheClear = mode ? mode : true; | ||
mockScope.forceCacheClear = mode ? mode : true; | ||
}; | ||
@@ -103,8 +103,8 @@ | ||
mockModule.passBy = function (pattern) { | ||
mockScope.passBy.push(pattern); | ||
return mockModule; | ||
mockScope.passBy.push(pattern); | ||
return mockModule; | ||
}; | ||
mockModule.overrideEntryPoint = function (parent) { | ||
mockScope.parentModule = parentModule = parent || getModuleParent(getModuleParent(module)); | ||
mockScope.parentModule = parentModule = parent || getModuleParent(getModuleParent(module)); | ||
}; | ||
@@ -118,8 +118,8 @@ | ||
mockModule.enable = function () { | ||
scope(); | ||
Module.overloadRequire(); | ||
storeCache(); | ||
wipeCache(); | ||
onEnable(getAllMocks()); | ||
return mockModule; | ||
scope(); | ||
Module.overloadRequire(); | ||
storeCache(); | ||
wipeCache(); | ||
onEnable(getAllMocks()); | ||
return mockModule; | ||
}; | ||
@@ -131,8 +131,8 @@ | ||
mockModule.disable = function () { | ||
scope(); | ||
Module.restoreRequire(); | ||
onDisable(getAllMocks()); | ||
mockModule.withoutIsolation(); | ||
mockModule.flush(); | ||
return mockModule; | ||
scope(); | ||
Module.restoreRequire(); | ||
onDisable(getAllMocks()); | ||
mockModule.withoutIsolation(); | ||
mockModule.flush(); | ||
return mockModule; | ||
}; | ||
@@ -146,21 +146,21 @@ | ||
mockModule.proxy = function (file) { | ||
var overrides = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var overrides = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var result = 0; | ||
var stubs = typeof overrides === 'function' ? overrides(ModuleMock.inlineConstructor) : overrides; | ||
var result = 0; | ||
var stubs = typeof overrides === 'function' ? overrides(ModuleMock.inlineConstructor) : overrides; | ||
mockModule.inScope(function () { | ||
Object.keys(stubs).forEach(function (key) { | ||
return mockModule(key).from(stubs[key]); | ||
}); | ||
mockModule.inScope(function () { | ||
Object.keys(stubs).forEach(function (key) { | ||
return mockModule(key).from(stubs[key]); | ||
}); | ||
mockModule.enable(); | ||
if (typeof file === 'string') { | ||
result = mockModule.requireActual(file); | ||
} else { | ||
result = file(); | ||
} | ||
mockModule.disable(); | ||
}); | ||
return result; | ||
mockModule.enable(); | ||
if (typeof file === 'string') { | ||
result = mockModule.requireActual(file); | ||
} else { | ||
result = file(); | ||
} | ||
mockModule.disable(); | ||
}); | ||
return result; | ||
}; | ||
@@ -175,11 +175,11 @@ | ||
mockModule.module = function (importFunction) { | ||
var overrides = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var overrides = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var stubs = typeof overrides === 'function' ? overrides(ModuleMock.inlineConstructor) : overrides; | ||
var stubs = typeof overrides === 'function' ? overrides(ModuleMock.inlineConstructor) : overrides; | ||
return mockModule.around(importFunction, function () { | ||
return Object.keys(stubs).forEach(function (key) { | ||
return mockModule(key).from(stubs[key]); | ||
}); | ||
return mockModule.around(importFunction, function () { | ||
return Object.keys(stubs).forEach(function (key) { | ||
return mockModule(key).from(stubs[key]); | ||
}); | ||
}); | ||
}; | ||
@@ -192,14 +192,14 @@ | ||
mockModule.inScope = function (callback) { | ||
var currentScope = mockScope; | ||
var error = void 0; | ||
updateScope(currentScope); | ||
try { | ||
callback(); | ||
} catch (e) { | ||
error = e; | ||
} | ||
var currentScope = mockScope; | ||
var error = void 0; | ||
updateScope(currentScope); | ||
try { | ||
callback(); | ||
} catch (e) { | ||
error = e; | ||
} | ||
mockScope = currentScope; | ||
if (error) throw error; | ||
return mockModule; | ||
mockScope = currentScope; | ||
if (error) throw error; | ||
return mockModule; | ||
}; | ||
@@ -214,63 +214,68 @@ | ||
mockModule.around = function (loader, createCallback) { | ||
return new Promise(function (resolve, reject) { | ||
var currentScope = mockScope; | ||
updateScope(currentScope); | ||
return new Promise(function (resolve, reject) { | ||
var currentScope = mockScope; | ||
updateScope(currentScope); | ||
var restore = function restore() { | ||
mockModule.disable(); | ||
mockScope = currentScope; | ||
}; | ||
var restore = function restore() { | ||
mockModule.disable(); | ||
mockScope = currentScope; | ||
}; | ||
Promise.resolve(createCallback && createCallback(mockModule)).then(function () { | ||
return Module.probeAsyncModules(); | ||
}).then(function () { | ||
return mockModule.enable(); | ||
}).then(function () { | ||
return Promise.resolve(loader()).then(function (mockedResult) { | ||
restore(); | ||
resolve(mockedResult); | ||
}, function (err) { | ||
restore(); | ||
reject(err); | ||
}); | ||
}); | ||
Promise.resolve(createCallback && createCallback(mockModule)).then(function () { | ||
return Module.probeAsyncModules(); | ||
}).then(function () { | ||
return mockModule.enable(); | ||
}).then(function () { | ||
return Promise.resolve(loader()).then(function (mockedResult) { | ||
restore(); | ||
resolve(mockedResult); | ||
}, function (err) { | ||
restore(); | ||
reject(err); | ||
}); | ||
}); | ||
}); | ||
}; | ||
mockModule.stubFactory = function (factory) { | ||
var currentScope = mockScope; | ||
currentScope.options.stubFactory = factory; | ||
}; | ||
var storeCache = function storeCache() { | ||
mockScope.requireCache = mockScope.requireCache || Object.assign({}, require.cache); | ||
mockScope.requireCache = mockScope.requireCache || Object.assign({}, require.cache); | ||
}; | ||
var restoreCache = function restoreCache() { | ||
var oldCache = mockScope.requireCache; | ||
var newCache = require.cache; | ||
if (oldCache) { | ||
Object.keys(oldCache).filter(function (key) { | ||
return !newCache[key]; | ||
}).forEach(function (key) { | ||
return newCache[key] = oldCache[key]; | ||
}); | ||
var oldCache = mockScope.requireCache; | ||
var newCache = require.cache; | ||
if (oldCache) { | ||
Object.keys(oldCache).filter(function (key) { | ||
return !newCache[key]; | ||
}).forEach(function (key) { | ||
return newCache[key] = oldCache[key]; | ||
}); | ||
mockScope.requireCache = null; | ||
} | ||
mockScope.requireCache = null; | ||
} | ||
}; | ||
var swapCache = function swapCache() { | ||
var oldCache = mockScope.requireCache; | ||
var newCache = require.cache; | ||
if (oldCache) { | ||
Object.keys(newCache).filter(function (key) { | ||
return !oldCache[key]; | ||
}).filter(function (key) { | ||
return key.indexOf('\.node') < 0; | ||
}).forEach(function (key) { | ||
return delete newCache[key]; | ||
}); | ||
var oldCache = mockScope.requireCache; | ||
var newCache = require.cache; | ||
if (oldCache) { | ||
Object.keys(newCache).filter(function (key) { | ||
return !oldCache[key]; | ||
}).filter(function (key) { | ||
return key.indexOf('\.node') < 0; | ||
}).forEach(function (key) { | ||
return delete newCache[key]; | ||
}); | ||
Object.keys(oldCache).forEach(function (key) { | ||
return newCache[key] = oldCache[key]; | ||
}); | ||
Object.keys(oldCache).forEach(function (key) { | ||
return newCache[key] = oldCache[key]; | ||
}); | ||
mockScope.requireCache = null; | ||
} | ||
mockScope.requireCache = null; | ||
} | ||
}; | ||
@@ -281,15 +286,15 @@ /** | ||
mockModule.flush = function () { | ||
var forceCacheClear = getScopeVariable('forceCacheClear'); | ||
// flush away soiled modules | ||
wipeCache(mockScope.mockedModules); | ||
mockScope.mockedModules = {}; | ||
if (forceCacheClear) { | ||
if (forceCacheClear !== 'nocache') { | ||
// restore cache completely | ||
swapCache(); | ||
} | ||
} else { | ||
// merge caches | ||
restoreCache(); | ||
var forceCacheClear = getScopeVariable('forceCacheClear'); | ||
// flush away soiled modules | ||
wipeCache(mockScope.mockedModules); | ||
mockScope.mockedModules = {}; | ||
if (forceCacheClear) { | ||
if (forceCacheClear !== 'nocache') { | ||
// restore cache completely | ||
swapCache(); | ||
} | ||
} else { | ||
// merge caches | ||
restoreCache(); | ||
} | ||
}; | ||
@@ -302,3 +307,3 @@ | ||
mockModule.requireActual = function (fileName) { | ||
return Module.require(Module.relativeFileName(fileName, parentModule), parentModule); | ||
return Module.require(Module.relativeFileName(fileName, parentModule), parentModule); | ||
}; | ||
@@ -311,3 +316,3 @@ | ||
mockModule.importActual = function (fileName) { | ||
return Promise.resolve(_this.requireActual(fileName)); | ||
return Promise.resolve(_this.requireActual(fileName)); | ||
}; | ||
@@ -319,27 +324,27 @@ | ||
mockModule.clear = function () { | ||
updateScope(); | ||
scope(); | ||
mockModule.withoutIsolation(); | ||
mockModule.flush(); | ||
updateScope(); | ||
scope(); | ||
mockModule.withoutIsolation(); | ||
mockModule.flush(); | ||
}; | ||
var cleanup = function cleanup() { | ||
delete require.cache[require.resolve(__filename)]; | ||
delete require.cache[require.resolve(__filename)]; | ||
}; | ||
var addPlugin = function addPlugin(plugin) { | ||
scope(); | ||
addPluginAPI(plugin); | ||
scope(); | ||
addPluginAPI(plugin); | ||
}; | ||
var removePlugin = function removePlugin(plugin) { | ||
scope(); | ||
removePluginAPI(plugin); | ||
scope(); | ||
removePluginAPI(plugin); | ||
}; | ||
mockModule.addPlugin = function (plugin) { | ||
addPlugin(plugin); | ||
return mockModule; | ||
addPlugin(plugin); | ||
return mockModule; | ||
}; | ||
export { mockModule, addPlugin, removePlugin, cleanup }; |
@@ -10,2 +10,3 @@ import childOnly from './childOnly'; | ||
import disabledByDefault from './disabledByDefault'; | ||
import mockThoughByDefault from './mockThoughByDefault'; | ||
import usedByDefault from './usedByDefault'; | ||
@@ -23,2 +24,3 @@ import alwaysMatchOrigin from './toMatchOrigin'; | ||
disabledByDefault: disabledByDefault, | ||
mockThoughByDefault: mockThoughByDefault, | ||
alwaysMatchOrigin: alwaysMatchOrigin, | ||
@@ -25,0 +27,0 @@ usedByDefault: usedByDefault, |
@@ -11,5 +11,6 @@ var createScope = function createScope(parentScope, parentModule) { | ||
passBy: [], | ||
isolation: false, | ||
//isolation: false, | ||
plugins: [] | ||
plugins: [], | ||
options: {} | ||
}; | ||
@@ -16,0 +17,0 @@ }; |
@@ -38,2 +38,4 @@ 'use strict'; | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var matchOrigin = (0, _compareModuleExports2.default)('rewiremock'); | ||
@@ -110,5 +112,40 @@ var thisModule = module; | ||
function standardStubFactory(name, object, deeperMock) { | ||
if (typeof object === 'function') { | ||
return function () {}; | ||
} | ||
if ((typeof object === 'undefined' ? 'undefined' : _typeof(object)) === 'object') { | ||
return deeperMock(deeperMock); | ||
} | ||
return object; | ||
} | ||
function mockThought(stubFactory, mockOriginal) { | ||
var name = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ''; | ||
if (typeof mockOriginal === 'function') { | ||
return stubFactory(name || 'default', mockOriginal); | ||
} | ||
if ((typeof mockOriginal === 'undefined' ? 'undefined' : _typeof(mockOriginal)) === 'object') { | ||
var deeperMock = function deeperMock(key, value) { | ||
return mockThought(stubFactory, value, name ? name + '.' + key : key); | ||
}; | ||
if (Array.isArray(mockOriginal)) { | ||
return mockOriginal.map(function (x, i) { | ||
return deeperMock(i, x); | ||
}); | ||
} else { | ||
return Object.keys(mockOriginal).map(function (key) { | ||
return { key: key, value: deeperMock(key, mockOriginal[key]) }; | ||
}).reduce(function (acc, x) { | ||
return Object.assign(acc, _defineProperty({}, x.key, x.value)); | ||
}, {}); | ||
} | ||
} | ||
return mockOriginal; | ||
} | ||
function monkeyPatchPath(addr) { | ||
var path = addr.split('/'); | ||
if (path[0] == '..') { | ||
if (path[0] === '..') { | ||
path[0] = '.'; | ||
@@ -165,2 +202,8 @@ return path.join('/'); | ||
if (mock.mockThrough) { | ||
var factory = mock.mockThrough === true ? (0, _globals.getScopeOption)('stubFactory') : mock.mockThrough; | ||
mock.override = mockThought(factory || standardStubFactory, mock.original); | ||
return mockResult(request, mock, mock.override); | ||
} | ||
if (mock.overrideBy) { | ||
@@ -167,0 +210,0 @@ if (!mock.override) { |
@@ -27,2 +27,14 @@ "use strict"; | ||
var getScopeOption = exports.getScopeOption = function getScopeOption(name) { | ||
var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : currentScope; | ||
if (name in scope.options) { | ||
return scope.options[name]; | ||
} | ||
if (scope.parentScope) { | ||
return getScopeOption(name, scope.parentScope); | ||
} | ||
return undefined; | ||
}; | ||
var collectScopeVariable = exports.collectScopeVariable = function collectScopeVariable(name) { | ||
@@ -29,0 +41,0 @@ var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : currentScope; |
@@ -62,3 +62,3 @@ 'use strict'; | ||
* @name ModuleMock.mockThrough | ||
* @param {Function} [mockFactory=sinon.stub] - mock constructor | ||
* @param {Function} [mockFactory=() => emptyFunction] - mock constructor | ||
* @return {ModuleMock} | ||
@@ -65,0 +65,0 @@ */ |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
@@ -35,9 +35,9 @@ exports.cleanup = exports.removePlugin = exports.addPlugin = exports.mockModule = undefined; | ||
var scope = function scope() { | ||
return (0, _globals.setScope)(mockScope); | ||
return (0, _globals.setScope)(mockScope); | ||
}; | ||
var updateScope = function updateScope() { | ||
var parentScope = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; | ||
var parentScope = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; | ||
mockScope = (0, _scope2.default)(parentScope, parentModule); | ||
scope(); | ||
mockScope = (0, _scope2.default)(parentScope, parentModule); | ||
scope(); | ||
}; | ||
@@ -55,24 +55,24 @@ | ||
function mockModule(moduleName) { | ||
scope(); | ||
if (typeof moduleName === 'function') { | ||
return (0, _plugins.onMockCreate)(new _mock2.default((0, _mocks.getAsyncMock)(moduleName, parentModule))); | ||
} else { | ||
var name = (0, _plugins.convertName)(moduleName, parentModule); | ||
(0, _mocks.resetMock)(name); | ||
return (0, _plugins.onMockCreate)(new _mock2.default((0, _mocks.getMock)(name))); | ||
} | ||
scope(); | ||
if (typeof moduleName === 'function') { | ||
return (0, _plugins.onMockCreate)(new _mock2.default((0, _mocks.getAsyncMock)(moduleName, parentModule))); | ||
} else { | ||
var name = (0, _plugins.convertName)(moduleName, parentModule); | ||
(0, _mocks.resetMock)(name); | ||
return (0, _plugins.onMockCreate)(new _mock2.default((0, _mocks.getMock)(name))); | ||
} | ||
} | ||
mockModule.getMock = function (module) { | ||
var moduleName = module; | ||
if (typeof moduleName === 'function') { | ||
moduleName = (0, _mocks.getAsyncModuleName)(moduleName, parentModule); | ||
} else { | ||
moduleName = (0, _plugins.convertName)(moduleName, parentModule); | ||
} | ||
var mock = (0, _mocks.getMock)(moduleName); | ||
if (mock) { | ||
return new _mock2.default(mock); | ||
} | ||
return null; | ||
var moduleName = module; | ||
if (typeof moduleName === 'function') { | ||
moduleName = (0, _mocks.getAsyncModuleName)(moduleName, parentModule); | ||
} else { | ||
moduleName = (0, _plugins.convertName)(moduleName, parentModule); | ||
} | ||
var mock = (0, _mocks.getMock)(moduleName); | ||
if (mock) { | ||
return new _mock2.default(mock); | ||
} | ||
return null; | ||
}; | ||
@@ -86,4 +86,4 @@ | ||
mockModule.resolve = function (module) { | ||
scope(); | ||
return (0, _plugins.convertName)(module, parentModule); | ||
scope(); | ||
return (0, _plugins.convertName)(module, parentModule); | ||
}; | ||
@@ -99,6 +99,6 @@ | ||
mockModule.isolation = function () { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
mockScope.isolation = Object.assign({}, options); | ||
return mockModule; | ||
mockScope.isolation = Object.assign({}, options); | ||
return mockModule; | ||
}; | ||
@@ -110,8 +110,8 @@ | ||
mockModule.withoutIsolation = function () { | ||
mockScope.isolation = false; | ||
return mockModule; | ||
mockScope.isolation = false; | ||
return mockModule; | ||
}; | ||
mockModule.forceCacheClear = function (mode) { | ||
mockScope.forceCacheClear = mode ? mode : true; | ||
mockScope.forceCacheClear = mode ? mode : true; | ||
}; | ||
@@ -124,8 +124,8 @@ | ||
mockModule.passBy = function (pattern) { | ||
mockScope.passBy.push(pattern); | ||
return mockModule; | ||
mockScope.passBy.push(pattern); | ||
return mockModule; | ||
}; | ||
mockModule.overrideEntryPoint = function (parent) { | ||
mockScope.parentModule = parentModule = parent || (0, _module.getModuleParent)((0, _module.getModuleParent)(module)); | ||
mockScope.parentModule = parentModule = parent || (0, _module.getModuleParent)((0, _module.getModuleParent)(module)); | ||
}; | ||
@@ -139,8 +139,8 @@ | ||
mockModule.enable = function () { | ||
scope(); | ||
_module2.default.overloadRequire(); | ||
storeCache(); | ||
(0, _wipeCache2.default)(); | ||
(0, _plugins.onEnable)((0, _mocks.getAllMocks)()); | ||
return mockModule; | ||
scope(); | ||
_module2.default.overloadRequire(); | ||
storeCache(); | ||
(0, _wipeCache2.default)(); | ||
(0, _plugins.onEnable)((0, _mocks.getAllMocks)()); | ||
return mockModule; | ||
}; | ||
@@ -152,8 +152,8 @@ | ||
mockModule.disable = function () { | ||
scope(); | ||
_module2.default.restoreRequire(); | ||
(0, _plugins.onDisable)((0, _mocks.getAllMocks)()); | ||
mockModule.withoutIsolation(); | ||
mockModule.flush(); | ||
return mockModule; | ||
scope(); | ||
_module2.default.restoreRequire(); | ||
(0, _plugins.onDisable)((0, _mocks.getAllMocks)()); | ||
mockModule.withoutIsolation(); | ||
mockModule.flush(); | ||
return mockModule; | ||
}; | ||
@@ -167,21 +167,21 @@ | ||
mockModule.proxy = function (file) { | ||
var overrides = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var overrides = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var result = 0; | ||
var stubs = typeof overrides === 'function' ? overrides(_mock2.default.inlineConstructor) : overrides; | ||
var result = 0; | ||
var stubs = typeof overrides === 'function' ? overrides(_mock2.default.inlineConstructor) : overrides; | ||
mockModule.inScope(function () { | ||
Object.keys(stubs).forEach(function (key) { | ||
return mockModule(key).from(stubs[key]); | ||
}); | ||
mockModule.inScope(function () { | ||
Object.keys(stubs).forEach(function (key) { | ||
return mockModule(key).from(stubs[key]); | ||
}); | ||
mockModule.enable(); | ||
if (typeof file === 'string') { | ||
result = mockModule.requireActual(file); | ||
} else { | ||
result = file(); | ||
} | ||
mockModule.disable(); | ||
}); | ||
return result; | ||
mockModule.enable(); | ||
if (typeof file === 'string') { | ||
result = mockModule.requireActual(file); | ||
} else { | ||
result = file(); | ||
} | ||
mockModule.disable(); | ||
}); | ||
return result; | ||
}; | ||
@@ -196,11 +196,11 @@ | ||
mockModule.module = function (importFunction) { | ||
var overrides = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var overrides = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var stubs = typeof overrides === 'function' ? overrides(_mock2.default.inlineConstructor) : overrides; | ||
var stubs = typeof overrides === 'function' ? overrides(_mock2.default.inlineConstructor) : overrides; | ||
return mockModule.around(importFunction, function () { | ||
return Object.keys(stubs).forEach(function (key) { | ||
return mockModule(key).from(stubs[key]); | ||
}); | ||
return mockModule.around(importFunction, function () { | ||
return Object.keys(stubs).forEach(function (key) { | ||
return mockModule(key).from(stubs[key]); | ||
}); | ||
}); | ||
}; | ||
@@ -213,14 +213,14 @@ | ||
mockModule.inScope = function (callback) { | ||
var currentScope = mockScope; | ||
var error = void 0; | ||
updateScope(currentScope); | ||
try { | ||
callback(); | ||
} catch (e) { | ||
error = e; | ||
} | ||
var currentScope = mockScope; | ||
var error = void 0; | ||
updateScope(currentScope); | ||
try { | ||
callback(); | ||
} catch (e) { | ||
error = e; | ||
} | ||
mockScope = currentScope; | ||
if (error) throw error; | ||
return mockModule; | ||
mockScope = currentScope; | ||
if (error) throw error; | ||
return mockModule; | ||
}; | ||
@@ -235,63 +235,68 @@ | ||
mockModule.around = function (loader, createCallback) { | ||
return new Promise(function (resolve, reject) { | ||
var currentScope = mockScope; | ||
updateScope(currentScope); | ||
return new Promise(function (resolve, reject) { | ||
var currentScope = mockScope; | ||
updateScope(currentScope); | ||
var restore = function restore() { | ||
mockModule.disable(); | ||
mockScope = currentScope; | ||
}; | ||
var restore = function restore() { | ||
mockModule.disable(); | ||
mockScope = currentScope; | ||
}; | ||
Promise.resolve(createCallback && createCallback(mockModule)).then(function () { | ||
return _module2.default.probeAsyncModules(); | ||
}).then(function () { | ||
return mockModule.enable(); | ||
}).then(function () { | ||
return Promise.resolve(loader()).then(function (mockedResult) { | ||
restore(); | ||
resolve(mockedResult); | ||
}, function (err) { | ||
restore(); | ||
reject(err); | ||
}); | ||
}); | ||
Promise.resolve(createCallback && createCallback(mockModule)).then(function () { | ||
return _module2.default.probeAsyncModules(); | ||
}).then(function () { | ||
return mockModule.enable(); | ||
}).then(function () { | ||
return Promise.resolve(loader()).then(function (mockedResult) { | ||
restore(); | ||
resolve(mockedResult); | ||
}, function (err) { | ||
restore(); | ||
reject(err); | ||
}); | ||
}); | ||
}); | ||
}; | ||
mockModule.stubFactory = function (factory) { | ||
var currentScope = mockScope; | ||
currentScope.options.stubFactory = factory; | ||
}; | ||
var storeCache = function storeCache() { | ||
mockScope.requireCache = mockScope.requireCache || Object.assign({}, require.cache); | ||
mockScope.requireCache = mockScope.requireCache || Object.assign({}, require.cache); | ||
}; | ||
var restoreCache = function restoreCache() { | ||
var oldCache = mockScope.requireCache; | ||
var newCache = require.cache; | ||
if (oldCache) { | ||
Object.keys(oldCache).filter(function (key) { | ||
return !newCache[key]; | ||
}).forEach(function (key) { | ||
return newCache[key] = oldCache[key]; | ||
}); | ||
var oldCache = mockScope.requireCache; | ||
var newCache = require.cache; | ||
if (oldCache) { | ||
Object.keys(oldCache).filter(function (key) { | ||
return !newCache[key]; | ||
}).forEach(function (key) { | ||
return newCache[key] = oldCache[key]; | ||
}); | ||
mockScope.requireCache = null; | ||
} | ||
mockScope.requireCache = null; | ||
} | ||
}; | ||
var swapCache = function swapCache() { | ||
var oldCache = mockScope.requireCache; | ||
var newCache = require.cache; | ||
if (oldCache) { | ||
Object.keys(newCache).filter(function (key) { | ||
return !oldCache[key]; | ||
}).filter(function (key) { | ||
return key.indexOf('\.node') < 0; | ||
}).forEach(function (key) { | ||
return delete newCache[key]; | ||
}); | ||
var oldCache = mockScope.requireCache; | ||
var newCache = require.cache; | ||
if (oldCache) { | ||
Object.keys(newCache).filter(function (key) { | ||
return !oldCache[key]; | ||
}).filter(function (key) { | ||
return key.indexOf('\.node') < 0; | ||
}).forEach(function (key) { | ||
return delete newCache[key]; | ||
}); | ||
Object.keys(oldCache).forEach(function (key) { | ||
return newCache[key] = oldCache[key]; | ||
}); | ||
Object.keys(oldCache).forEach(function (key) { | ||
return newCache[key] = oldCache[key]; | ||
}); | ||
mockScope.requireCache = null; | ||
} | ||
mockScope.requireCache = null; | ||
} | ||
}; | ||
@@ -302,15 +307,15 @@ /** | ||
mockModule.flush = function () { | ||
var forceCacheClear = (0, _globals.getScopeVariable)('forceCacheClear'); | ||
// flush away soiled modules | ||
(0, _wipeCache2.default)(mockScope.mockedModules); | ||
mockScope.mockedModules = {}; | ||
if (forceCacheClear) { | ||
if (forceCacheClear !== 'nocache') { | ||
// restore cache completely | ||
swapCache(); | ||
} | ||
} else { | ||
// merge caches | ||
restoreCache(); | ||
var forceCacheClear = (0, _globals.getScopeVariable)('forceCacheClear'); | ||
// flush away soiled modules | ||
(0, _wipeCache2.default)(mockScope.mockedModules); | ||
mockScope.mockedModules = {}; | ||
if (forceCacheClear) { | ||
if (forceCacheClear !== 'nocache') { | ||
// restore cache completely | ||
swapCache(); | ||
} | ||
} else { | ||
// merge caches | ||
restoreCache(); | ||
} | ||
}; | ||
@@ -323,3 +328,3 @@ | ||
mockModule.requireActual = function (fileName) { | ||
return _module2.default.require(_module2.default.relativeFileName(fileName, parentModule), parentModule); | ||
return _module2.default.require(_module2.default.relativeFileName(fileName, parentModule), parentModule); | ||
}; | ||
@@ -332,3 +337,3 @@ | ||
mockModule.importActual = function (fileName) { | ||
return Promise.resolve(undefined.requireActual(fileName)); | ||
return Promise.resolve(undefined.requireActual(fileName)); | ||
}; | ||
@@ -340,25 +345,25 @@ | ||
mockModule.clear = function () { | ||
updateScope(); | ||
scope(); | ||
mockModule.withoutIsolation(); | ||
mockModule.flush(); | ||
updateScope(); | ||
scope(); | ||
mockModule.withoutIsolation(); | ||
mockModule.flush(); | ||
}; | ||
var cleanup = function cleanup() { | ||
delete require.cache[require.resolve(__filename)]; | ||
delete require.cache[require.resolve(__filename)]; | ||
}; | ||
var addPlugin = function addPlugin(plugin) { | ||
scope(); | ||
(0, _plugins.addPlugin)(plugin); | ||
scope(); | ||
(0, _plugins.addPlugin)(plugin); | ||
}; | ||
var removePlugin = function removePlugin(plugin) { | ||
scope(); | ||
(0, _plugins.removePlugin)(plugin); | ||
scope(); | ||
(0, _plugins.removePlugin)(plugin); | ||
}; | ||
mockModule.addPlugin = function (plugin) { | ||
addPlugin(plugin); | ||
return mockModule; | ||
addPlugin(plugin); | ||
return mockModule; | ||
}; | ||
@@ -365,0 +370,0 @@ |
@@ -39,2 +39,6 @@ 'use strict'; | ||
var _mockThoughByDefault = require('./mockThoughByDefault'); | ||
var _mockThoughByDefault2 = _interopRequireDefault(_mockThoughByDefault); | ||
var _usedByDefault = require('./usedByDefault'); | ||
@@ -59,2 +63,3 @@ | ||
disabledByDefault: _disabledByDefault2.default, | ||
mockThoughByDefault: _mockThoughByDefault2.default, | ||
alwaysMatchOrigin: _toMatchOrigin2.default, | ||
@@ -61,0 +66,0 @@ usedByDefault: _usedByDefault2.default, |
@@ -16,5 +16,6 @@ "use strict"; | ||
passBy: [], | ||
isolation: false, | ||
//isolation: false, | ||
plugins: [] | ||
plugins: [], | ||
options: {} | ||
}; | ||
@@ -21,0 +22,0 @@ }; |
{ | ||
"name": "rewiremock", | ||
"version": "3.4.3", | ||
"version": "3.5.0", | ||
"description": "Advanced dependency mocking device.", | ||
@@ -98,2 +98,3 @@ "main": "lib/index.js", | ||
"phantomjs-polyfill-object-assign": "0.0.2", | ||
"sinon": "^4.4.2", | ||
"webpack": "^3.5.5", | ||
@@ -100,0 +101,0 @@ "webpack-dev-server": "^2.7.1" |
@@ -47,6 +47,7 @@ # rewiremock [![Build Status](https://secure.travis-ci.org/theKashey/rewiremock.svg)](http://travis-ci.org/theKashey/rewiremock) | ||
- .by(otherModule: string| function) - overload by another module(if string provider) or by result of a function call. | ||
- .callThrough() - first load original module, and next extend it by provided stub. | ||
- .callThrough() - first load the original module, and next extend it by provided stub. | ||
- .mockThrough() - first load the original module, and then replaces all exports by stubs. | ||
- .toBeUsed() - enables usage checking. | ||
- .directChildOnly - will do mock only direct dependencies. | ||
- .calledFromMock - will do mock only dependencies of mocked dependencies. | ||
- .calledFromMock - will do mock only dependencies of mocked dependencies. | ||
- rewiremock(moduleName: string|loader) - returns existing mock | ||
@@ -59,2 +60,4 @@ ## isolation API | ||
- rewiremock.inScope(callback) - place synchronous callback inside a sandbox. | ||
## helper functions | ||
- rewuremock.stubFactory(factory) - define a stub factory for mockThrough command. | ||
@@ -73,4 +76,6 @@ # Which one? | ||
# Usage | ||
- `proxy` will load a file by it's own ( keep in mind - name resolution is a complex thing) | ||
```js | ||
// 1. proxy will load a file by it's own ( keep in mind - name resolution is a complex thing) | ||
const mock = rewiremock.proxy('somemodule', (r) => ({ | ||
@@ -80,4 +85,5 @@ 'dep1': { name: 'override' }, | ||
})); | ||
// 2. you can require a file by yourself. ( yep, proxy is a god function) | ||
``` | ||
- you can require a file by yourself. ( yep, proxy is a "god" function) | ||
```js | ||
const mock = rewiremock.proxy(() => require('somemodule'), { | ||
@@ -87,5 +93,6 @@ 'dep1': { name: 'override' }, | ||
})); | ||
// 3. or use es6 import (not for node.js mjs `real` es6 modules) | ||
// PS: module is an async version of proxy, so you can use imports | ||
``` | ||
- or use es6 `import` (not for node.js mjs `real` es6 modules) | ||
`module` is an async version of proxy, so you can use imports | ||
```js | ||
const mock = await rewiremock.module(() => import('somemodule'), { | ||
@@ -95,15 +102,25 @@ 'dep1': { name: 'override' }, | ||
})); | ||
// 3. another version of .module, where you can do just ~anything~. | ||
``` | ||
- `around` - another version of .module, where you can do just ~anything~. | ||
```js | ||
const mock = await rewiremock.around(() => import('somemodule'), () => { | ||
rewiremock('dep1').with('something'); | ||
callMom(); | ||
// prepare mocking behavior | ||
})); | ||
// 4. Low level API | ||
``` | ||
- `enable`/`disable` - Low level API | ||
```js | ||
rewiremock('someThing').with('someThingElse') | ||
rewiremock.enable(); | ||
// require something | ||
rewiremock.disable(); | ||
``` | ||
In all the cases you can specify what exactly you want to mock, or just mock anything | ||
```js | ||
addPlugin(plugins.mockThoughByDefault); | ||
``` | ||
# Type safety | ||
@@ -357,2 +374,3 @@ Rewiremock is able to provide a type-safe mocks. To enable type-safety follow these steps: | ||
- disabledByDefault. All mocks will be disabled on create and at the end of each cycle. | ||
- mockThoughByDefault. All mocks mocked though. | ||
- usedByDefault. All mocks to be used by fact (reverse isolation) | ||
@@ -359,0 +377,0 @@ ```javascript |
declare module 'rewiremock' { | ||
type Plugin = any; | ||
type PluginNames = 'childOnly' | 'nodejs' | 'protectNodeModules' | 'relative' | 'webpackAlias' | 'toBeUsed' | 'disabledByDefault' | 'usedByDefault' | 'alwaysMatchOrigin' | 'directChild'; | ||
type PluginNames = 'childOnly' | 'nodejs' | 'protectNodeModules' | 'relative' | 'webpackAlias' | 'toBeUsed' | 'disabledByDefault' | 'mockThoughByDefault' | 'usedByDefault' | 'alwaysMatchOrigin' | 'directChild'; | ||
type Plugins = { | ||
@@ -17,5 +17,9 @@ [Key in PluginNames]: any | ||
type IStubFactory = (name: string, value: any) => any; | ||
interface BaseMock { | ||
/** | ||
* Enabled call thought original module | ||
* Enabled call thought original module, making all the original methods accessible. | ||
* @example | ||
* mock.callThrough(); | ||
*/ | ||
@@ -25,4 +29,14 @@ callThrough(): this, | ||
/** | ||
* Setting es6 bahavior for a module | ||
* Mimic the original file, replacing all the original methods by mocks. | ||
* @param {IStubFactory} [stubFactory] - stub factory function | ||
* @example | ||
* mock.mockThrough(); | ||
* mock.mockThrough( () => sinon.stub() ); | ||
* mock.mockThrough( (name, value) => typeof value === 'function' ? sinon.stub() : value ); | ||
*/ | ||
mockThrough(stubFactory?: IStubFactory): this, | ||
/** | ||
* Setting es6 behaviour for a module | ||
*/ | ||
es6(): this, | ||
@@ -32,2 +46,4 @@ | ||
* Overriding export of one module by another | ||
* @example | ||
* mock.by('otherModuleName'); | ||
*/ | ||
@@ -38,2 +54,4 @@ by(module: string): BaseMock, | ||
* Overriding export of one module by something generated by a function | ||
* @example | ||
* mock.by( originalModule => cache || cache = originalModule.requireActual('./nestedDep')); | ||
*/ | ||
@@ -47,6 +65,9 @@ by(module: (module: OverloadedModule) => Object): BaseMock, | ||
/** | ||
* mocks only first direct children. | ||
* will mock this only first (directly nested) children. | ||
*/ | ||
directChildOnly(): this; | ||
/** | ||
* will mocks this regardless of position | ||
*/ | ||
atAnyPlace(): this; | ||
@@ -66,3 +87,3 @@ | ||
noToBeUsed(): this, | ||
notToBeUsed(): this, | ||
@@ -141,3 +162,3 @@ /** | ||
/** | ||
* executes module in a sanbox | ||
* executes module in a sandbox | ||
* @param {Function} loader - loader of target module. You can use import or require. May return a Promise | ||
@@ -169,2 +190,8 @@ * @param {Function} [creator] - mock creator. You may add any mocks inside. | ||
/** | ||
* Define stub factory for mockThrough command | ||
* @param {IStubFactory} stubFactory | ||
*/ | ||
stubFactory(stubFactory: IStubFactory): void; | ||
/** | ||
* converts module name | ||
@@ -171,0 +198,0 @@ * @param module |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
150427
65
3250
543
26