rewiremock
Advanced tools
Comparing version 1.3.1 to 1.3.2
@@ -78,2 +78,4 @@ 'use strict'; | ||
mockedModules[parent.filename] = true; | ||
mock.usedAs = mock.usedAs || []; | ||
mock.usedAs.push(baseRequest); | ||
@@ -98,2 +100,4 @@ mockedModules[baseRequest] = true; | ||
return mockResult(request, mock.value); | ||
} else { | ||
// why you shouldn't? | ||
} | ||
@@ -100,0 +104,0 @@ } |
@@ -111,2 +111,3 @@ 'use strict'; | ||
(0, _wipeCache2.default)(); | ||
(0, _plugins.onEnable)((0, _mocks.getAllMocks)()); | ||
}; | ||
@@ -193,2 +194,4 @@ | ||
mockModule.addPlugin = addPlugin; | ||
exports.mockModule = mockModule; | ||
@@ -195,0 +198,0 @@ exports.addPlugin = addPlugin; |
@@ -14,2 +14,4 @@ 'use strict'; | ||
var _common = require('./_common'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -28,2 +30,11 @@ | ||
var pickFrom = function pickFrom(mocks, name) { | ||
var ext = _common.extensions.find(function (ext) { | ||
return mocks[name + ext]; | ||
}); | ||
if (ext !== undefined) { | ||
return mocks[name + ext]; | ||
} | ||
}; | ||
var getMock = function getMock(name) { | ||
@@ -35,3 +46,5 @@ var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : (0, _globals2.default)(); | ||
var shortName = (0, _path.join)(fn.dir, fn.name); | ||
var mock = mocks[name] || mocks[shortName]; | ||
var mock = pickFrom(mocks, name) || pickFrom(mocks, shortName); | ||
if (!mock && scope.parentScope) { | ||
@@ -38,0 +51,0 @@ return getMock(name, scope.parentScope); |
@@ -6,3 +6,3 @@ 'use strict'; | ||
}); | ||
exports._clearPlugins = exports.removePlugin = exports.addPlugin = exports.onDisable = exports.onMockCreate = exports.shouldMock = exports.shouldWipe = exports.convertName = undefined; | ||
exports._clearPlugins = exports.removePlugin = exports.addPlugin = exports.onEnable = exports.onDisable = exports.onMockCreate = exports.shouldMock = exports.shouldWipe = exports.convertName = undefined; | ||
@@ -81,2 +81,12 @@ var _globals = require('./globals'); | ||
var onEnable = function onEnable(mocks) { | ||
var plugs = plugins(); | ||
Object.keys(mocks).forEach(function (mockName) { | ||
var mock = mocks[mockName]; | ||
plugs.forEach(function (plugin) { | ||
return plugin.onEnable && plugin.onEnable(mock._parent); | ||
}); | ||
}); | ||
}; | ||
var addPlugin = function addPlugin(plugin) { | ||
@@ -101,4 +111,5 @@ (0, _globals2.default)().plugins.push(plugin); | ||
exports.onDisable = onDisable; | ||
exports.onEnable = onEnable; | ||
exports.addPlugin = addPlugin; | ||
exports.removePlugin = removePlugin; | ||
exports._clearPlugins = _clearPlugins; |
@@ -17,6 +17,14 @@ 'use strict'; | ||
var onDisable = function onDisable(mock) { | ||
var name = mock.mock.name; | ||
if (mock.flag_toBeUsed && !(0, _globals2.default)().mockedModules[name]) { | ||
throw new Error(name + ' set toBeUsed, but was unused'); | ||
var onEnable = function onEnable(_ref) { | ||
var mock = _ref.mock; | ||
mock.usedAs = undefined; | ||
}; | ||
var onDisable = function onDisable(_ref2) { | ||
var mock = _ref2.mock; | ||
var name = mock.name; | ||
if (mock.flag_toBeUsed && !mock.usedAs) { | ||
throw new Error(name + ' is set toBeUsed, but was unused'); | ||
} | ||
@@ -26,5 +34,6 @@ }; | ||
var plugin = (0, _common2.default)({ | ||
onDisable: onDisable | ||
onDisable: onDisable, | ||
onEnable: onEnable | ||
}); | ||
exports.default = plugin; |
{ | ||
"name": "rewiremock", | ||
"version": "1.3.1", | ||
"version": "1.3.2", | ||
"description": "Easy and es6 compatible mocking tool", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -59,2 +59,4 @@ import Module from 'module'; | ||
mockedModules[parent.filename] = true; | ||
mock.usedAs = (mock.usedAs || []); | ||
mock.usedAs.push(baseRequest); | ||
@@ -81,2 +83,4 @@ mockedModules[baseRequest] = true; | ||
return mockResult(request, mock.value); | ||
} else { | ||
// why you shouldn't? | ||
} | ||
@@ -83,0 +87,0 @@ } |
@@ -10,2 +10,3 @@ import Module from 'module'; | ||
onDisable, | ||
onEnable, | ||
addPlugin as addPluginAPI, | ||
@@ -89,2 +90,3 @@ removePlugin as removePluginAPI | ||
wipeCache(); | ||
onEnable(getAllMocks()); | ||
}; | ||
@@ -170,2 +172,4 @@ | ||
mockModule.addPlugin = addPlugin; | ||
export { | ||
@@ -172,0 +176,0 @@ mockModule, |
import {parse, join} from 'path'; | ||
import getScope from './globals'; | ||
import {extensions} from './_common'; | ||
@@ -13,2 +14,9 @@ const genMock = (name) => { | ||
const pickFrom = (mocks, name) => { | ||
const ext = extensions.find(ext => mocks[name + ext]); | ||
if(ext!==undefined){ | ||
return mocks[name + ext] | ||
} | ||
}; | ||
const getMock = (name, scope = getScope()) => { | ||
@@ -18,3 +26,5 @@ const {mocks} = scope; | ||
const shortName = join(fn.dir, fn.name); | ||
const mock = mocks[name] || mocks[shortName]; | ||
const mock = pickFrom(mocks, name) || pickFrom(mocks, shortName); | ||
if (!mock && scope.parentScope) { | ||
@@ -33,3 +43,3 @@ return getMock(name, scope.parentScope); | ||
const mocks = scope.mocks; | ||
Object.keys(scope.mocks).forEach(key => result[key]=mocks[key]); | ||
Object.keys(scope.mocks).forEach(key => result[key] = mocks[key]); | ||
}; | ||
@@ -36,0 +46,0 @@ collect(getScope()); |
@@ -70,2 +70,10 @@ import getScope from './globals'; | ||
const onEnable = (mocks) => { | ||
const plugs = plugins(); | ||
Object.keys(mocks).forEach(mockName => { | ||
const mock = mocks[mockName]; | ||
plugs.forEach(plugin => plugin.onEnable && plugin.onEnable(mock._parent)) | ||
}); | ||
}; | ||
const addPlugin = (plugin) => { | ||
@@ -86,3 +94,5 @@ getScope().plugins.push(plugin); | ||
onMockCreate, | ||
onDisable, | ||
onEnable, | ||
@@ -89,0 +99,0 @@ addPlugin, |
import getScope from '../globals'; | ||
import createPlugin from './_common'; | ||
const onDisable = (mock) => { | ||
const name = mock.mock.name; | ||
if (mock.flag_toBeUsed && !getScope().mockedModules[name]) { | ||
throw new Error(name + ' set toBeUsed, but was unused') | ||
const onEnable = ({mock}) => { | ||
mock.usedAs = undefined; | ||
}; | ||
const onDisable = ({mock}) => { | ||
const name = mock.name; | ||
if (mock.flag_toBeUsed && !mock.usedAs) { | ||
throw new Error(name + ' is set toBeUsed, but was unused') | ||
} | ||
@@ -12,5 +17,6 @@ }; | ||
const plugin = createPlugin({ | ||
onDisable | ||
onDisable, | ||
onEnable | ||
}); | ||
export default plugin; |
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
107714
83
2680