Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const chai = require('chai');
- const sinon = require('sinon');
- const index = require('./index');
- const myModule = require('./myModule'); // Require normally the module to stub
- const expect = chai.expect;
- describe('test for sinon', () => {
- let stubMyModuleMyFunction;
- beforeEach(() => {
- stubMyModuleMyFunction = sinon.stub(myModule, 'myFunction'); // Stub the function in beforeEach hook of mocha to avoid side effect between tests
- });
- afterEach(() => {
- stubMyModuleMyFunction.restore(); // Always restore the stub in the afterEach hook
- });
- it('should fail', (done) => {
- stubMyModuleMyFunction.yields(new Error('KO')); // Stub with sinon can either return something or call the first callback it receives (with yields)
- index.handler(payload, context, (err, data) => {
- try {
- expect(err.message).to.equal('myFunction failed with message : KO');
- expect(stubMyModuleMyFunction.called).to.be.true; // All sinon stub are also spies
- done();
- } catch (e) {
- done(e);
- }
- });
- });
- });
Add Comment
Please, Sign In to add comment