Permalink
Browse files

Allow warnings to be turned off.

Allow warnings about unregistered modules to be turned off, such
that every module need not be 'allow'ed explicitly. Primarily
intended for use with functional and integration testing.
  • Loading branch information...
1 parent bb72056 commit e03e317871700c87c0cab4a5d0bcadea16a05cc5 @mfncooper committed Dec 27, 2011
Showing with 35 additions and 6 deletions.
  1. +17 −0 README.md
  2. +17 −5 mockery.js
  3. +1 −1 package.json
View
@@ -136,6 +136,23 @@ function to deregister everything:
This will deregister all mocks, substitutes, and allowable modules, as well as
unhooking any hooked modules.
+## Disabling warnings
+
+As mentioned above, if you enable Mockery and _don't_ mock, substitute, or
+allow a module that is later loaded, Mockery will print a warning to the
+console to tell you that. This is important when you're writing unit tests,
+so that you don't end up using modules you weren't aware of.
+
+In certain circumstances, such as when writing functional or integration tests,
+you may find it irritating to have to allow each module or to have all the
+warnings appear on the console. If you need to, you can tell Mockery to turn
+off those warnings:
+
+ mockery.warnOnUnregistered(true);
+
+If you later need to re-enable the warnings, then passing `false` to the same
+function will do that, as you might imagine.
+
## The name
Mockery is to mocks as rookery is to rooks.
View
@@ -36,7 +36,8 @@ var m = require('module'),
registeredMocks = {},
registeredSubstitutes = {},
registeredAllowables = {},
- originalLoader = null;
+ originalLoader = null,
+ warnIfUnregistered = true;
/*
* The (private) loader replacement that is used when hooking is enabled. It
@@ -64,16 +65,18 @@ function hookedLoader(request, parent, isMain) {
}
return subst.module;
} else {
- if (!registeredAllowables.hasOwnProperty(request)) {
- console.warn("WARNING: loading non-allowed module: " + request);
- } else {
+ if (registeredAllowables.hasOwnProperty(request)) {
allow = registeredAllowables[request];
if (allow.unhook) {
file = m._resolveFilename(request, parent)[1];
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);
}
@@ -110,6 +113,14 @@ function disable() {
}
/*
+ * Enable or disable warnings to the console when modules are loaded that have
+ * not been registered as a mock, a substitute, or allowed.
+ */
+function warnOnUnregistered(enable) {
+ warnIfUnregistered = enable;
+}
+
+/*
* Register a mock object for the specified module. While mockery is enabled,
* any subsequent 'require' for this module will return the mock object. The
* mock need not mock out all original exports, but no fallback is provided
@@ -141,7 +152,7 @@ function deregisterMock(mod) {
*/
function registerSubstitute(mod, subst) {
if (registeredSubstitutes.hasOwnProperty(mod)) {
- console.warn("WARNING: Replacing existing substiute for module: " + mod);
+ console.warn("WARNING: Replacing existing substitute for module: " + mod);
}
registeredSubstitutes[mod] = {
name: subst
@@ -215,6 +226,7 @@ function deregisterAll() {
// Exported functions
exports.enable = enable;
exports.disable = disable;
+exports.warnOnUnregistered = warnOnUnregistered;
exports.registerMock = registerMock;
exports.registerSubstitute = registerSubstitute;
exports.registerAllowable = registerAllowable;
View
@@ -2,7 +2,7 @@
"author": "Martin Cooper <mfncooper@gmail.com>",
"name": "mockery",
"description": "Simplifying the use of mocks with Node.js",
- "version": "1.0.1",
+ "version": "1.1.1",
"repository": {
"type": "git",
"url": "git://github.com/mfncooper/mockery.git"

0 comments on commit e03e317

Please sign in to comment.