rewiremock
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -6,3 +6,3 @@ 'use strict'; | ||
}); | ||
exports.overrideEntryPoint = exports.addPlugin = exports.cleanup = undefined; | ||
exports.addPlugin = exports.overrideEntryPoint = exports.cleanup = undefined; | ||
@@ -27,4 +27,2 @@ var _path = require('path'); | ||
delete require.cache[require.resolve(__filename)]; | ||
var cleanup = exports.cleanup = function cleanup() { | ||
@@ -38,3 +36,9 @@ var wipeAll = function wipeAll(stubs, moduleName) { | ||
API.overrideEntryPoint(module.parent); | ||
var overrideEntryPoint = exports.overrideEntryPoint = function overrideEntryPoint(module) { | ||
delete require.cache[require.resolve(module.filename)]; | ||
API.overrideEntryPoint(module.parent); | ||
}; | ||
overrideEntryPoint(module); | ||
// instance must be clear | ||
@@ -45,4 +49,2 @@ API.mockModule.clear(); | ||
var addPlugin = exports.addPlugin = API.addPlugin; | ||
var overrideEntryPoint = exports.overrideEntryPoint = API.overrideEntryPoint; | ||
exports.default = API.mockModule; |
@@ -8,4 +8,2 @@ 'use strict'; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _module = require('module'); | ||
@@ -92,3 +90,3 @@ | ||
} | ||
return _extends({}, mock.__MI_module, mock, { | ||
return Object.assign({}, mock.__MI_module, mock, { | ||
__esModule: mock.__MI_module.__esModule | ||
@@ -95,0 +93,0 @@ }); |
@@ -8,4 +8,2 @@ 'use strict'; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
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; }; | ||
@@ -23,3 +21,3 @@ | ||
if (stubType === 'object') { | ||
mocks[name] = _extends({}, mocks[name], stubs); | ||
mocks[name] = Object.assign({}, mocks[name], stubs); | ||
} else { | ||
@@ -26,0 +24,0 @@ mocks[name] = stubs; |
@@ -8,4 +8,2 @@ 'use strict'; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _common = require('../_common'); | ||
@@ -25,3 +23,3 @@ | ||
var createPlugin = function createPlugin(plugin) { | ||
return _extends({ | ||
return Object.assign({ | ||
fileNameTransformer: onetoone, | ||
@@ -28,0 +26,0 @@ wipeCheck: pass, |
{ | ||
"name": "rewiremock", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Easy and es6 compatible mocking tool", | ||
@@ -31,6 +31,4 @@ "main": "lib/index.js", | ||
"babel-eslint": "^7.2.3", | ||
"babel-preset-es2015": "^6.24.1", | ||
"babel-plugin-transform-object-rest-spread": "^6.23.0", | ||
"babel-preset-latest": "^6.16.0", | ||
"babel-preset-react": "^6.11.1", | ||
"babel-preset-stage-1": "^6.16.0", | ||
"babel-register": "6.18.0", | ||
@@ -37,0 +35,0 @@ "chai": "^3.5.0", |
128
README.md
@@ -29,2 +29,19 @@ # rewiremock [![Build Status](https://secure.travis-ci.org/theKashey/rewiremock.svg)](http://travis-ci.org/theKashey/rewiremock) | ||
# API | ||
see d.ts file, or JSDoc in javascript sources. | ||
## main API | ||
- rewiremock.enable() - wipes cache and enables interceptor. | ||
- rewiremock.disable() - wipes cache and disables interceptor. | ||
## mocking API | ||
- rewiremock(moduleName: string):rewiremock - set name of overloading module | ||
- .with(stubs: function | Object) - overloads current module | ||
- .withDefault(stub: function | Object) - overload `default` es6 export | ||
- .by(otherModule: string) - overload everything by another module | ||
- .callThought() - first load original module, and next extend it by provided stub. | ||
## isolation API | ||
- rewiremock.isolation() - enables isolation | ||
- rewiremock.withoutIsolation() - enables isolation | ||
- rewiremock.passBy(pattern or function) - enables some modules to pass thought isolation. | ||
# Setup | ||
@@ -60,3 +77,3 @@ | ||
# Running | ||
Just enabled, and dont forget to disable. | ||
Just enable it, and dont forget to disable. | ||
```javascript | ||
@@ -85,2 +102,10 @@ //in mocha tests | ||
- webpack-alias. Enabled you to use webpack aliases as module names. | ||
```javascript | ||
import rewiremock, { addPlugin } from 'rewiremock'; | ||
import webpackAliasPlugin from 'rewiremock/lib/plugins/webpack-alias'; | ||
addPlugin(webpackAliasPlugin); | ||
``` | ||
Bad news - you cannot remove plugin, as you cannot remove mocks. Only reset everything. | ||
But you should not different setups/plugins in single test. | ||
@@ -125,3 +150,100 @@ # Nested declarations | ||
Thats all. Happy mocking! | ||
# Your own setup. | ||
In most cases you have to: | ||
- add plugin | ||
- setup default passBy rules | ||
- add some common mocks | ||
- do something else. | ||
And it is not a good idea to do it in every test you have. | ||
It is better to have one setup file, which will do everything for you | ||
* Part one - man in the middle | ||
```javascript | ||
// this is your test file | ||
// instead of importing original file - import your own one | ||
// import rewiremock from 'rewiremock'; | ||
import rewiremock from 'test/rewiremock'; | ||
``` | ||
* Part 2 - create your own one | ||
```javascript | ||
// this tests/rewiremock.js | ||
import rewiremock, { addPlugin, overrideEntryPoint} from 'rewiremock'; | ||
// do anything you need | ||
addPlugin(something); | ||
rewiremock('somemodule').with(/*....*/); | ||
// but dont forget to add some magic | ||
overrideEntryPoint(module); // <-- set yourself as top module | ||
// PS: rewiremock will wipe this module from cache to keep magics alive. | ||
export default rewiremock; | ||
``` | ||
* Part 3 - enjoy. | ||
You extract some common code into helper. A lot of things become easer. | ||
# Caching | ||
Other libraries will always do a strange things with a cache: | ||
- just wipe everything. Absolutely. | ||
As result tests will run slowly, or even did not run at all. | ||
Normaly you should not wipe native(.node) modules, and external(node_modules) modules. | ||
For example you should not wipe React - _new_ version of React will be incompatible with old one. | ||
- wipe only listed modules. Exactly. | ||
Also not a good idea, as long sometimes between first module, and mocked one you can found some sort of middleware. | ||
Syntax sugar, thirdparty library, helper, and so on. | ||
Rewiremock uses a bit different, more smart way: | ||
- all files required from original test, while interceptor is active, will bypass cache. | ||
(proxyquire cant do it, as long it work at more low level API). | ||
- all files you indicate as mock will be removed from cache. Unfortunately all, no matter of usage. | ||
- all files which use any wiped ones - will be also removed from a cache. | ||
- repeat. | ||
As result - it will never wipe something it should not. | ||
As result - you can mock any file at any level. Sometimes it is usefull. | ||
If you __dont want__ this - just add `relative` plugin. It will allow mocking only for modules | ||
> _required from __module__ with __parent__ equals __entryPoint__._ | ||
PS: module with parent equals entryPoint - any module you require from test(it is an entry point). | ||
required from that module - first level require. Simple. | ||
# Own plugins | ||
Dont forget - you can write your own plugins. | ||
plugin is an object with fields: | ||
```javascript | ||
{ | ||
// to transform name. Used by alias or node.js module | ||
fileNameTransformer: (fileName, parentModule) => fileName; | ||
// check should you wipe module or not. Never used :) | ||
wipeCheck: (stubs, moduleName) => boolean, | ||
// check is mocking allowed for a module. User in relative plugin | ||
shouldMock: (mock, requestFilename, parentModule, entryPoint) => boolean | ||
} | ||
``` | ||
## magic constants | ||
Rewiremock stores some information in magic, unenumerable, constants: | ||
- '__esModule' - standard babel one | ||
- '__MI_overrideBy' - .by command information | ||
- '__MI_allowCallThought' - .callThought command information | ||
- '__MI_name' - mock original name | ||
- '__MI_module' - original module. If exists due to .callThought or .by commands. | ||
# Licence | ||
MIT | ||
Happy mocking! |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
59761
10
246
1347