rewiremock
Advanced tools
Comparing version 3.1.1 to 3.2.0
@@ -11,3 +11,5 @@ 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; }; | ||
import ModuleLoader from './getModule'; | ||
import matchOriginFabric from 'compare-module-exports'; | ||
var matchOrigin = matchOriginFabric('rewiremock'); | ||
var thisModule = module; | ||
@@ -67,3 +69,6 @@ | ||
function mockResult(name, data) { | ||
function mockResult(name, mock, data) { | ||
if (mock.matchOrigin) { | ||
matchOrigin(mock.original, data, name, '%mock%', { noFunctionCompare: true }); | ||
} | ||
if (data && !data.default) { | ||
@@ -123,3 +128,3 @@ data.default = data; | ||
if (mock.allowCallThrough) { | ||
if (mock.allowCallThrough || mock.matchOrigin) { | ||
if (!mock.original) { | ||
@@ -146,3 +151,3 @@ mock.original = originalLoader(request, parent, isMain); | ||
} | ||
return mockResult(request, mock.override); | ||
return mockResult(request, mock, mock.override); | ||
} | ||
@@ -153,3 +158,3 @@ | ||
if (_typeof(mock.value) === 'object' && Object.keys(mock.value).length === 0) { | ||
return mockResult(request, mock.original); | ||
return mockResult(request, mock, mock.original); | ||
} else { | ||
@@ -159,5 +164,5 @@ throw new Error('rewiremock: trying to merge Functional base with callThrough mock at ' + request + '. Use overrideBy instead.'); | ||
} | ||
return mockResult(request, Object.assign({}, mock.original, mock.value, { __esModule: mock.original.__esModule })); | ||
return mockResult(request, mock, Object.assign({}, mock.original, mock.value, { __esModule: mock.original.__esModule })); | ||
} | ||
return mockResult(request, mock.value); | ||
return mockResult(request, mock, mock.value); | ||
} else { | ||
@@ -164,0 +169,0 @@ // why you shouldn't? |
@@ -152,2 +152,8 @@ 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; }; | ||
}, { | ||
key: 'toMatchOrigin', | ||
value: function toMatchOrigin() { | ||
this.mock.matchOrigin = true; | ||
return this; | ||
} | ||
}, { | ||
key: 'notToBeUsed', | ||
@@ -154,0 +160,0 @@ value: function notToBeUsed() { |
@@ -8,3 +8,3 @@ var _this = this; | ||
import { convertName, onMockCreate, onDisable, onEnable, addPlugin as addPluginAPI, removePlugin as removePluginAPI } from './plugins'; | ||
import { resetMock, getMock, getAsyncMock, getAllMocks } from './mocks'; | ||
import { resetMock, getMock, getAsyncMock, getAsyncModuleName, getAllMocks } from './mocks'; | ||
import ModuleMock from './mock'; | ||
@@ -36,3 +36,3 @@ | ||
if (typeof moduleName === 'function') { | ||
return onMockCreate(new ModuleMock(getAsyncMock(moduleName))); | ||
return onMockCreate(new ModuleMock(getAsyncMock(moduleName, parentModule))); | ||
} else { | ||
@@ -45,2 +45,16 @@ var name = convertName(moduleName, parentModule); | ||
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; | ||
}; | ||
/** | ||
@@ -47,0 +61,0 @@ * @name rewiremock.resolve |
@@ -5,2 +5,3 @@ import { join } from 'path'; | ||
import { extensions } from './_common'; | ||
import { getModuleName } from "./module"; | ||
@@ -46,6 +47,10 @@ var genMock = function genMock(name) { | ||
var getAsyncMock = function getAsyncMock(creator) { | ||
var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : getScope(); | ||
export var getAsyncModuleName = function getAsyncModuleName(creator, parent) { | ||
return creator.toString() + ':' + getModuleName(parent); | ||
}; | ||
var signature = creator.toString(); | ||
var getAsyncMock = function getAsyncMock(creator, parent) { | ||
var scope = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : getScope(); | ||
var signature = getAsyncModuleName(creator, parent); | ||
var mock = resetMock(signature); | ||
@@ -52,0 +57,0 @@ scope.asyncMocks.push({ |
@@ -11,2 +11,3 @@ import childOnly from './childOnly'; | ||
import usedByDefault from './usedByDefault'; | ||
import alwaysMatchOrigin from './toMatchOrigin'; | ||
@@ -22,2 +23,3 @@ var exports = { | ||
disabledByDefault: disabledByDefault, | ||
alwaysMatchOrigin: alwaysMatchOrigin, | ||
usedByDefault: usedByDefault, | ||
@@ -24,0 +26,0 @@ directChild: directChild |
@@ -32,4 +32,9 @@ 'use strict'; | ||
var _compareModuleExports = require('compare-module-exports'); | ||
var _compareModuleExports2 = _interopRequireDefault(_compareModuleExports); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var matchOrigin = (0, _compareModuleExports2.default)('rewiremock'); | ||
var thisModule = module; | ||
@@ -89,3 +94,6 @@ | ||
function mockResult(name, data) { | ||
function mockResult(name, mock, data) { | ||
if (mock.matchOrigin) { | ||
matchOrigin(mock.original, data, name, '%mock%', { noFunctionCompare: true }); | ||
} | ||
if (data && !data.default) { | ||
@@ -145,3 +153,3 @@ data.default = data; | ||
if (mock.allowCallThrough) { | ||
if (mock.allowCallThrough || mock.matchOrigin) { | ||
if (!mock.original) { | ||
@@ -168,3 +176,3 @@ mock.original = (0, _module.originalLoader)(request, parent, isMain); | ||
} | ||
return mockResult(request, mock.override); | ||
return mockResult(request, mock, mock.override); | ||
} | ||
@@ -175,3 +183,3 @@ | ||
if (_typeof(mock.value) === 'object' && Object.keys(mock.value).length === 0) { | ||
return mockResult(request, mock.original); | ||
return mockResult(request, mock, mock.original); | ||
} else { | ||
@@ -181,5 +189,5 @@ throw new Error('rewiremock: trying to merge Functional base with callThrough mock at ' + request + '. Use overrideBy instead.'); | ||
} | ||
return mockResult(request, Object.assign({}, mock.original, mock.value, { __esModule: mock.original.__esModule })); | ||
return mockResult(request, mock, Object.assign({}, mock.original, mock.value, { __esModule: mock.original.__esModule })); | ||
} | ||
return mockResult(request, mock.value); | ||
return mockResult(request, mock, mock.value); | ||
} else { | ||
@@ -186,0 +194,0 @@ // why you shouldn't? |
@@ -163,2 +163,8 @@ 'use strict'; | ||
}, { | ||
key: 'toMatchOrigin', | ||
value: function toMatchOrigin() { | ||
this.mock.matchOrigin = true; | ||
return this; | ||
} | ||
}, { | ||
key: 'notToBeUsed', | ||
@@ -165,0 +171,0 @@ value: function notToBeUsed() { |
@@ -56,3 +56,3 @@ 'use strict'; | ||
if (typeof moduleName === 'function') { | ||
return (0, _plugins.onMockCreate)(new _mock2.default((0, _mocks.getAsyncMock)(moduleName))); | ||
return (0, _plugins.onMockCreate)(new _mock2.default((0, _mocks.getAsyncMock)(moduleName, parentModule))); | ||
} else { | ||
@@ -65,2 +65,16 @@ var name = (0, _plugins.convertName)(moduleName, parentModule); | ||
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; | ||
}; | ||
/** | ||
@@ -67,0 +81,0 @@ * @name rewiremock.resolve |
@@ -6,3 +6,3 @@ 'use strict'; | ||
}); | ||
exports.resetMock = exports.getAllMocks = exports.getAllAsyncMocks = exports.getAsyncMock = exports.getMock = exports.insertMock = undefined; | ||
exports.resetMock = exports.getAllMocks = exports.getAllAsyncMocks = exports.getAsyncMock = exports.getMock = exports.insertMock = exports.getAsyncModuleName = undefined; | ||
@@ -21,2 +21,4 @@ var _path = require('path'); | ||
var _module = require('./module'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -63,6 +65,10 @@ | ||
var getAsyncMock = function getAsyncMock(creator) { | ||
var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : (0, _globals2.default)(); | ||
var getAsyncModuleName = exports.getAsyncModuleName = function getAsyncModuleName(creator, parent) { | ||
return creator.toString() + ':' + (0, _module.getModuleName)(parent); | ||
}; | ||
var signature = creator.toString(); | ||
var getAsyncMock = function getAsyncMock(creator, parent) { | ||
var scope = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : (0, _globals2.default)(); | ||
var signature = getAsyncModuleName(creator, parent); | ||
var mock = resetMock(signature); | ||
@@ -69,0 +75,0 @@ scope.asyncMocks.push({ |
@@ -43,2 +43,6 @@ 'use strict'; | ||
var _toMatchOrigin = require('./toMatchOrigin'); | ||
var _toMatchOrigin2 = _interopRequireDefault(_toMatchOrigin); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -55,2 +59,3 @@ | ||
disabledByDefault: _disabledByDefault2.default, | ||
alwaysMatchOrigin: _toMatchOrigin2.default, | ||
usedByDefault: _usedByDefault2.default, | ||
@@ -57,0 +62,0 @@ directChild: _directChild2.default |
{ | ||
"name": "rewiremock", | ||
"version": "3.1.1", | ||
"version": "3.2.0", | ||
"description": "Easy and es6 compatible dependency mocking tool.", | ||
@@ -57,2 +57,3 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"compare-module-exports": "^1.0.4", | ||
"lodash.some": "^4.6.0", | ||
@@ -59,0 +60,0 @@ "lodash.template": "^4.4.0", |
@@ -51,2 +51,3 @@ # rewiremock [![Build Status](https://secure.travis-ci.org/theKashey/rewiremock.svg)](http://travis-ci.org/theKashey/rewiremock) | ||
- .calledFromMock - will do mock only dependencies of mocked dependencies. | ||
- rewiremock(moduleName: string|loader) - returns existing mock | ||
## isolation API | ||
@@ -108,3 +109,3 @@ - rewiremock.isolation() - enables isolation | ||
3. Use rewiremock.around or rewiremock.module to perform a mock. | ||
4. Use async form of rewiremock mock declaration. | ||
4. Use async form of rewiremock mock declaration. | ||
@@ -145,4 +146,22 @@ ```js | ||
``` | ||
# Type safety for JavaScript | ||
Rewiremock can check mock against the real implementation. This does not perform `type checking`, but | ||
could check exported names and exported types (function vs number, for example). | ||
Rewiremock expects that mock will be __less or equal__ than original file. | ||
```text | ||
rewiremock: mocked export "somethingMissing" does not exists in ./b.js | ||
rewiremock: exported type mismatch: ./b.js:default. Expected function, got number | ||
``` | ||
To activate exports comparison | ||
```js | ||
rewiremock('somemoduname') | ||
.toMatchOrigin(); // to activate | ||
// or | ||
import rewiremock, { addPlugin, removePlugin, plugins } from 'rewiremock'; | ||
addPlugin(plugins.alwaysMatchOrigin); | ||
``` | ||
# Setup | ||
@@ -359,3 +378,4 @@ | ||
# Reverse isolation. | ||
# Reverse isolation | ||
Sometimes you have to be sure, that you mock is actually was called. | ||
@@ -362,0 +382,0 @@ Isolation will protect you then you add new dependencies, `.toBeUsed` protect you from removal. |
declare module 'rewiremock' { | ||
type Plugin = any; | ||
type PluginNames = 'childOnly' | 'nodejs' | 'protectNodeModules' | 'relative' | 'webpackAlias' | 'toBeUsed' | 'disabledByDefault' | 'usedByDefault' | 'directChild'; | ||
type PluginNames = 'childOnly' | 'nodejs' | 'protectNodeModules' | 'relative' | 'webpackAlias' | 'toBeUsed' | 'disabledByDefault' | 'usedByDefault' | 'alwaysMatchOrigin' | 'directChild'; | ||
type Plugins = { | ||
@@ -61,3 +61,9 @@ [Key in PluginNames]: any | ||
noToBeUsed(): this | ||
noToBeUsed(): this, | ||
/** | ||
* checks mocks agains implementation | ||
* @return {this} | ||
*/ | ||
toMatchOrigin(): this | ||
} | ||
@@ -117,2 +123,10 @@ | ||
/** | ||
* returns existing mock | ||
* @return {"rewiremock".ModuleMock} | ||
*/ | ||
getMock(module: string): ModuleMock; | ||
getMock<T extends HasDefault>(module: ImportFunction<T>): DefaultModuleMock<T> | ||
getMock<T>(module: ImportFunction<T>): NamedModuleMock<T> | ||
enable(): rewiremock; | ||
@@ -119,0 +133,0 @@ |
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
132481
62
2861
515
6
+ Addedcompare-module-exports@1.1.0(transitive)