rewiremock
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -9,8 +9,29 @@ import {expect} from 'chai'; | ||
describe('scope ', () => { | ||
it('scope test: ', () => { | ||
addPlugin(nodePlugin); | ||
const unmockedBaz = require('./lib/a/test.js'); | ||
expect(unmockedBaz()).to.be.equal('foobarbaz'); | ||
rewiremock('./lib/a/foo').with(() => 'aa'); | ||
rewiremock.inScope(() => { | ||
rewiremock('./lib/a/../b/bar').with(() => 'bb'); | ||
rewiremock.enable(); | ||
const mocked = require('./lib/a/test.js'); | ||
expect(mocked()).to.be.equal('aabbbaz'); | ||
rewiremock.disable(); | ||
}); | ||
rewiremock.enable(); | ||
const mocked = require('./lib/a/test.js'); | ||
expect(mocked()).to.be.equal('aabarbaz'); | ||
rewiremock.disable(); | ||
rewiremock.clear(); | ||
_clearPlugins(); | ||
}); | ||
it('scope load es5: ', () => { | ||
const unmockedBaz = require('./lib/a/test.js'); | ||
expect(unmockedBaz()).to.be.equal('foobarbaz'); | ||
return rewiremock.inScope(() => require('./lib/a/test.js'), | ||
return rewiremock.around(() => require('./lib/a/test.js'), | ||
(mock) => { | ||
@@ -31,3 +52,3 @@ addPlugin(nodePlugin); | ||
return rewiremock.inScope(() => import('./lib/a/test.js'), | ||
return rewiremock.around(() => import('./lib/a/test.js'), | ||
(mock) => { | ||
@@ -49,3 +70,3 @@ addPlugin(nodePlugin); | ||
return rewiremock.inScope(() => import('./lib/a/test.js'), | ||
return rewiremock.around(() => import('./lib/a/test.js'), | ||
(mock) => | ||
@@ -68,3 +89,3 @@ Promise.resolve().then(() => { | ||
return rewiremock.inScope( | ||
return rewiremock.around( | ||
() => | ||
@@ -88,3 +109,3 @@ import('./lib/a/test.js') | ||
return rewiremock.inScope(() => import('./lib/a/test.js'), | ||
return rewiremock.around(() => import('./lib/a/test.js'), | ||
(mock) => { | ||
@@ -106,3 +127,3 @@ addPlugin(nodePlugin); | ||
return rewiremock.inScope(() => import('./lib/a/test.js')) | ||
return rewiremock.around(() => import('./lib/a/test.js')) | ||
.then((mockedBaz) => { | ||
@@ -109,0 +130,0 @@ expect(mockedBaz()).to.be.equal('foobarbaz'); |
@@ -125,2 +125,13 @@ 'use strict'; | ||
/** | ||
* Creates temporary executing scope. All mocks and plugins you will add in callback will be removed at exit. | ||
* @param callback | ||
*/ | ||
mockModule.inScope = function (callback) { | ||
var currentScope = mockScope; | ||
updateScope(currentScope); | ||
callback(); | ||
mockScope = currentScope; | ||
}; | ||
/** | ||
* executes module in sandbox | ||
@@ -131,3 +142,3 @@ * @param {Function} loader loader callback | ||
*/ | ||
mockModule.inScope = function (loader, createCallback) { | ||
mockModule.around = function (loader, createCallback) { | ||
return new Promise(function (resolve, reject) { | ||
@@ -134,0 +145,0 @@ var currentScope = mockScope; |
{ | ||
"name": "rewiremock", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Easy and es6 compatible mocking tool", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -35,3 +35,4 @@ # rewiremock [![Build Status](https://secure.travis-ci.org/theKashey/rewiremock.svg)](http://travis-ci.org/theKashey/rewiremock) | ||
- rewiremock.disable() - wipes cache and disables interceptor. | ||
- rewuremock.inScope(loader) - loads a module in sandbox. | ||
- rewiremock.inScope(callback) - place callback inside a sandbox. | ||
- rewiremock.around(loader, creator) - loads a module in an asynchronous sandbox. | ||
## mocking API | ||
@@ -108,2 +109,16 @@ - rewiremock(moduleName: string):rewiremock - set name of overloading module | ||
# inScope | ||
Sometimes you will have independent tests in a single file, and you might need separate mocks for each one. | ||
`inScope` execute callback inside sandbox, and all mocks or plugins or anything else you have added will not leaks away. | ||
```javascript | ||
rewiremock.inScope( () => { | ||
rewiremock('something').with(something); | ||
rewiremock.enable(); | ||
.... | ||
rewiremock.disable(); | ||
// is 'something' mocked? Yes | ||
}); | ||
// is 'something' mocked? No | ||
``` | ||
# Around | ||
And there is a bit harder way to do it - scope. | ||
@@ -117,3 +132,3 @@ inScope will create new internal scope, next you can add something new to it, and then it will be destroyed. | ||
```javascript | ||
rewiremock.inScope( | ||
rewiremock.around( | ||
() => import('somemodule'), // load a module. Using import or require. | ||
@@ -135,7 +150,7 @@ // just before it you can specify mocks or anything else | ||
```javascript | ||
rewiremock.inScope(() => import('somemodule')).then(mockedModule => doSomething) | ||
rewiremock.around(() => import('somemodule')).then(mockedModule => doSomething) | ||
``` | ||
or | ||
```javascript | ||
rewiremock.inScope( | ||
rewiremock.around( | ||
() => import('somemodule').then( mockedModule => doSomething), | ||
@@ -142,0 +157,0 @@ (mock) => aPromise |
@@ -49,3 +49,4 @@ interface ModuleMock { | ||
inScope(loader: Function, creator: Function): Promise<any>; | ||
around(loader: Function, creator: Function): Promise<any>; | ||
inScope(callback); | ||
@@ -52,0 +53,0 @@ flush(); |
@@ -6,3 +6,9 @@ import Module from 'module'; | ||
import executor, {originalLoader} from './executor'; | ||
import {convertName, onMockCreate, onDisable, addPlugin as addPluginAPI, removePlugin as removePluginAPI} from './plugins'; | ||
import { | ||
convertName, | ||
onMockCreate, | ||
onDisable, | ||
addPlugin as addPluginAPI, | ||
removePlugin as removePluginAPI | ||
} from './plugins'; | ||
import {resetMock, getMock, getAllMocks} from './mocks'; | ||
@@ -97,2 +103,13 @@ import ModuleMock from './mock'; | ||
/** | ||
* Creates temporary executing scope. All mocks and plugins you will add in callback will be removed at exit. | ||
* @param callback | ||
*/ | ||
mockModule.inScope = (callback) => { | ||
const currentScope = mockScope; | ||
updateScope(currentScope); | ||
callback(); | ||
mockScope = currentScope; | ||
}; | ||
/** | ||
* executes module in sandbox | ||
@@ -103,3 +120,3 @@ * @param {Function} loader loader callback | ||
*/ | ||
mockModule.inScope = (loader, createCallback) => { | ||
mockModule.around = (loader, createCallback) => { | ||
return new Promise((resolve, reject) => { | ||
@@ -106,0 +123,0 @@ const currentScope = mockScope; |
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
104026
2577
298