feat: Add require-again to help with unit testing

This commit is contained in:
Blade Barringer
2016-05-06 12:05:06 -05:00
parent 887aa478ec
commit 192488cb02
4 changed files with 20 additions and 29 deletions

View File

@@ -149,6 +149,7 @@
"nock": "^2.17.0",
"phantomjs": "^1.9",
"protractor": "^3.1.1",
"require-again": "^1.0.1",
"rewire": "^2.3.3",
"rimraf": "^2.4.3",
"shelljs": "^0.5.3",

View File

@@ -3,6 +3,7 @@ import request from 'request';
import nconf from 'nconf';
import nodemailer from 'nodemailer';
import Q from 'q';
import requireAgain from 'require-again';
import logger from '../../../../../website/src/libs/api-v3/logger';
function getUser () {
@@ -34,10 +35,6 @@ function getUser () {
describe('emails', () => {
let pathToEmailLib = '../../../../../website/src/libs/api-v3/email';
beforeEach(() => {
delete require.cache[require.resolve(pathToEmailLib)];
});
describe('sendEmail', () => {
it('can send an email using the default transport', () => {
let sendMailSpy = sandbox.stub().returns(Q.defer().promise);
@@ -46,7 +43,7 @@ describe('emails', () => {
sendMail: sendMailSpy,
});
let attachEmail = require(pathToEmailLib);
let attachEmail = requireAgain(pathToEmailLib);
attachEmail.send();
expect(sendMailSpy).to.be.calledOnce;
});
@@ -60,7 +57,7 @@ describe('emails', () => {
});
sandbox.stub(logger, 'error');
let attachEmail = require(pathToEmailLib);
let attachEmail = requireAgain(pathToEmailLib);
attachEmail.send();
expect(sendMailSpy).to.be.calledOnce;
deferred.reject();
@@ -75,13 +72,13 @@ describe('emails', () => {
describe('getUserInfo', () => {
it('returns an empty object if no field request', () => {
let attachEmail = require(pathToEmailLib);
let attachEmail = requireAgain(pathToEmailLib);
let getUserInfo = attachEmail.getUserInfo;
expect(getUserInfo({}, [])).to.be.empty;
});
it('returns correct user data', () => {
let attachEmail = require(pathToEmailLib);
let attachEmail = requireAgain(pathToEmailLib);
let getUserInfo = attachEmail.getUserInfo;
let user = getUser();
let data = getUserInfo(user, ['name', 'email', '_id', 'canSend']);
@@ -93,7 +90,7 @@ describe('emails', () => {
});
it('returns correct user data [facebook users]', () => {
let attachEmail = require(pathToEmailLib);
let attachEmail = requireAgain(pathToEmailLib);
let getUserInfo = attachEmail.getUserInfo;
let user = getUser();
delete user.profile.name;
@@ -108,7 +105,7 @@ describe('emails', () => {
});
it('has fallbacks for missing data', () => {
let attachEmail = require(pathToEmailLib);
let attachEmail = requireAgain(pathToEmailLib);
let getUserInfo = attachEmail.getUserInfo;
let user = getUser();
delete user.profile.name;
@@ -135,7 +132,7 @@ describe('emails', () => {
it('can send a txn email to one recipient', () => {
sandbox.stub(nconf, 'get').withArgs('IS_PROD').returns(true);
let attachEmail = require(pathToEmailLib);
let attachEmail = requireAgain(pathToEmailLib);
let sendTxnEmail = attachEmail.sendTxn;
let emailType = 'an email type';
let mailingInfo = {
@@ -158,7 +155,7 @@ describe('emails', () => {
it('does not send email if address is missing', () => {
sandbox.stub(nconf, 'get').withArgs('IS_PROD').returns(true);
let attachEmail = require(pathToEmailLib);
let attachEmail = requireAgain(pathToEmailLib);
let sendTxnEmail = attachEmail.sendTxn;
let emailType = 'an email type';
let mailingInfo = {
@@ -172,7 +169,7 @@ describe('emails', () => {
it('uses getUserInfo in case of user data', () => {
sandbox.stub(nconf, 'get').withArgs('IS_PROD').returns(true);
let attachEmail = require(pathToEmailLib);
let attachEmail = requireAgain(pathToEmailLib);
let sendTxnEmail = attachEmail.sendTxn;
let emailType = 'an email type';
let mailingInfo = getUser();
@@ -190,7 +187,7 @@ describe('emails', () => {
it('sends email with some default variables', () => {
sandbox.stub(nconf, 'get').withArgs('IS_PROD').returns(true);
let attachEmail = require(pathToEmailLib);
let attachEmail = requireAgain(pathToEmailLib);
let sendTxnEmail = attachEmail.sendTxn;
let emailType = 'an email type';
let mailingInfo = {

View File

@@ -1,4 +1,5 @@
import winston from 'winston';
import requireAgain from 'require-again';
/* eslint-disable global-require */
describe('logger', () => {
@@ -7,8 +8,6 @@ describe('logger', () => {
let errorSpy;
beforeEach(() => {
delete require.cache[require.resolve(pathToLoggerLib)];
infoSpy = sandbox.stub();
errorSpy = sandbox.stub();
sandbox.stub(winston, 'Logger').returns({
@@ -22,7 +21,7 @@ describe('logger', () => {
});
it('info', () => {
let attachLogger = require(pathToLoggerLib);
let attachLogger = requireAgain(pathToLoggerLib);
attachLogger.info(1, 2, 3);
expect(infoSpy).to.be.calledOnce;
expect(infoSpy).to.be.calledWith(1, 2, 3);
@@ -30,14 +29,14 @@ describe('logger', () => {
describe('error', () => {
it('with custom arguments', () => {
let attachLogger = require(pathToLoggerLib);
let attachLogger = requireAgain(pathToLoggerLib);
attachLogger.error(1, 2, 3, 4);
expect(errorSpy).to.be.calledOnce;
expect(errorSpy).to.be.calledWith(1, 2, 3, 4);
});
it('with error', () => {
let attachLogger = require(pathToLoggerLib);
let attachLogger = requireAgain(pathToLoggerLib);
let errInstance = new Error('An error.');
attachLogger.error(errInstance, {
data: 1,

View File

@@ -6,6 +6,7 @@ import {
} from '../../../../helpers/api-unit.helper';
import analyticsService from '../../../../../website/src/libs/api-v3/analyticsService';
import nconf from 'nconf';
import requireAgain from 'require-again';
describe('analytics middleware', () => {
let res, req, next;
@@ -17,15 +18,8 @@ describe('analytics middleware', () => {
next = generateNext();
});
afterEach(() => {
// The nconf.get('IS_PROD') occurs when the file is required
// Since node caches IS_PROD, we have to delete it from the cache
// to test prod vs non-prod behaviors
delete require.cache[require.resolve(pathToAnalyticsMiddleware)];
});
it('attaches analytics object res.locals', () => {
let attachAnalytics = require(pathToAnalyticsMiddleware);
let attachAnalytics = requireAgain(pathToAnalyticsMiddleware);
attachAnalytics(req, res, next);
@@ -34,7 +28,7 @@ describe('analytics middleware', () => {
it('attaches stubbed methods for non-prod environments', () => {
sandbox.stub(nconf, 'get').withArgs('IS_PROD').returns(false);
let attachAnalytics = require(pathToAnalyticsMiddleware);
let attachAnalytics = requireAgain(pathToAnalyticsMiddleware);
attachAnalytics(req, res, next);
@@ -45,7 +39,7 @@ describe('analytics middleware', () => {
it('attaches real methods for prod environments', () => {
sandbox.stub(nconf, 'get').withArgs('IS_PROD').returns(true);
let attachAnalytics = require(pathToAnalyticsMiddleware);
let attachAnalytics = requireAgain(pathToAnalyticsMiddleware);
attachAnalytics(req, res, next);