Skip to content
This repository was archived by the owner on Apr 3, 2019. It is now read-only.

Commit 47389fa

Browse files
committed
chore(tests): remove weird mocking magic
1 parent 045c4d3 commit 47389fa

File tree

3 files changed

+13
-69
lines changed

3 files changed

+13
-69
lines changed

lib/db/mysql/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ const MysqlPatcher = require('mysql-patcher');
1111

1212
const config = require('../../config');
1313
const encrypt = require('../../encrypt');
14-
const logger = require('../../logging')('db.mysql');
1514
const P = require('../../promise');
1615
const Scope = require('../../scope');
1716
const unique = require('../../unique');
1817
const patch = require('./patch');
1918

2019
const MAX_TTL = config.get('expiration.accessToken');
2120

21+
// logger is not const to support mocking in the unit tests
22+
var logger = require('../../logging')('db.mysql');
2223

2324
function MysqlStore(options) {
2425
if (options.charset && options.charset !== 'UTF8_UNICODE_CI') {
@@ -87,6 +88,9 @@ function checkDbPatchLevel(patcher) {
8788
}
8889

8990
MysqlStore.connect = function mysqlConnect(options) {
91+
if (options.logger) {
92+
logger = options.logger;
93+
}
9094

9195
options.createDatabase = options.createSchema;
9296
options.dir = path.join(__dirname, 'patches');

test/db/mysql.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
const assert = require('insist');
66
const sinon = require('sinon');
7+
const proxyquire = require('proxyquire');
78
const mocks = require('../lib/mocks');
89

910
const modulePath = '../../lib/db/mysql';
@@ -15,9 +16,8 @@ var dependencies = mocks.require([
1516
{ path: 'mysql-patcher', ctor: function() { return instances.patcher; } },
1617
{ path: '../../config' },
1718
{ path: '../../encrypt' },
18-
{ path: '../../logging', ctor: function() { return instances.logger; } },
19-
{ path: '../../scope' },
20-
{ path: '../../unique', ctor: function() { return instances.scope; } },
19+
{ path: '../../scope', ctor: function() { return instances.scope; } },
20+
{ path: '../../unique' },
2121
{ path: './patch' }
2222
], modulePath, __dirname);
2323

@@ -50,14 +50,11 @@ describe('db/mysql:', function() {
5050
sandbox.spy(instances.logger, methodName);
5151
});
5252

53-
mocks.register(dependencies, modulePath, __dirname);
54-
55-
mysql = require(modulePath);
53+
mysql = proxyquire(modulePath, dependencies);
5654
});
5755

5856
afterEach(function() {
5957
sandbox.restore();
60-
mocks.deregister();
6158
});
6259

6360
it('exports a connect function', function() {
@@ -90,7 +87,7 @@ describe('db/mysql:', function() {
9087
var result;
9188

9289
beforeEach(function() {
93-
return mysql.connect({}).then(function(r) {
90+
return mysql.connect({ logger: instances.logger }).then(function(r) {
9491
result = r;
9592
});
9693
});
@@ -140,7 +137,7 @@ describe('db/mysql:', function() {
140137
describe('db patch level is bad:', function() {
141138
beforeEach(function() {
142139
instances.patcher.currentPatchLevel -= 2;
143-
return mysql.connect({});
140+
return mysql.connect({ logger: instances.logger });
144141
});
145142

146143
afterEach(function() {
@@ -174,7 +171,7 @@ describe('db/mysql:', function() {
174171
sandbox.stub(instances.patcher, 'readDbPatchLevel', function(callback) {
175172
callback('foo');
176173
});
177-
return mysql.connect({});
174+
return mysql.connect({ logger: instances.logger });
178175
});
179176

180177
it('called patcher.end', function() {

test/lib/mocks.js

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,9 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
const path = require('path');
6-
const proxyquire = require('proxyquire');
7-
const m = require('module');
8-
9-
var moduleCache;
106

117
module.exports = {
12-
require: requireDependencies,
13-
register: registerDependencies,
14-
deregister: deregisterDependencies
8+
require: requireDependencies
159
};
1610

1711
// `mocks.require`
@@ -58,54 +52,3 @@ function requireDependency(dependency, modulePath, basePath) {
5852
return require(localPath);
5953
}
6054

61-
// `mocks.register`
62-
//
63-
// Register mock dependencies, fixing paths as we go so that it works
64-
// with the blanket coverage tool (which rewrites require paths in the
65-
// instrumented code). You should call this function inside beforeEach.
66-
//
67-
// Expects three arguments; `dependencies`, `modulePath` and `basePath`.
68-
//
69-
// dependencies: An object, where keys are dependency paths and values
70-
// are mock objects. This argument is typically the return
71-
// value from `mocks.require`, modified by sinon for your
72-
// tests.
73-
// modulePath: The relative path to the module under test.
74-
// basePath: The base path, i.e. __dirname for the test itself.
75-
function registerDependencies(dependencies, modulePath, basePath) {
76-
var instrumentedDependencies = {};
77-
78-
clearModuleCache();
79-
80-
Object.keys(dependencies).forEach(function(dependencyPath) {
81-
var instrumentedPath = getInstrumentedPath(dependencyPath, modulePath, basePath);
82-
instrumentedDependencies[instrumentedPath] = dependencies[dependencyPath];
83-
});
84-
85-
proxyquire(modulePath, instrumentedDependencies);
86-
}
87-
88-
function clearModuleCache() {
89-
moduleCache = m._cache;
90-
m._cache = {};
91-
}
92-
93-
function getInstrumentedPath(dependencyPath, modulePath, basePath) {
94-
if (dependencyPath[0] !== '.') {
95-
return dependencyPath;
96-
}
97-
98-
return path.resolve(basePath, modulePath) + '/' + dependencyPath;
99-
}
100-
101-
// `mocks.deregister`
102-
//
103-
// Deregister mock dependencies. You should call this function
104-
// inside afterEach.
105-
function deregisterDependencies() {
106-
if (moduleCache) {
107-
m._cache = moduleCache;
108-
moduleCache = null;
109-
}
110-
}
111-

0 commit comments

Comments
 (0)