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.
crispy-train/test/test-base.js

71 lines
1.8 KiB
JavaScript
Raw Permalink Normal View History

2016-01-25 09:19:28 -05:00
'use strict';
const path = require('path');
2016-02-18 21:50:45 -05:00
// 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'),
});
2016-01-25 09:19:28 -05:00
/**
* Base Object for unit test utilities
*/
const testBase = {
/**
* Chai expect assertion library
*/
2016-02-18 21:50:45 -05:00
expect: chai.expect,
2016-01-25 09:19:28 -05:00
/**
* 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
*/
2016-02-18 21:50:45 -05:00
require(modulePath) {
2016-01-25 09:19:28 -05:00
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
*/
2016-02-18 21:50:45 -05:00
requireNoCache(modulePath) {
2016-01-25 09:19:28 -05:00
const includePath = testBase._normalizeIncludePath(modulePath);
delete require.cache[includePath];
return require(includePath);
},
};
module.exports = testBase;