Comparing version 1.2.0 to 1.3.0
@@ -7,3 +7,3 @@ /* | ||
Copyright (c) 2011 Yahoo! Inc. All Rights Reserved. | ||
Copyright (c) 2011-2012 Yahoo! Inc. All Rights Reserved. | ||
@@ -36,2 +36,6 @@ Permission is hereby granted, free of charge, to any person obtaining a copy | ||
/*jslint nomen: true */ | ||
"use strict"; | ||
var m = require('module'), | ||
@@ -42,5 +46,27 @@ registeredMocks = {}, | ||
originalLoader = null, | ||
warnIfUnregistered = true; | ||
defaultOptions = { | ||
warnOnReplace: true, | ||
warnOnUnregistered: true | ||
}, | ||
options = {}; | ||
/* | ||
* Merge the supplied options in with a new copy of the default options to get | ||
* the effective options, and return those. | ||
*/ | ||
function getEffectiveOptions(opts) { | ||
var options = {}; | ||
Object.keys(defaultOptions).forEach(function (key) { | ||
options[key] = defaultOptions[key]; | ||
}); | ||
if (opts) { | ||
Object.keys(opts).forEach(function (key) { | ||
options[key] = opts[key]; | ||
}); | ||
} | ||
return options; | ||
} | ||
/* | ||
* The perils of using internal functions. The Node-internal _resolveFilename | ||
@@ -76,3 +102,5 @@ * function was changed in commit 840229a8251955d2b791928875f36d35127dcad0 | ||
return registeredMocks[request]; | ||
} else if (registeredSubstitutes.hasOwnProperty(request)) { | ||
} | ||
if (registeredSubstitutes.hasOwnProperty(request)) { | ||
subst = registeredSubstitutes[request]; | ||
@@ -86,18 +114,19 @@ if (!subst.module && subst.name) { | ||
return subst.module; | ||
} else { | ||
if (registeredAllowables.hasOwnProperty(request)) { | ||
allow = registeredAllowables[request]; | ||
if (allow.unhook) { | ||
file = resolveFilename(request, parent); | ||
if (file.indexOf('/') !== -1 && allow.paths.indexOf(file) === -1) { | ||
allow.paths.push(file); | ||
} | ||
} | ||
if (registeredAllowables.hasOwnProperty(request)) { | ||
allow = registeredAllowables[request]; | ||
if (allow.unhook) { | ||
file = resolveFilename(request, parent); | ||
if (file.indexOf('/') !== -1 && allow.paths.indexOf(file) === -1) { | ||
allow.paths.push(file); | ||
} | ||
} else { | ||
if (warnIfUnregistered) { | ||
console.warn("WARNING: loading non-allowed module: " + request); | ||
} | ||
} | ||
return originalLoader(request, parent, isMain); | ||
} else { | ||
if (options.warnOnUnregistered) { | ||
console.warn("WARNING: loading non-allowed module: " + request); | ||
} | ||
} | ||
return originalLoader(request, parent, isMain); | ||
} | ||
@@ -110,3 +139,3 @@ | ||
*/ | ||
function enable() { | ||
function enable(opts) { | ||
if (originalLoader) { | ||
@@ -116,3 +145,3 @@ // Already hooked | ||
} | ||
/*jslint nomen: true */ | ||
options = getEffectiveOptions(opts); | ||
originalLoader = m._load; | ||
@@ -132,3 +161,2 @@ m._load = hookedLoader; | ||
} | ||
/*jslint nomen: true */ | ||
m._load = originalLoader; | ||
@@ -139,2 +167,10 @@ originalLoader = null; | ||
/* | ||
* Enable or disable warnings to the console when previously registered mocks | ||
* and subsitutes are replaced. | ||
*/ | ||
function warnOnReplace(enable) { | ||
options.warnOnReplace = enable; | ||
} | ||
/* | ||
* Enable or disable warnings to the console when modules are loaded that have | ||
@@ -144,3 +180,3 @@ * not been registered as a mock, a substitute, or allowed. | ||
function warnOnUnregistered(enable) { | ||
warnIfUnregistered = enable; | ||
options.warnOnUnregistered = enable; | ||
} | ||
@@ -155,3 +191,3 @@ | ||
function registerMock(mod, mock) { | ||
if (registeredMocks.hasOwnProperty(mod)) { | ||
if (options.warnOnReplace && registeredMocks.hasOwnProperty(mod)) { | ||
console.warn("WARNING: Replacing existing mock for module: " + mod); | ||
@@ -180,3 +216,3 @@ } | ||
function registerSubstitute(mod, subst) { | ||
if (registeredSubstitutes.hasOwnProperty(mod)) { | ||
if (options.warnOnReplace && registeredSubstitutes.hasOwnProperty(mod)) { | ||
console.warn("WARNING: Replacing existing substitute for module: " + mod); | ||
@@ -278,2 +314,3 @@ } | ||
exports.disable = disable; | ||
exports.warnOnReplace = warnOnReplace; | ||
exports.warnOnUnregistered = warnOnUnregistered; | ||
@@ -280,0 +317,0 @@ exports.registerMock = registerMock; |
@@ -11,3 +11,3 @@ { | ||
"description": "Simplifying the use of mocks with Node.js", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"repository": { | ||
@@ -14,0 +14,0 @@ "type": "git", |
@@ -43,2 +43,24 @@ # Mockery - Simplifying the use of mocks with Node.js | ||
### Options | ||
You can set up some initial configuration by passing an options object to | ||
`enable`. Omitting the options object, or any of the defined keys, causes the | ||
standard defaults to be used. | ||
For example, to disable all warnings, you might use this: | ||
mockery.enable({ | ||
warnOnReplace: false, | ||
warnOnUnregistered: false | ||
}); | ||
The available options are: | ||
* _warnOnReplace_ determines whether or not warnings are issued when a mock or | ||
substitute is replaced without being first deregistered. This has the same | ||
effect as the `warnOnReplace` function. [Default: true] | ||
* _warnOnUnregistered_ determines whether or not warnings are issued when a | ||
module is not mocked, substituted or allowed. This has the same effect as the | ||
`warnOnUnregistered` function. [Default: true] | ||
## Registering mocks | ||
@@ -165,5 +187,13 @@ | ||
If you later need to re-enable the warnings, then passing `true` to the same | ||
function will do that, as you might imagine. | ||
Mockery will also print a warning to the console whenever you register a mock | ||
or substitute for a module for which one is already registered. This is almost | ||
always what you want, since you should be deregistering mocks and substitutes | ||
that you no longer need. Occasionally, though, you may want to suppress these | ||
warnings, which you can do like this: | ||
mockery.warnOnReplace(false); | ||
In either of these cases, if you later need to re-enable the warnings, then | ||
passing `true` to the same functions will do that, as you might imagine. | ||
## The name | ||
@@ -170,0 +200,0 @@ |
/* | ||
Copyrights for code authored by Yahoo! Inc. is licensed under the following | ||
terms: | ||
MIT License | ||
Copyright (c) 2011-2012 Yahoo! Inc. All Rights Reserved. | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to | ||
deal in the Software without restriction, including without limitation the | ||
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
sell copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
DEALINGS IN THE SOFTWARE. | ||
*/ | ||
/* | ||
* Run with nodeunit: | ||
* nodeunit --reporter nested mockery.functional.js | ||
*/ | ||
var testCase = require('nodeunit').testCase; | ||
var mockery = require('../mockery'); | ||
var sinon = require('sinon'); | ||
var m = require('module'); | ||
/*jslint nomen: true */ | ||
"use strict"; | ||
var testCase = require('nodeunit').testCase, | ||
mockery = require('../mockery'), | ||
sinon = require('sinon'), | ||
m = require('module'); | ||
var mock_fake_module = { | ||
@@ -17,5 +49,9 @@ foo: function () { | ||
function isCached(name) { | ||
var id; | ||
// Super-simplistic, but good enough for the tests | ||
for (var id in m._cache) { | ||
if (id.indexOf(name) !== -1) return true; | ||
for (id in m._cache) { | ||
if (m._cache.hasOwnProperty(id) && id.indexOf(name) !== -1) { | ||
return true; | ||
} | ||
} | ||
@@ -45,6 +81,8 @@ | ||
"requiring a module causes a warning to be logged": function (test) { | ||
var mock_console = sinon.mock(console); | ||
var mock_console, fake_module; | ||
mock_console = sinon.mock(console); | ||
mock_console.expects('warn').once(); | ||
var fake_module = require('./fixtures/fake_module'); | ||
fake_module = require('./fixtures/fake_module'); | ||
test.equal(fake_module.foo(), 'real foo'); | ||
@@ -64,6 +102,8 @@ | ||
"requiring a module causes no warning to be logged": function (test) { | ||
var mock_console = sinon.mock(console); | ||
var mock_console, fake_module; | ||
mock_console = sinon.mock(console); | ||
mock_console.expects('warn').never(); | ||
var fake_module = require('./fixtures/fake_module'); | ||
fake_module = require('./fixtures/fake_module'); | ||
test.equal(fake_module.foo(), 'real foo'); | ||
@@ -84,6 +124,8 @@ | ||
"requiring a module causes a warning to be logged": function (test) { | ||
var mock_console = sinon.mock(console); | ||
var mock_console, fake_module; | ||
mock_console = sinon.mock(console); | ||
mock_console.expects('warn').once(); | ||
var fake_module = require('./fixtures/fake_module'); | ||
fake_module = require('./fixtures/fake_module'); | ||
test.equal(fake_module.foo(), 'real foo'); | ||
@@ -112,6 +154,8 @@ | ||
"requiring the module causes no warning to be logged": function (test) { | ||
var mock_console = sinon.mock(console); | ||
var mock_console, fake_module; | ||
mock_console = sinon.mock(console); | ||
mock_console.expects('warn').never(); | ||
var fake_module = require('./fixtures/fake_module'); | ||
fake_module = require('./fixtures/fake_module'); | ||
test.equal(fake_module.foo(), 'real foo'); | ||
@@ -131,6 +175,8 @@ | ||
"requiring the module causes a warning to be logged": function (test) { | ||
var mock_console = sinon.mock(console); | ||
var mock_console, fake_module; | ||
mock_console = sinon.mock(console); | ||
mock_console.expects('warn').once(); | ||
var fake_module = require('./fixtures/fake_module'); | ||
fake_module = require('./fixtures/fake_module'); | ||
test.equal(fake_module.foo(), 'real foo'); | ||
@@ -149,3 +195,4 @@ | ||
mockery.registerAllowables( | ||
['./fixtures/fake_module', './fixtures/fake_module_2']); | ||
['./fixtures/fake_module', './fixtures/fake_module_2'] | ||
); | ||
callback(); | ||
@@ -161,9 +208,11 @@ }, | ||
"requiring the modules causes no warning to be logged": function (test) { | ||
var mock_console = sinon.mock(console); | ||
var mock_console, fake_module, fake_module_2; | ||
mock_console = sinon.mock(console); | ||
mock_console.expects('warn').never(); | ||
var fake_module = require('./fixtures/fake_module'); | ||
fake_module = require('./fixtures/fake_module'); | ||
test.equal(fake_module.foo(), 'real foo'); | ||
var fake_module_2 = require('./fixtures/fake_module_2'); | ||
fake_module_2 = require('./fixtures/fake_module_2'); | ||
test.equal(fake_module_2.bar(), 'real bar'); | ||
@@ -179,3 +228,4 @@ | ||
mockery.deregisterAllowables( | ||
['./fixtures/fake_module', './fixtures/fake_module_2']); | ||
['./fixtures/fake_module', './fixtures/fake_module_2'] | ||
); | ||
callback(); | ||
@@ -185,9 +235,11 @@ }, | ||
"requiring the modules causes warnings to be logged": function (test) { | ||
var mock_console = sinon.mock(console); | ||
var mock_console, fake_module, fake_module_2; | ||
mock_console = sinon.mock(console); | ||
mock_console.expects('warn').twice(); | ||
var fake_module = require('./fixtures/fake_module'); | ||
fake_module = require('./fixtures/fake_module'); | ||
test.equal(fake_module.foo(), 'real foo'); | ||
var fake_module_2 = require('./fixtures/fake_module_2'); | ||
fake_module_2 = require('./fixtures/fake_module_2'); | ||
test.equal(fake_module_2.bar(), 'real bar'); | ||
@@ -236,3 +288,3 @@ | ||
setUp: function (callback) { | ||
var fake_module = require('./fixtures/fake_module'); | ||
require('./fixtures/fake_module'); | ||
callback(); | ||
@@ -257,3 +309,3 @@ }, | ||
}) | ||
}), | ||
}) | ||
}) | ||
@@ -297,2 +349,54 @@ }), | ||
} | ||
}), | ||
"registering a replacement causes a warning to be logged": function (test) { | ||
var mock_console; | ||
mock_console = sinon.mock(console); | ||
mock_console.expects('warn').once(); | ||
mockery.registerMock('./fixtures/fake_module', mock_fake_module); | ||
mock_console.verify(); | ||
mock_console.restore(); | ||
test.done(); | ||
}, | ||
"and warnings are disabled": testCase({ | ||
setUp: function (callback) { | ||
mockery.warnOnReplace(false); | ||
callback(); | ||
}, | ||
"registering a replacement causes no warning to be logged": function (test) { | ||
var mock_console; | ||
mock_console = sinon.mock(console); | ||
mock_console.expects('warn').never(); | ||
mockery.registerMock('./fixtures/fake_module', mock_fake_module); | ||
mock_console.verify(); | ||
mock_console.restore(); | ||
test.done(); | ||
}, | ||
"and warnings are reenabled": testCase({ | ||
setUp: function (callback) { | ||
mockery.warnOnReplace(true); | ||
callback(); | ||
}, | ||
"registering a replacement causes a warning to be logged": function (test) { | ||
var mock_console; | ||
mock_console = sinon.mock(console); | ||
mock_console.expects('warn').once(); | ||
mockery.registerMock('./fixtures/fake_module', mock_fake_module); | ||
mock_console.verify(); | ||
mock_console.restore(); | ||
test.done(); | ||
} | ||
}) | ||
}) | ||
@@ -319,3 +423,58 @@ }) | ||
test.done(); | ||
} | ||
}, | ||
"registering a replacement causes a warning to be logged": function (test) { | ||
var mock_console; | ||
mock_console = sinon.mock(console); | ||
mock_console.expects('warn').once(); | ||
mockery.registerSubstitute('./fixtures/fake_module', | ||
'./fixtures/substitute_fake_module'); | ||
mock_console.verify(); | ||
mock_console.restore(); | ||
test.done(); | ||
}, | ||
"and warnings are disabled": testCase({ | ||
setUp: function (callback) { | ||
mockery.warnOnReplace(false); | ||
callback(); | ||
}, | ||
"registering a replacement causes no warning to be logged": function (test) { | ||
var mock_console; | ||
mock_console = sinon.mock(console); | ||
mock_console.expects('warn').never(); | ||
mockery.registerSubstitute('./fixtures/fake_module', | ||
'./fixtures/substitute_fake_module'); | ||
mock_console.verify(); | ||
mock_console.restore(); | ||
test.done(); | ||
}, | ||
"and warnings are reenabled": testCase({ | ||
setUp: function (callback) { | ||
mockery.warnOnReplace(true); | ||
callback(); | ||
}, | ||
"registering a replacement causes a warning to be logged": function (test) { | ||
var mock_console; | ||
mock_console = sinon.mock(console); | ||
mock_console.expects('warn').once(); | ||
mockery.registerSubstitute('./fixtures/fake_module', | ||
'./fixtures/substitute_fake_module'); | ||
mock_console.verify(); | ||
mock_console.restore(); | ||
test.done(); | ||
} | ||
}) | ||
}) | ||
}) | ||
@@ -322,0 +481,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
36336
666
204