Skip to content

Instantly share code, notes, and snippets.

@msteckyefantis
Last active February 26, 2018 22:53
Show Gist options
  • Save msteckyefantis/957591c55f9aa302e7ce2cd6061eef54 to your computer and use it in GitHub Desktop.
Save msteckyefantis/957591c55f9aa302e7ce2cd6061eef54 to your computer and use it in GitHub Desktop.
General nodejs unit test template. Uses mocha, sinon, proxyquire, chai, and is-deep-frozen.
'use strict';
const ROOT_PATH = '../../';
const MODULE_PATH = 'lib/filename';
const FULL_MODULE_PATH = ROOT_PATH + MODULE_PATH;
const isDeepFrozen = require( 'is-deep-frozen' );
const expect = require( 'chai' ).expect;
const proxyquire = require( 'proxyquire' ).noCallThru();
const sinon = require( 'sinon' );
describe( MODULE_PATH, function() {
let getMonkeyStub;
let feedMonkeyStub;
function getModule( values ) {
// s1
getMonkeyStub = sinon.stub().returns( values.getMonkey );
// s2
feedMonkeyStub = sinon.stub().returns( values.feedMonkey );
const proxyquireStubs = {
'../../get_monkey': getMonkeyStub,
'../../feed_monkey': feedMonkeyStub,
};
return proxyquire( FULL_MODULE_PATH, proxyquireStubs );
}
it( 'normal operation', function() {
const module = getModule({
getMonkey: Promise.resolve({
getMonkeyContents: true
}),
feedMonkey: {
feedMonkeyContents: true
}
});
expect( isDeepFrozen( module ) ).eql( {} ); // indicates is deeply frozen
// run module command(s) here
// s1
expect( getMonkeyStub.args.length ).equal( 0 );
// s2
expect( feedMonkeyStub.args.length ).equal( 0 );
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment