Search code examples
typescriptunit-testingchaisinonstub

Sinon in typescript stub


Sinon in typescript not able to import sub module propely ..please find below code The below code is file parent.ts

import submodule from './sub-module'
class Parent {

/**
 * name
 */
public parentmethod() {
   let sub = new submodule();
   let result = sub.submethod();
   return result;
}

}

export default Parent

and submodule code named as submodule.ts

class submodule{
public submethod(){

    return "hai submodule"
}

}
export default submodule

and unit test script as below

"use strict";
import chai from 'chai';
import sinon from "sinon";
import submodule from '../src/sub-module'
import  parentmodule from '../src/app'
const expect = chai.expect;
describe("test",function(){
    let stub;
    beforeEach(() => {        
       // stub = sinon.stub(sub ,'saveuser');
     });
    it("test methods",function(){
        stub= sinon.createStubInstance(submodule);
        let parentObj = new parentmodule();
        const user =  parentObj.parentmethod(); 
        expect(stub.submethod.calledOnce).to.be.true;
    })

})

The result is showing that expected false to be true. Means submethod is not calling here .Can any one help me where i went wrong


Solution

  • You should use Link Seams and proxyquire to stub ./sub-module module.

    E.g.

    parent.ts:

    import Submodule from './sub-module';
    
    class Parent {
      public parentmethod() {
        let sub = new Submodule();
        let result = sub.submethod();
        return result;
      }
    }
    
    export default Parent;
    

    sub-module.ts:

    class Submodule {
      public submethod() {
        return 'hai submodule';
      }
    }
    export default Submodule;
    

    parent.test.ts:

    import { expect } from 'chai';
    import sinon from 'sinon';
    import proxyquire from 'proxyquire';
    
    describe('61798668', () => {
      it('should pass', () => {
        const submoduleStub = {
          submethod: sinon.stub().returns('stubbed data'),
        };
        const SubmoduleStub = sinon.stub().returns(submoduleStub);
        const Parent = proxyquire('./parent', {
          './sub-module': {
            default: SubmoduleStub,
          },
        }).default;
        const parent = new Parent();
        const actual = parent.parentmethod();
        expect(actual).to.be.eq('stubbed data');
        sinon.assert.calledOnce(SubmoduleStub);
        sinon.assert.calledOnce(submoduleStub.submethod);
      });
    });
    

    unit test results with coverage report:

      61798668
        ✓ should pass (2797ms)
    
    
      1 passing (3s)
    
    ---------------|---------|----------|---------|---------|-------------------
    File           | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
    ---------------|---------|----------|---------|---------|-------------------
    All files      |   85.71 |      100 |      50 |   85.71 |                   
     parent.ts     |     100 |      100 |     100 |     100 |                   
     sub-module.ts |      50 |      100 |       0 |      50 | 3                 
    ---------------|---------|----------|---------|---------|-------------------