This repository has been archived on 2018-10-12. You can view files and clone it, but cannot push or open issues or pull requests.
ProgBlog/test/unit/test-base.js

76 lines
1.9 KiB
JavaScript

'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;