Hi, I'm trying to do a test in a class written in ES6. The problem arises when I want to do a stub on a function of a module that I import.
the code I have is the following:
module.js
function soapModule(){
this.callSoap = (id) => {
....//some code
return new Promise((resolve,reject) =>{
return resolve("whatever");
}
}
}
index.js (this is the index of the module)
"use strict";
var soapModule = require('./module/module');
module.exports.soapModule = soapModule;
my-class.js
import {soapModule} from "soap-client"
export default class MyClass {
constructor(){
console.log("instance created");
}
myMethod(id){
let sm = new soapModule();
return sm.callSoap(id)
.then(result => {
console.log(result);
}).catch(e => {
console.log("Error :" + e);
})
}
}
test.js
import MyClass from "../src/my-class";
import {soapModule} from "soap-client";
import sinon from 'sinon';
describe("Test MyClass",()=>{
let myclass;
let soap;
let stub;
before(()=>{
myclass = new MyClass();
soap = new soapModule();
stub = sinon.stub(soap,'callSoap');
});
it("test a", ()=>{
let fakeout = {
resp : "tada!"
}
stub.resolves(fakeout);
myclass.myMethod(1);
});
});
I think I'm not generating the stub well, since I can not get coverage of the code that is inside the .then