Skip to content

Commit

Permalink
Sandbox the tests: move from sinon.stub to sandbox (#59)
Browse files Browse the repository at this point in the history
Using `sandbox` rather than `sinon` simplifies the `afterEach` and is also common to our application practices at Condé Nast. Simplifying this small part of the testing setup and teardown process also likely encourages more tests!
  • Loading branch information
stripethree authored Apr 28, 2017
1 parent 94fb778 commit 3743c91
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 35 deletions.
39 changes: 10 additions & 29 deletions test/app.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ chai.use(chaiHttp);

describe('app', () => {
let messenger;
let sandbox;
let session;

before(() => {
messenger = new Messenger();
});

beforeEach(() => {
sinon.stub(messenger, 'pageSend').resolves({});
messenger = new Messenger();
sandbox = sinon.sandbox.create();
sandbox.stub(messenger, 'pageSend').resolves({});
sandbox.stub(messenger.app, 'listen');
session = {
profile: {
first_name: ' Guy ',
Expand All @@ -30,7 +30,7 @@ describe('app', () => {

afterEach(() => {
// TODO investigate making the suite mock `reqPromise.post` instead of `send`
messenger.pageSend.restore && messenger.pageSend.restore();
sandbox.restore();
});

describe('Response', () => {
Expand Down Expand Up @@ -124,14 +124,6 @@ describe('app', () => {
});

describe('start', () => {
beforeEach(() => {
sinon.stub(messenger.app, 'listen');
});

afterEach(() => {
messenger.app.listen.restore();
});

it('emits a "starting" event', (done) => {
messenger.once('app.starting', (payload) => {
assert.ok(payload.port);
Expand Down Expand Up @@ -388,7 +380,7 @@ describe('app', () => {

it('emits "greeting" event when provided a pattern', () => {
const myMessenger = new Messenger({emitGreetings: /^olleh/i});
sinon.stub(myMessenger, 'send');
sandbox.stub(myMessenger, 'send');

const text = "olleh, it's just olleh, backwards";
const event = Object.assign({}, baseEvent, { message: { text: text } });
Expand All @@ -406,7 +398,7 @@ describe('app', () => {

it('emits "text" event for greeting when emitGreetings is disabled', () => {
const myMessenger = new Messenger({emitGreetings: false});
sinon.stub(myMessenger, 'send');
sandbox.stub(myMessenger, 'send');

const text = "hello, is it me you're looking for?";
const event = Object.assign({}, baseEvent, {
Expand Down Expand Up @@ -536,15 +528,9 @@ describe('app', () => {
});

describe('send', function () {
let postStub;

beforeEach(() => {
messenger.pageSend.restore();
postStub = sinon.stub(reqPromise, 'post').resolves({});
});

afterEach(() => {
postStub.restore();
sandbox.stub(reqPromise, 'post').resolves({});
});

it('throws if messenger is missing page configuration', () => {
Expand Down Expand Up @@ -632,11 +618,7 @@ describe('app', () => {

beforeEach(() => {
messenger = new Messenger({cache: new Cacheman('test')});
sinon.stub(messenger, 'getPublicProfile').resolves({});
});

afterEach(() => {
messenger.getPublicProfile.restore();
sandbox.stub(messenger, 'getPublicProfile').resolves({});
});

it('uses default session if cache returns falsey', () => {
Expand All @@ -649,7 +631,6 @@ describe('app', () => {
}
};
messenger = new Messenger({cache: nullCache});
sinon.stub(messenger, 'getPublicProfile').resolves({});

return messenger.routeEachMessage(baseMessage)
.then((session) => {
Expand Down
6 changes: 4 additions & 2 deletions test/conversationLogger.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ const { ConversationLogger } = require('../src/conversationLogger');
const config = require('config').get('launch-vehicle-fbm');

describe('conversationLogger', () => {
let sandbox;
const conversationLogger = new ConversationLogger(config);
const logger = conversationLogger.logger; // shorter alias

beforeEach(() => {
sinon.stub(logger, 'info');
sandbox = sinon.sandbox.create();
sandbox.stub(logger, 'info');
});

afterEach(() => {
logger.info.restore();
sandbox.restore();
});

describe('logIncoming', () => {
Expand Down
1 change: 0 additions & 1 deletion test/main.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const assert = require('assert');

const main = require('..');


describe('main', () => {
it('publicly exposes things', () => {
assert.equal(typeof main.Messenger, 'function');
Expand Down
9 changes: 6 additions & 3 deletions test/responses.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ const Text = responses.Text;

describe('Responses', () => {
let originalDictionary;
let sandbox;

before(() => {
beforeEach(() => {
originalDictionary = responses._dictionary;
sandbox = sinon.sandbox.create();
});

after(() => {
afterEach(() => {
responses._dictionary = originalDictionary;
sandbox.restore();
});

describe('dictionary', () => {
Expand All @@ -27,7 +30,7 @@ describe('Responses', () => {
it('loads a dictionary', () => {
const responsesRef = Object.keys(require.cache).find((x) => x.endsWith('/src/responses.js'));
delete require.cache[responsesRef];
sinon.stub(appRootDir, 'get').returns(path.resolve(path.join(__dirname, './fixtures')));
sandbox.stub(appRootDir, 'get').returns(path.resolve(path.join(__dirname, './fixtures')));

const dictionary = require('../src/responses')._dictionary;

Expand Down

0 comments on commit 3743c91

Please sign in to comment.