jeudi 29 septembre 2016

How can I write the unit test with rewire or mocha for following nodejs code in effective manner?

I have read some tuts related to nodejs testing mostly with rewire and mocha but unfortunately cannot understand how to start.. So please help me by giving some examples for following code snippet and help me to write my first tests... Thanks in advance .

let request = require("request");
let cheerio = require("cheerio");

function scrapUris(uri, selector, cb, baseUri) {
let rUris = [];

request(uri, function(error, response, body) {

    if (body) {
        let $ = cheerio.load(body);
        $(selector).map((index, elem) => {
            let href = $(elem).attr('href')
                // rUris.push(baseUri + href)
            baseUri ? rUris.push(baseUri + href) : rUris.push(href);
        })

        cb(rUris)
    } else if (error) {
        console.log("Error Occurred In HTML Parsing :", error)
        console.log("Requested URI :", uri)
        console.log("Received content :", body)
    } else {
        console.log("Requested URI :", uri)
        console.log("Received content :", body)
    }
})
}

module.exports = scrapUris;

Aucun commentaire:

Enregistrer un commentaire