0

All I want is a dynamic unit test running on Linux/Debian.

I want to get an array of links from a website using "superagent" or whatever module can send a GET request, and then do some tests on each of these links (again sending GET request to each link) using "describe", "it".

I searched for loops in mocha & chai in stackoverflow but the answers assume you already have an array and want to test each of its values so you won't be needing to send GET request to get the array values before test.
my problem is, I don't even have that array to iterate over its values, the array should contain almost 100 links and first I need to get them by sending a GET request to a website (example.com), then I need to test content of each of these links.

test.js

var expect = require('chai').expect;
var request = require('request');
var superagent = require('superagent');
var cheerio = require('cheerio');
var uri = 'http://example.com';

function caller(schemaID){
    url = uri + schemaID;
    describe("schema "+schemaID, function schema() {
        it("available", function schemaLoader(done) {
            superagent.get(url)
            .end(function(err, res) {
                var $ = cheerio.load(res.res.text);

                expect(true).to.be.true;
                done();
            });
        });
    });
}

superagent.get(uri).end(function(err, res) {
    var $ = cheerio.load(res.res.text);
    var array = [];

    $('#tab-content1').find('a').each(function(){
        array.push($(this).attr("href"));
    })

    array.forEach(val => {
        caller(val);
    });
});

When I run this in terminal:

mocha test.js

it does nothing!

How would Mocha test all 100 links after getting list of links from superagent?

Brian Salehi
  • 414
  • 2
  • 10
  • 19
  • 1
    Possible [duplicate question](https://stackoverflow.com/questions/32848584/dynamically-running-mocha-tests) – Troopers Sep 13 '17 at 09:37
  • @Rhayene for better explanation I should have mentioned that there's a website in company I'm working with, containing a dynamic menu, each item in that menu is a link, I need to get those links first then check all images and tables in each of those linked webpages. I already wrote the static list of links and it's working perfectly but soon this menu will be larger. I need this dynamically running. – Brian Salehi Sep 13 '17 at 09:44
  • Possible duplicate of [Dynamically Running Mocha Tests](https://stackoverflow.com/questions/32848584/dynamically-running-mocha-tests) – Brian Salehi Sep 13 '17 at 11:50
  • @Rhayene yes, setup would be a better solution, integrated with the duplicate answer. thanks anyway – Brian Salehi Sep 13 '17 at 12:31

0 Answers0