Guest User

Untitled

a guest
Nov 18th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. 'use strict';
  2. const chai = require('chai');
  3. const sinon = require('sinon');
  4. const index = require('./index');
  5. const myModule = require('./myModule'); // Require normally the module to stub
  6.  
  7. const expect = chai.expect;
  8.  
  9. describe('test for sinon', () => {
  10. let stubMyModuleMyFunction;
  11.  
  12. beforeEach(() => {
  13. stubMyModuleMyFunction = sinon.stub(myModule, 'myFunction'); // Stub the function in beforeEach hook of mocha to avoid side effect between tests
  14. });
  15.  
  16. afterEach(() => {
  17. stubMyModuleMyFunction.restore(); // Always restore the stub in the afterEach hook
  18. });
  19.  
  20. it('should fail', (done) => {
  21. stubMyModuleMyFunction.yields(new Error('KO')); // Stub with sinon can either return something or call the first callback it receives (with yields)
  22. index.handler(payload, context, (err, data) => {
  23. try {
  24. expect(err.message).to.equal('myFunction failed with message : KO');
  25. expect(stubMyModuleMyFunction.called).to.be.true; // All sinon stub are also spies
  26. done();
  27. } catch (e) {
  28. done(e);
  29. }
  30. });
  31. });
  32. });
Add Comment
Please, Sign In to add comment