I have function which return promise. here is the code of function.
function firstFunction () {
return new Promise (function (resolve , return) {
callApi(function(data, error){
if (data) resolve (data)
else if (error) reject (error)
})
})
}
as you can see it is calling some api and on result its returning data.
I have another function which call this function
function secondFunction () {
firstFunction ().then(function (data , error) {
return data
})
}
Now I want to write test case for the secondfunction . but not able to do this. I put logger also in the functions but seems not working, here is my test case
describe.only ("network_test" , function () {
it ("test 01", function (done){
secondFunction (function (data) {
console.log(data);
done()
})
})
})
To test promises you should return the promise in the mocha test and not use done
.
Your test should be something like:
describe.only('network_test', function () {
it('test 01', function () {
return secondFunction(function (data) {
console.log(data);
});
});
});
Update: Regarding your comment: check your error handlers, in firstFunction
reject
is undefinded
. in secondFunction
an error will make the promise to never resolve.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.