Permalink
Browse files

chore(tests): remove weird mocking magic

  • Loading branch information...
1 parent 045c4d3 commit 47389fac209967ce3f3c3315dca2bf20d2ebe2b5 @philbooth philbooth committed Sep 24, 2015
Showing with 13 additions and 69 deletions.
  1. +5 −1 lib/db/mysql/index.js
  2. +7 −10 test/db/mysql.js
  3. +1 −58 test/lib/mocks.js
@@ -11,14 +11,15 @@ const MysqlPatcher = require('mysql-patcher');
const config = require('../../config');
const encrypt = require('../../encrypt');
-const logger = require('../../logging')('db.mysql');
const P = require('../../promise');
const Scope = require('../../scope');
const unique = require('../../unique');
const patch = require('./patch');
const MAX_TTL = config.get('expiration.accessToken');
+// logger is not const to support mocking in the unit tests
+var logger = require('../../logging')('db.mysql');
function MysqlStore(options) {
if (options.charset && options.charset !== 'UTF8_UNICODE_CI') {
@@ -87,6 +88,9 @@ function checkDbPatchLevel(patcher) {
}
MysqlStore.connect = function mysqlConnect(options) {
+ if (options.logger) {
+ logger = options.logger;
+ }
options.createDatabase = options.createSchema;
options.dir = path.join(__dirname, 'patches');
View
@@ -4,6 +4,7 @@
const assert = require('insist');
const sinon = require('sinon');
+const proxyquire = require('proxyquire');
const mocks = require('../lib/mocks');
const modulePath = '../../lib/db/mysql';
@@ -15,9 +16,8 @@ var dependencies = mocks.require([
{ path: 'mysql-patcher', ctor: function() { return instances.patcher; } },
{ path: '../../config' },
{ path: '../../encrypt' },
- { path: '../../logging', ctor: function() { return instances.logger; } },
- { path: '../../scope' },
- { path: '../../unique', ctor: function() { return instances.scope; } },
+ { path: '../../scope', ctor: function() { return instances.scope; } },
+ { path: '../../unique' },
{ path: './patch' }
], modulePath, __dirname);
@@ -50,14 +50,11 @@ describe('db/mysql:', function() {
sandbox.spy(instances.logger, methodName);
});
- mocks.register(dependencies, modulePath, __dirname);
-
- mysql = require(modulePath);
+ mysql = proxyquire(modulePath, dependencies);
});
afterEach(function() {
sandbox.restore();
- mocks.deregister();
});
it('exports a connect function', function() {
@@ -90,7 +87,7 @@ describe('db/mysql:', function() {
var result;
beforeEach(function() {
- return mysql.connect({}).then(function(r) {
+ return mysql.connect({ logger: instances.logger }).then(function(r) {
result = r;
});
});
@@ -140,7 +137,7 @@ describe('db/mysql:', function() {
describe('db patch level is bad:', function() {
beforeEach(function() {
instances.patcher.currentPatchLevel -= 2;
- return mysql.connect({});
+ return mysql.connect({ logger: instances.logger });
});
afterEach(function() {
@@ -174,7 +171,7 @@ describe('db/mysql:', function() {
sandbox.stub(instances.patcher, 'readDbPatchLevel', function(callback) {
callback('foo');
});
- return mysql.connect({});
+ return mysql.connect({ logger: instances.logger });
});
it('called patcher.end', function() {
View
@@ -3,15 +3,9 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const path = require('path');
-const proxyquire = require('proxyquire');
-const m = require('module');
-
-var moduleCache;
module.exports = {
- require: requireDependencies,
- register: registerDependencies,
- deregister: deregisterDependencies
+ require: requireDependencies
};
// `mocks.require`
@@ -58,54 +52,3 @@ function requireDependency(dependency, modulePath, basePath) {
return require(localPath);
}
-// `mocks.register`
-//
-// Register mock dependencies, fixing paths as we go so that it works
-// with the blanket coverage tool (which rewrites require paths in the
-// instrumented code). You should call this function inside beforeEach.
-//
-// Expects three arguments; `dependencies`, `modulePath` and `basePath`.
-//
-// dependencies: An object, where keys are dependency paths and values
-// are mock objects. This argument is typically the return
-// value from `mocks.require`, modified by sinon for your
-// tests.
-// modulePath: The relative path to the module under test.
-// basePath: The base path, i.e. __dirname for the test itself.
-function registerDependencies(dependencies, modulePath, basePath) {
- var instrumentedDependencies = {};
-
- clearModuleCache();
-
- Object.keys(dependencies).forEach(function(dependencyPath) {
- var instrumentedPath = getInstrumentedPath(dependencyPath, modulePath, basePath);
- instrumentedDependencies[instrumentedPath] = dependencies[dependencyPath];
- });
-
- proxyquire(modulePath, instrumentedDependencies);
-}
-
-function clearModuleCache() {
- moduleCache = m._cache;
- m._cache = {};
-}
-
-function getInstrumentedPath(dependencyPath, modulePath, basePath) {
- if (dependencyPath[0] !== '.') {
- return dependencyPath;
- }
-
- return path.resolve(basePath, modulePath) + '/' + dependencyPath;
-}
-
-// `mocks.deregister`
-//
-// Deregister mock dependencies. You should call this function
-// inside afterEach.
-function deregisterDependencies() {
- if (moduleCache) {
- m._cache = moduleCache;
- moduleCache = null;
- }
-}
-

0 comments on commit 47389fa

Please sign in to comment.