jeudi 28 juillet 2016

Unit Testing functions in javascript which returns a Promise Object using mocha

Hello I'm trying to test a Javascript function which returns a Promise object. My Javascript function is ISession.getInstance = function () { return new Promise(function(resolve, reject) {

    var instance;
    var storedObject = JSON.parse(localStorage.getItem(HM_APP_KEY + "_stored_session"));

    if(storedObject)
    {
        instance = new ISession(storedObject);
        resolve(instance);
    }
    else
    {
        if(!HM_APP_KEY)
            reject("App Key is missing in the global vars file");
        if(!HM_APP_SECRET)
            reject("App Secret is missing in the global vars file");
        if(!HM_APP_URL)
            reject("App URL is missing in the global vars file");

        instance = new ISession();

        instance.verify().then(
            function(response)
            {
                localStorage.setItem(HM_APP_KEY + "_stored_session" , JSON.stringify(instance));
                resolve(instance);
            },
            function(response)
            {
                reject(response);
            });
    }
});

};

when i test it from CLI using mocha-phantomjs ./TestRunner.html I get a error as ReferenceError: Can't find variable: Promise

Can anyone guide me on how to go about this or suggest me other implementations. I need this unit tests to run from CLI and not in a browser as i'll be automating them on a Jenkins job later.

Thanks in advanced.

Aucun commentaire:

Enregistrer un commentaire