0

Hey guys so I am currently working on this async function and writing unit test for this but it is not working it says AssertionError: expected undefined to equal 'Everytime I think of coding, I am happy' this is my code for the function:

async function funcFour() {// async automatically returns a promise
let result = new Promise((resolve) => {// returns a new promise named result
    setTimeout(() => {
        resolve(`Everytime I think of coding, I am happy`);//resolve happens after three seconds
    }, 1500)
});
const response = await result;//closing resolve of new promise
console.log(response);//console.logging response of new promise

} funcFour();

And here is my unit test for it:

describe("flirtFour()", () => {
it("Should return Everytime I think of coding, I am happy", async function () {
    return flirtFour().then(result => {
        expect(result).to.equal("Everytime I think of coding, I am happy")

    })
    

})

})

This is the first time I write unit test and I am trying to do it with async func so I am new to it. I really want to see how this is done so Thanks in advance :)

1

1 Answer 1

1

Although you have given funcFour() above and trying to test flirtFour() below, I am assuming they are the same. Now flirtFour() doesn't return anything as of now. You need to return the response from that function. By default, the returned value is undefined. Also remember that whatever you return from the async function gets wrapped into a Promise itself. So you're actually returning a Promise like so :-

return Promise.resolve(undefined).

If you simply return response that will automatically be treated as

return Promise.resolve(response)

which is probably what you need.

So change your funcFour() to the following :-

async function funcFour() {// async automatically returns a promise
let result = new Promise((resolve) => {// returns a new promise named result
    setTimeout(() => {
        resolve(`Everytime I think of coding, I am happy`);//resolve happens after three seconds
    }, 1500)
});
const response = await result;
return response;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.