0

I have of couple JS modules, let's say A and B. In module A, I have a method which depends on another method imported from module B. Now how do I test with sinon.spy, whether the method from A triggers the method from B?

//ModuleA.js
import{ methodFromB } from "ModuleB.js";

function methodFromA (){
methodFromB();
}

export{
 methodFromA
}

//ModuleB.js
function methodFromB (){ 
 //doSomething
}

ModuleA.Spec.js

import sinon from 'sinon';
import { assert,expect } from "chai";
import * as modB from "ModuleB.js";

import { methodA } from '../js/ModuleA.js';


describe("ModuleA.js", function() {

beforeEach(function() {
    stubmethod = sinon.stub(modB, "methodB").returns("success");              
});

after(function() {

});

describe("#methodA", function() {
    it("Should call the method methodB", function() {
        expect(methodA()).to.deep.equal('success');
        expect(stubmethod.calledOnce).to.equal(true);
    });

});    

});

After trying to stub methodB, i get the error "expected undefined to deeply equal 'success'".

Thanks in advance.

Murali Nepalli
  • 1,588
  • 8
  • 17

2 Answers2

0

you should mock module B, and expect it to.have.been.called instead of spy from methodFromA

Vu Luu
  • 792
  • 5
  • 16
0

You stub the wrong function from module B. It is supposed to be methodFromB not methodB based on your source file.

describe("ModuleA.js", function () {

  beforeEach(function () {
    stubmethod = sinon.stub(modB, "methodFromB").returns("success"); // change to methodFromB
  });

  after(function () {
    stubmethod.restore(); // don't forget to restore
  });

  describe("#methodA", function () {
    it("Should call the method methodB", function () {
      expect(methodA()).to.deep.equal('success');
      expect(stubmethod.calledOnce).to.equal(true);
    });

  });
});
deerawan
  • 8,002
  • 5
  • 42
  • 51