rewiremock
Advanced tools
Comparing version 3.14.0 to 3.14.1
@@ -0,1 +1,38 @@ | ||
## [3.14.1](https://github.com/theKashey/rewiremock/compare/v3.14.0...v3.14.1) (2020-05-08) | ||
### Bug Fixes | ||
* isolation is working only for the current scope ([5d5a07f](https://github.com/theKashey/rewiremock/commit/5d5a07f)) | ||
# [3.14.0](https://github.com/theKashey/rewiremock/compare/v3.13.9...v3.14.0) (2020-05-07) | ||
### Bug Fixes | ||
* update cache controls ([dd9df1d](https://github.com/theKashey/rewiremock/commit/dd9df1d)) | ||
* update wipe-node-cache to prevent memory leak in the cache management ([49ac22a](https://github.com/theKashey/rewiremock/commit/49ac22a)) | ||
## [3.13.9](https://github.com/theKashey/rewiremock/compare/v3.13.8...v3.13.9) (2019-10-20) | ||
## [3.13.8](https://github.com/theKashey/rewiremock/compare/v3.13.7...v3.13.8) (2019-10-19) | ||
### Bug Fixes | ||
* don't parse imports at exit hook, fixes [#102](https://github.com/theKashey/rewiremock/issues/102) ([451b9e1](https://github.com/theKashey/rewiremock/commit/451b9e1)) | ||
### Features | ||
* new cache management ([d9f164c](https://github.com/theKashey/rewiremock/commit/d9f164c)) | ||
## [3.13.7](https://github.com/theKashey/rewiremock/compare/v3.13.6...v3.13.7) (2019-05-11) | ||
@@ -2,0 +39,0 @@ |
@@ -15,2 +15,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; }; | ||
import { NO } from "./plugins/_common"; | ||
import { safelyRemoveCache } from "./wipeCache"; | ||
@@ -211,5 +212,6 @@ var matchOrigin = matchOriginFabric('rewiremock'); | ||
parentModule = _getScope2.parentModule, | ||
mockedModules = _getScope2.mockedModules, | ||
isolation = _getScope2.isolation; | ||
mockedModules = _getScope2.mockedModules; | ||
var isolation = getScopeVariable('isolation'); | ||
asyncTest(); | ||
@@ -223,3 +225,3 @@ | ||
if (moduleCompare(parent, parentModule) || moduleCompare(parent, thisModule)) { | ||
delete Module._cache[baseRequest]; | ||
safelyRemoveCache(baseRequest); | ||
mockedModules[baseRequest] = true; | ||
@@ -226,0 +228,0 @@ } |
import path from 'path'; | ||
import { wipe } from './wipeCache'; | ||
import { safelyRemoveCache, wipe } from './wipeCache'; | ||
import { _clearPlugins } from './plugins'; | ||
@@ -17,14 +17,2 @@ import plugins from './plugins/index'; | ||
function safelyRemoveCache(moduleName) { | ||
var m = require.cache[moduleName]; | ||
if (m) { | ||
if (m.parent && m.parent.children) { | ||
m.parent.children = m.parent.children.filter(function (x) { | ||
return x !== m; | ||
}); | ||
} | ||
delete require.cache[moduleName]; | ||
} | ||
} | ||
// delete core | ||
@@ -31,0 +19,0 @@ safelyRemoveCache(path.join(path.dirname(__filename), './mockModule.js')); |
@@ -44,3 +44,3 @@ import { dirname, resolve } from 'path'; | ||
relativeFileName: function relativeFileName(name, parent) { | ||
if (name[0] == '.') { | ||
if (name[0] === '.') { | ||
return this._resolveFilename(name, parent); | ||
@@ -47,0 +47,0 @@ } |
import { getAllMocks } from './mocks'; | ||
import { shouldWipe } from './plugins'; | ||
import { relativeWipeCheck } from "./plugins/relative"; | ||
import Module from './module'; | ||
@@ -30,2 +31,14 @@ // which one? | ||
export function safelyRemoveCache(moduleName) { | ||
var m = Module._cache[moduleName]; | ||
if (m) { | ||
if (m.parent && m.parent.children) { | ||
m.parent.children = m.parent.children.filter(function (x) { | ||
return x !== m; | ||
}); | ||
} | ||
delete Module._cache[moduleName]; | ||
} | ||
} | ||
export default wipeCache; |
@@ -68,2 +68,4 @@ 'use strict'; | ||
var _wipeCache = require('./wipeCache'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -265,5 +267,6 @@ | ||
parentModule = _getScope2.parentModule, | ||
mockedModules = _getScope2.mockedModules, | ||
isolation = _getScope2.isolation; | ||
mockedModules = _getScope2.mockedModules; | ||
var isolation = (0, _globals.getScopeVariable)('isolation'); | ||
asyncTest(); | ||
@@ -277,3 +280,3 @@ | ||
if ((0, _module.moduleCompare)(parent, parentModule) || (0, _module.moduleCompare)(parent, thisModule)) { | ||
delete _module2.default._cache[baseRequest]; | ||
(0, _wipeCache.safelyRemoveCache)(baseRequest); | ||
mockedModules[baseRequest] = true; | ||
@@ -280,0 +283,0 @@ } |
@@ -45,18 +45,6 @@ 'use strict'; | ||
function safelyRemoveCache(moduleName) { | ||
var m = require.cache[moduleName]; | ||
if (m) { | ||
if (m.parent && m.parent.children) { | ||
m.parent.children = m.parent.children.filter(function (x) { | ||
return x !== m; | ||
}); | ||
} | ||
delete require.cache[moduleName]; | ||
} | ||
} | ||
// delete core | ||
safelyRemoveCache(_path2.default.join(_path2.default.dirname(__filename), './mockModule.js')); | ||
(0, _wipeCache.safelyRemoveCache)(_path2.default.join(_path2.default.dirname(__filename), './mockModule.js')); | ||
// delete self | ||
safelyRemoveCache(moduleName.replace('index.js', 'mockModule.js')); | ||
(0, _wipeCache.safelyRemoveCache)(moduleName.replace('index.js', 'mockModule.js')); | ||
@@ -71,3 +59,3 @@ var cleanup = exports.cleanup = function cleanup() { | ||
var overrideEntryPoint = exports.overrideEntryPoint = function overrideEntryPoint(module) { | ||
safelyRemoveCache((0, _module.getModuleName)(module)); | ||
(0, _wipeCache.safelyRemoveCache)((0, _module.getModuleName)(module)); | ||
API.mockModule.overrideEntryPoint((0, _module.getModuleParent)(module)); | ||
@@ -74,0 +62,0 @@ //API.cleanup(); |
@@ -69,3 +69,3 @@ 'use strict'; | ||
relativeFileName: function relativeFileName(name, parent) { | ||
if (name[0] == '.') { | ||
if (name[0] === '.') { | ||
return this._resolveFilename(name, parent); | ||
@@ -72,0 +72,0 @@ } |
@@ -7,2 +7,3 @@ 'use strict'; | ||
exports.wipe = undefined; | ||
exports.safelyRemoveCache = safelyRemoveCache; | ||
@@ -15,2 +16,8 @@ var _mocks = require('./mocks'); | ||
var _module = require('./module'); | ||
var _module2 = _interopRequireDefault(_module); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
// which one? | ||
@@ -41,2 +48,14 @@ var wipe = exports.wipe = typeof __webpack_require__ === 'function' ? require('wipe-webpack-cache') : require('wipe-node-cache').wipeCache; | ||
function safelyRemoveCache(moduleName) { | ||
var m = _module2.default._cache[moduleName]; | ||
if (m) { | ||
if (m.parent && m.parent.children) { | ||
m.parent.children = m.parent.children.filter(function (x) { | ||
return x !== m; | ||
}); | ||
} | ||
delete _module2.default._cache[moduleName]; | ||
} | ||
} | ||
exports.default = wipeCache; |
{ | ||
"name": "rewiremock", | ||
"version": "3.14.0", | ||
"version": "3.14.1", | ||
"description": "Advanced dependency mocking device.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -12,16 +12,6 @@ ```text | ||
[![Build Status](https://secure.travis-ci.org/theKashey/rewiremock.svg)](http://travis-ci.org/theKashey/rewiremock) | ||
[![Build Status](https://travis-ci.org/theKashey/rewiremock.svg)](http://travis-ci.org/theKashey/rewiremock) | ||
[![coverage-badge](https://img.shields.io/codecov/c/github/thekashey/rewiremock.svg?style=flat-square)](https://codecov.io/github/thekashey/rewiremock) | ||
[![version-badge](https://img.shields.io/npm/v/rewiremock.svg?style=flat-square)](https://www.npmjs.com/package/rewiremock) | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/theKashey/rewiremock.svg)](https://greenkeeper.io/) | ||
Dependency mocking, inspired by the best libraries: | ||
- [mockery](https://github.com/mfncooper/mockery) - `rewiremock` __is__ a better `mockery`, with the same interface. | ||
- [proxyquire](https://github.com/theKashey/proxyquire) - `rewiremock` __is__ a better `proxyquire`, with the same interface. | ||
- [mock-require](https://github.com/boblauer/mock-require) - things must not be complex, `rewiremock` __is__ not. | ||
- [jest.mocks](https://facebook.github.io/jest/docs/en/manual-mocks.html) - `jest` is awesome. As well as `rewiremock`. | ||
Rewiremock is a better version of your favorite mocking library. It can be used with `mocha`, `ava`, `karma`, and anything that's not `jest`. | ||
By design, rewiremock has the same behavior as Mockery. But it can behave like other libraries too, exposing handy interfaces to make mocking a joy. Supports type-safe mocking and provides TS/Flow types for itself. | ||
@@ -32,3 +22,3 @@ # Quick start | ||
## 2. Setup | ||
I would recommend not to importing `rewiremock` directly from tests, but create a `rewiremock.js` file and require it - that way, you can preconfigure rewiremock for all tests. | ||
I would recommend not importing `rewiremock` directly from tests, but create a `rewiremock.js` file and require it - in this way, you can _preconfigure_ rewiremock for all tests. | ||
### for ts/es6/esm use `import` | ||
@@ -38,4 +28,5 @@ ```js | ||
import rewiremock from 'rewiremock'; | ||
/// settings | ||
rewiremock.overrideEntryPoint(module); // this is important | ||
// settings | ||
// .... | ||
rewiremock.overrideEntryPoint(module); // this is important. This command is "transfering" this module parent to rewiremock | ||
export { rewiremock } | ||
@@ -46,3 +37,4 @@ ``` | ||
// rewiremock.cjs.js | ||
const rewiremock = require('rewiremock/node'); | ||
const rewiremock = require('rewiremock/node'); | ||
// nothng more than `plugins.node`, but it might change how filename resolution works | ||
/// settings | ||
@@ -76,2 +68,7 @@ rewiremock.overrideEntryPoint(module); // this is important | ||
``` | ||
💡to make it really "proxyquire like" - add `.relative` plugin. Which will allow mocking of direct dependencies only, like with `proxyquire` | ||
```js | ||
import rewiremock, { addPlugin, plugins } from 'rewiremock'; | ||
addPlugin(plugins.relative); | ||
``` | ||
### mockery - like | ||
@@ -82,2 +79,3 @@ Most powerfull one | ||
rewiremock(() => require('dependency')).with(stub); | ||
rewiremock(() => import('dependency')).with(stub); // works with async API only | ||
rewiremock.enable(); | ||
@@ -93,2 +91,3 @@ const file = require('file.js'); | ||
``` | ||
💡 requires `rewiremock/babel` plugin | ||
## 4. Tune | ||
@@ -99,3 +98,3 @@ There are plenty of plugins to make your life easier. For example - this is my favorite setup | ||
import rewiremock, { addPlugin, overrideEntryPoint, plugins } from 'rewiremock'; | ||
import { configure } from 'rewiremock/lib/plugins/webpack-alias'; // could be better | ||
import { configure } from 'rewiremock/lib/plugins/webpack-alias'; // actually dont use it | ||
@@ -634,5 +633,5 @@ configure(resolve(`${__dirname}/../../webpack.config.test.js`)); | ||
By default - rewiremock has limited features. You can extend its behavior via plugins. | ||
- `relative`. A bit simplistic, proxyquire-like behavior. Will override only first level dependencies, and will wipe a lot of modules from a cache. | ||
- `relative`. A bit simplistic, proxyquire-like behavior. Will override only first level dependencies, and will wipe a lot of modules from a cache. If you need override at other place - use `.atAnyPlace` modificator. | ||
- `nodejs`. Common support to "usual" Node.js application. Will absolutize all paths. Will wipe cache very accurately. | ||
- `webpack-alias`. Enabled you to use webpack aliases as module names. | ||
- `webpack-alias`. __deprecated__. Enables you to use webpack aliases as module names. Please [use node-js resolution](https://github.com/theKashey/rewiremock/issues/7#issuecomment-621666559) for this. | ||
- `childOnly`. Only first level dependencies will be mocked. | ||
@@ -841,2 +840,14 @@ - `protectNodeModules`. Ensures that any module from node_modules will not be wiped from a cache. | ||
# Other libraries | ||
Dependency mocking, inspired by the best libraries: | ||
- [mockery](https://github.com/mfncooper/mockery) - `rewiremock` __is__ a better `mockery`, with the same interface. | ||
- [proxyquire](https://github.com/theKashey/proxyquire) - `rewiremock` __is__ a better `proxyquire`, with the same interface. | ||
- [mock-require](https://github.com/boblauer/mock-require) - things must not be complex, `rewiremock` __is__ not. | ||
- [jest.mocks](https://facebook.github.io/jest/docs/en/manual-mocks.html) - `jest` is awesome. As well as `rewiremock`. | ||
Rewiremock is a better version of your favorite mocking library. It can be used with `mocha`, `ava`, `karma`, and anything that's not `jest`. | ||
By design, rewiremock has the same behavior as Mockery. But it can behave like other libraries too, exposing handy interfaces to make mocking a joy. Supports type-safe mocking and provides TS/Flow types for itself. | ||
# Wanna read something about? | ||
@@ -843,0 +854,0 @@ [Rewiremock - medium article](https://medium.com/@antonkorzunov/how-to-mock-dependency-in-a-node-js-and-why-2ad4386f6587) |
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
201365
4160
853