rewiremock
Advanced tools
Comparing version 1.3.2 to 1.3.3
@@ -103,2 +103,43 @@ import {expect} from 'chai'; | ||
it('should replace one module by another: ', () => { | ||
return rewiremock.around( () => require('./lib/a/test.js'), | ||
()=> { | ||
addPlugin(relativePlugin); | ||
rewiremock('./foo').by('./foo2'); | ||
}) | ||
.then( mocked => expect(mocked()).to.be.equal('FOObarbaz')); | ||
}); | ||
it('should replace one module by autogenerated one: ', () => { | ||
let toched = false; | ||
return rewiremock.around( () => require('./lib/a/test.js'), | ||
()=> { | ||
addPlugin(relativePlugin); | ||
rewiremock('./foo').by(({name}) => { | ||
toched = name; | ||
return () => "FOO"; | ||
}); | ||
expect(toched).to.be.false; | ||
}) | ||
.then( mocked => { | ||
expect(mocked()).to.be.equal('FOObarbaz'); | ||
expect(toched).to.be.equal('./foo'); | ||
}); | ||
}); | ||
it('should replace one module by autogenerated one with callThougth: ', () => { | ||
return rewiremock.around( () => require('./lib/a/test.js'), | ||
()=> { | ||
addPlugin(relativePlugin); | ||
rewiremock('./foo') | ||
.callThought() | ||
.by(({original}) => { | ||
return () => "~"+original()+'~' | ||
}); | ||
}) | ||
.then( mocked => expect(mocked()).to.be.equal('~foo~barbaz')); | ||
}); | ||
}); |
@@ -83,15 +83,27 @@ 'use strict'; | ||
if (mock.allowCallThought) { | ||
if (!mock.original) { | ||
mock.original = originalLoader(request, parent, isMain); | ||
} | ||
} | ||
if (mock.overrideBy) { | ||
if (!mock.module) { | ||
mock.module = originalLoader(mock.overrideBy, parent, isMain); | ||
if (!mock.override) { | ||
if (typeof mock.overrideBy === 'string') { | ||
mock.override = originalLoader(mock.overrideBy, parent, isMain); | ||
} else { | ||
mock.override = mock.overrideBy({ | ||
name: request, | ||
fullName: baseRequest, | ||
parent: parent, | ||
original: mock.original | ||
}); | ||
} | ||
} | ||
return mockResult(request, mock.module); | ||
return mockResult(request, mock.override); | ||
} | ||
if (mock.allowCallThought) { | ||
if (!mock.module) { | ||
mock.module = originalLoader(request, parent, isMain); | ||
} | ||
return mockResult(request, Object.assign({}, mock.module, mock.value, { | ||
__esModule: mock.module.__esModule | ||
return mockResult(request, Object.assign({}, mock.original, mock.value, { | ||
__esModule: mock.original.__esModule | ||
})); | ||
@@ -98,0 +110,0 @@ } |
@@ -90,3 +90,3 @@ 'use strict'; | ||
* Overriding export of one module by another | ||
* @param {String} name | ||
* @param {String|Function} newTarget | ||
* @return {ModuleMock} | ||
@@ -97,4 +97,8 @@ */ | ||
key: 'by', | ||
value: function by(name) { | ||
this.mock.overrideBy = (0, _plugins.convertName)(name, (0, _globals2.default)().parentModule); | ||
value: function by(newTarget) { | ||
if (typeof newTarget == 'string') { | ||
this.mock.overrideBy = (0, _plugins.convertName)(newTarget, (0, _globals2.default)().parentModule); | ||
} else { | ||
this.mock.overrideBy = newTarget; | ||
} | ||
return this; | ||
@@ -101,0 +105,0 @@ } |
@@ -7,6 +7,2 @@ 'use strict'; | ||
var _globals = require('../globals'); | ||
var _globals2 = _interopRequireDefault(_globals); | ||
var _common = require('./_common'); | ||
@@ -13,0 +9,0 @@ |
{ | ||
"name": "rewiremock", | ||
"version": "1.3.2", | ||
"version": "1.3.3", | ||
"description": "Easy and es6 compatible mocking tool", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -0,1 +1,8 @@ | ||
interface OverloadedModule { | ||
name: String, | ||
fileName: String, | ||
parent: Object, | ||
original: Object | ||
}; | ||
interface ModuleMock { | ||
@@ -26,2 +33,6 @@ /** | ||
by(module: string): ModuleMock, | ||
/** | ||
* Overriding export of one module by something generated by a function | ||
*/ | ||
by(module: (OverloadedModule) => Object): ModuleMock, | ||
@@ -28,0 +39,0 @@ enable(): ModuleMock, |
@@ -56,3 +56,3 @@ import Module from 'module'; | ||
if (mock) { | ||
if(shouldMock(mock, request, parent, parentModule)) { | ||
if (shouldMock(mock, request, parent, parentModule)) { | ||
// this file fill be not cached, but it`s opener - will. And we have to remeber it | ||
@@ -65,17 +65,29 @@ mockedModules[parent.filename] = true; | ||
if (mock.allowCallThought) { | ||
if (!mock.original) { | ||
mock.original = originalLoader(request, parent, isMain); | ||
} | ||
} | ||
if (mock.overrideBy) { | ||
if (!mock.module) { | ||
mock.module = originalLoader(mock.overrideBy, parent, isMain) | ||
if (!mock.override) { | ||
if (typeof mock.overrideBy === 'string') { | ||
mock.override = originalLoader(mock.overrideBy, parent, isMain) | ||
} else { | ||
mock.override = mock.overrideBy({ | ||
name: request, | ||
fullName: baseRequest, | ||
parent: parent, | ||
original: mock.original | ||
}); | ||
} | ||
} | ||
return mockResult(request, mock.module); | ||
return mockResult(request, mock.override); | ||
} | ||
if (mock.allowCallThought) { | ||
if (!mock.module) { | ||
mock.module = originalLoader(request, parent, isMain); | ||
} | ||
return mockResult(request, { | ||
...mock.module, | ||
...mock.original, | ||
...mock.value, | ||
__esModule: mock.module.__esModule | ||
__esModule: mock.original.__esModule | ||
}); | ||
@@ -82,0 +94,0 @@ } |
@@ -58,7 +58,11 @@ import {convertName} from './plugins'; | ||
* Overriding export of one module by another | ||
* @param {String} name | ||
* @param {String|Function} newTarget | ||
* @return {ModuleMock} | ||
*/ | ||
by(name) { | ||
this.mock.overrideBy = convertName(name, getScope().parentModule); | ||
by(newTarget) { | ||
if (typeof newTarget == 'string') { | ||
this.mock.overrideBy = convertName(newTarget, getScope().parentModule); | ||
} else { | ||
this.mock.overrideBy = newTarget; | ||
} | ||
return this; | ||
@@ -65,0 +69,0 @@ } |
@@ -1,2 +0,1 @@ | ||
import getScope from '../globals'; | ||
import createPlugin from './_common'; | ||
@@ -3,0 +2,0 @@ |
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
110574
84
2755