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/test-base.js
2016-02-19 12:58:16 -05:00

71 lines
1.8 KiB
JavaScript

'use strict';
const path = require('path');
// Set up chai as promised to allow for
// better testing of promises
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
// Load environment file
require('dotenv').config({
path: path.resolve(__dirname, '../.env'),
});
/**
* Base Object for unit test utilities
*/
const testBase = {
/**
* Chai expect assertion library
*/
expect: chai.expect,
/**
* 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;