Ugly progress commit

This commit is contained in:
Timothy Warren 2016-03-09 14:33:45 -05:00
parent 2b4cd0c3a6
commit edd7dd8492
9 changed files with 143 additions and 10 deletions

View File

@ -80,11 +80,37 @@ class Container {
return this._container.get(name); return this._container.get(name);
} }
try { return this._require(name);
return this._require(name); }
} catch (e) {
return;
} /**
* Get a base library instance
*
* @param {string} name - name of the base module
* @returns {*} - the base module
*/
getBase(name) {
return this.get(`base/${name}`);
}
/**
* Get a helper library instance
*
* @param {string} name - name of the helper module
* @return {*} - the helper instance
*/
getHelper(name) {
return this.get(`helpers/${name}`);
}
/**
* Get a model
* @param {string} name - the name of the model module
* @returns {*} - the model
*/
getModel(name) {
return this.get(`models/${name}`);
} }
/** /**

10
app/base/DBModel.js Normal file
View File

@ -0,0 +1,10 @@
'use strict';
const container = require('../Container');
const Model = container.getBase('Model');
class DBModel extends Model {
}
module.exports = DBModel;

4
app/models/User.js Normal file
View File

@ -0,0 +1,4 @@
'use strict';
const container = require('../Container');

View File

@ -219,7 +219,7 @@ gulp.task('coverage', ['lint', 'pre-coverage'], () => {
mocha(MOCHA_SETTINGS), mocha(MOCHA_SETTINGS),
istanbul.writeReports({ istanbul.writeReports({
dir: 'public/generated/coverage', dir: 'public/generated/coverage',
reporters:['lcov', 'lcovonly', 'html', 'text'], reporters:['clover', 'lcov', 'lcovonly', 'html', 'text'],
}), }),
]); ]);
}); });

View File

@ -44,6 +44,8 @@
"istanbul": "0.4.*", "istanbul": "0.4.*",
"mocha": "2.3.*", "mocha": "2.3.*",
"pre-commit": "^1.1.2", "pre-commit": "^1.1.2",
"sinon": "^1.17.3",
"sinon-chai": "^2.8.0",
"supertest": "^1.1.0" "supertest": "^1.1.0"
}, },
"engines": { "engines": {

View File

@ -1,4 +1,4 @@
test/**/*.js test/**/*_test.js
--ui tdd --ui tdd
--reporter spec --reporter spec
--slow 1000 --slow 1000

View File

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const testBase = require('../../test-base'); const testBase = require('../test-base');
const expect = testBase.expect; const expect = testBase.expect;
suite('Dependency Container tests', () => { suite('Dependency Container tests', () => {
@ -54,8 +54,9 @@ suite('Dependency Container tests', () => {
expect(actual).to.be.equal(obj); expect(actual).to.be.equal(obj);
}); });
test('Attempt to get non-existent item returns undefined', () => { test('Attempt to get non-existent item throws error', () => {
expect(container.get('aseiutj')).to.be.undefined; let fn = () => container.get('aseiutj');
expect(fn).to.throw(Error, `Cannot find module 'aseiutj'`);
}); });
}); });
@ -73,5 +74,19 @@ suite('Dependency Container tests', () => {
expect(containerFile).to.be.equal(nativeRequire); expect(containerFile).to.be.equal(nativeRequire);
}); });
test('getHelper(foo) returns same object as get(helpers/foo)', () => {
let expected = container.get('helpers/promisify');
let actual = container.getHelper('promisify');
expect(expected).to.deep.equal(actual);
});
test('getBase(foo) returns same object as get(base/foo)', () => {
let expected = container.get('base/Model');
let actual = container.getBase('Model');
expect(expected).to.deep.equal(actual);
});
}); });
}); });

76
test/unit/test-base.js Normal file
View File

@ -0,0 +1,76 @@
'use strict';
const path = require('path');
const sinon = require('sinon');
// Set up chai as promised to allow for
// better testing of promises,
// set up sinon-chai for better integration
// of sinon for spies, stubs and mocks
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
const sinonChai = require('sinon-chai');
chai.use(chaiAsPromised);
chai.use(sinonChai);
/**
* Base Object for unit test utilities
*/
const testBase = {
/**
* Chai expect assertion library
*/
expect: chai.expect,
/**
* Sinon library for spies, stubs, and mocks
*/
sinon: sinon,
/**
* Determine the appropriate path to a module relative to the root folder
*
* @param {String} modulePath - the raw path to the module
* @return {String} - the normalized path to the module
*/
_normalizeIncludePath(modulePath) {
const basePath = path.resolve(path.join(__dirname, '../'));
let includePath = modulePath;
// Allow referencing local modules without using a ./
// eg. util/route-loader instead of ./util/route-loader
if (modulePath.includes('/') && ! modulePath.startsWith('./')) {
includePath = path.join(basePath, modulePath);
}
return includePath;
},
/**
* Load a module relative to the root folder
*
* @param {String} modulePath - path to the module, relative to the tests/ folder
* @return {mixed} - whatever the module returns
*/
require(modulePath) {
const includePath = testBase._normalizeIncludePath(modulePath);
return require(includePath);
},
/**
* Load a module relative to the root folder, but first delete
* the module from the require cache
*
* @param {String} modulePath - path to the module, relative to the tests/ folder
* @return {mixed} - whatever the module returns
*/
requireNoCache(modulePath) {
const includePath = testBase._normalizeIncludePath(modulePath);
delete require.cache[includePath];
return require(includePath);
},
};
module.exports = testBase;