Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 3 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
Showing with 129 additions and 2 deletions.
  1. +16 −1 mockery.js
  2. +1 −1  package.json
  3. +112 −0 test/mockery.functional.js
View
17 mockery.js
@@ -40,6 +40,21 @@ var m = require('module'),
warnIfUnregistered = true;
/*
+ * The perils of using internal functions. The Node-internal _resolveFilename
+ * function was changed in commit 840229a8251955d2b791928875f36d35127dcad0
+ * (just prior to v0.6.10) such that it returns a string, whereas previously
+ * it returned an array. Instead of playing version number tricks, just check
+ * for an array and pull the filename from that if necessary.
+ */
+function resolveFilename(request, parent) {
+ var filename = m._resolveFilename(request, parent);
+ if (Array.isArray(filename)) {
+ filename = filename[1];
+ }
+ return filename;
+}
+
+/*
* The (private) loader replacement that is used when hooking is enabled. It
* does the work of returning a mock or substitute when configured, reporting
* non-allowed modules, and invoking the original loader when appropriate.
@@ -68,7 +83,7 @@ function hookedLoader(request, parent, isMain) {
if (registeredAllowables.hasOwnProperty(request)) {
allow = registeredAllowables[request];
if (allow.unhook) {
- file = m._resolveFilename(request, parent)[1];
+ file = resolveFilename(request, parent);
if (file.indexOf('/') !== -1 && allow.paths.indexOf(file) === -1) {
allow.paths.push(file);
}
View
2  package.json
@@ -8,7 +8,7 @@
],
"name": "mockery",
"description": "Simplifying the use of mocks with Node.js",
- "version": "1.1.1",
+ "version": "1.1.2",
"repository": {
"type": "git",
"url": "git://github.com/mfncooper/mockery.git"
View
112 test/mockery.functional.js
@@ -5,6 +5,7 @@
var testCase = require('nodeunit').testCase;
var mockery = require('../mockery');
var sinon = require('sinon');
+var m = require('module');
var mock_fake_module = {
foo: function () {
@@ -12,6 +13,15 @@ var mock_fake_module = {
}
};
+function isCached(name) {
+ // Super-simplistic, but good enough for the tests
+ for (var id in m._cache) {
+ if (id.indexOf(name) !== -1) return true;
+ }
+
+ return false;
+}
+
module.exports = testCase({
setUp: function (callback) {
callback();
@@ -82,6 +92,108 @@ module.exports = testCase({
})
}),
+ "when an allowable is registered": testCase({
+ setUp: function (callback) {
+ mockery.registerAllowable('./fixtures/fake_module');
+ callback();
+ },
+
+ "and mockery is enabled": testCase({
+ setUp: function (callback) {
+ mockery.enable();
+ callback();
+ },
+
+ "requiring the module causes no warning to be logged": function (test) {
+ var mock_console = sinon.mock(console);
+ mock_console.expects('warn').never();
+
+ var fake_module = require('./fixtures/fake_module');
+ test.equal(fake_module.foo(), 'real foo');
+
+ mock_console.verify();
+ mock_console.restore();
+ test.done();
+ },
+
+ "and the allowable is deregistered": testCase({
+ setUp: function (callback) {
+ mockery.deregisterAllowable('./fixtures/fake_module');
+ callback();
+ },
+
+ "requiring the module causes a warning to be logged": function (test) {
+ var mock_console = sinon.mock(console);
+ mock_console.expects('warn').once();
+
+ var fake_module = require('./fixtures/fake_module');
+ test.equal(fake_module.foo(), 'real foo');
+
+ mock_console.verify();
+ mock_console.restore();
+ test.done();
+ }
+ })
+ })
+ }),
+
+ "when an allowable is registered for unhooking": testCase({
+ setUp: function (callback) {
+ mockery.registerAllowable('./fixtures/fake_module', true);
+ callback();
+ },
+
+ "and mockery is enabled": testCase({
+ setUp: function (callback) {
+ if (!this.originalCache) {
+ // Initialise a clean cache
+ this.originalCache = m._cache;
+ m._cache = {};
+ }
+ mockery.enable();
+ callback();
+ },
+
+ tearDown: function (callback) {
+ if (this.originalCache) {
+ // Restore the original cache
+ m._cache = this.originalCache;
+ this.originalCache = null;
+ }
+ callback();
+ },
+
+ "the module is not cached": function (test) {
+ test.ok(!isCached('fixtures/fake_module'));
+ test.done();
+ },
+
+ "and the module is required": testCase({
+ setUp: function (callback) {
+ var fake_module = require('./fixtures/fake_module');
+ callback();
+ },
+
+ "the module is cached": function (test) {
+ test.ok(isCached('fixtures/fake_module'));
+ test.done();
+ },
+
+ "and the module is deregistered": testCase({
+ setUp: function (callback) {
+ mockery.deregisterAllowable('./fixtures/fake_module');
+ callback();
+ },
+
+ "the module is not cached": function (test) {
+ test.ok(!isCached('fixtures/fake_module'));
+ test.done();
+ }
+ })
+ }),
+ })
+ }),
+
"when a mock is registered": testCase({
setUp: function (callback) {
mockery.registerMock('./fixtures/fake_module', mock_fake_module);

No commit comments for this range

Something went wrong with that request. Please try again.