Reputation: 338
I am writing Unit Testing which require me to hit the exceptions. Incase of promises, I cannot really send any boolean or string values. My intended output and the one I am getting is mentioned below.
function mainFunction(registry, name,....){
justAnotherPromiseFunction(name, function (err, twin) {
if (err) {
return "I want to return a failure message here!";
} else {
<MyLogic Goes Here>
}
});
}
console.log(mainFunction(registry, name,....));
This returns Promise { <rejected> [ArgumentError: The connection string is missing the property: Something] }
.
Instead of I want to return a failure message here!
.
Any ideas on how I can manage to do this?
Edit: I am implementing a function from azure sdk that uses promises. So looks like this:
function mainFunction(){
registry.getTwin(name, function (err, twin) {
if (err) {
return "I want to return a failure message here!";
} else {
<My Business Logic Goes Here>
}
});
}
console.log(mainFunction());
Upvotes: 1
Views: 159
Reputation: 895
You are using it in the wrong way. You cannot print the result of promise just by callint it. A promise return its result in the then function. Please see following examples.
async function mainFunction() {
try {
const result = await myPromise();
return result;
}
catch (err) {
return 'I want to return a failure message here!'
}
}
function myPromise() {
return new Promise(function(resolve, reject) {
})
}
it("test async function", async() => {
console.log(await mainFunction())
})
Make your test function async so you can await for the result.
Upvotes: 2