jeudi 19 novembre 2020

Async function returns undefined in test, but is fine in main program

I have this async function in Javascript that takes a link, checks if its valid (200) or invalid (404), it then returns true if its valid or false otherwise

Output

The function:

const fetch = require("node-fetch");
//processLink("https://www.youtube.com/").then(data => console.log(data));
callProcessLink();
async function callProcessLink() {
    const result = await processLink("https://www.youtube.com/");
    console.log("Result is: " + result);
}
async function processLink(link) {
    try {
        console.log("The link is: " + link);
        // get status of each link
        const response = await fetch(link, { method: "HEAD" });
        console.log("The response status is: " + response.status);
        console.log("The response is: " + response);
        let isGood = false;

        if (process.argv[3] == "--good") {
            // check for --good --bad
            if (response.status == 200) {
                // good
                console.log(
                    `${link} was good! status: ${response.status}`.green,
                );
                isGood = true;
            }
        } else if (process.argv[3] == "--bad") {
            // Here we don't care about the good ones so no need to worry about isGood
            if (response.status == 404 || response.status == 401) {
                // bad
                console.log(
                    `${link} was bad! status: ${response.status}`.red,
                );
            }
        } else {
            // all
            if (response.status == 200) {
                // good
                console.log(
                    `${link} was good! status: ${response.status}`,
                );
                isGood = true;
            } else if (response.status == 404 || response.status == 401) {
                // bad
                console.log(
                    `${link} was bad! status: ${response.status}`.red,
                );
            } else {
                // unknown
                console.log(
                    `${link} was unknown! status: ${response.status}`.gray,
                );
            }
        }
        return isGood;
    } catch (err) {
        console.log("The error thrown is: " + err);
    }
}

module.exports = { processLink };

I want to write a test for this function using jest and nock to test when a valid URL is passed. This is my test code:

test("Pass valid link (status 200) to processLink", async () => {
    const host = "https://www.youtube.com";
    const path = "/";
    nock(host).head(path).reply(200);
    const url = `${host}${path}`;
    console.log("Url is " + url);
    const result = await processLink(url); 
    expect(result).toBe(true);
});

However, the test fails. I did some logging in the function and for some reason when I call it via the test the result of fetch() is undefined. I read the jest documentation and it seems like I'm testing an async function correctly. I have no idea what's going on. Any help is greatly appreciated. Thank you! Output of test:

enter image description here

enter image description here

Aucun commentaire:

Enregistrer un commentaire