File: index.ts // Class Based Default exported
getItemsA() {
return Promise.resolve({ // Api Call in real scenario. Mocking here for now.
success: true;
result: [{
itemA: []
}]
});
}
getItemsB() {
return Promise.resolve({ // Api Call in real scenario. Mocking here for now.
success: true;
result: [{
itemB: []
}]
});
}
File service.ts
import { getItemsA, getItemsB } from 'index.ts';
getService() {
return new Promise(async (resolve, reject) => {
const parallelApis = await Promise.all([ // UT Stucks here. Not able to get the parallelApis response.
getItemsA(), getItemsB()
]);
.... other mapping
resolve({
.......
});
});
}
File test.ts
import items from 'index.ts'; // Class based. Default exported
import service from 'service.ts'; // Class based. Default exported
import { expect } from "chai";
import * as sinon from "sinon";
describe.only("Service", () => {
let itemAStub;
let itemBStub;
beforeEach(() => {
itemAStub = sinon.stub(items, "getItemsA");
itemBStub = sinon.stub(items, "getItemsB");
itemAStub.callsFake(() => {
return Promise.resolve({
body: myMock // Dummy
});
});
itemBStub.callsFake(() => {
return Promise.resolve({
body: someOtherMock // Dummy
});
});
});
afterEach(() => {
itemAStub.restore();
itemBStub.restore();
});
it("Should filter valid gift options", (done) => {
service.getService().then(res => {
console.log("RESPONSEEEEEE", res);
expect(res).to.deep.equal(myMockResponse);
done();
})
.catch(err => {
console.log("Errorr");
done(err);
});
});
});
Can somebody help me to identify the issue when i tried to run the test case I am getting the below error.
Error: Timeout of 10000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
I know that extending the timeout is not a solution to fix the issue. Stub is created, but not sure why it is getting timeout error when code reaches await Promise.all. If I remove done() then test case will succeed but then function is not getting executed. Any help would be really appreciated.
When we run Mocha from the command line, it will read this file which has a default timeout.
To change this time you can reset using:
./node_modules/.bin/mocha --timeout 20000