dimanche 18 février 2018

Making a mocha test with async function.

I am trying to make a mocha test with an async functions, but always get the following error:

Error: Timeout of 15000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

Have tried it with done() but it did not work.

The Functions are right. If you execute them manually it works.

Code for functions that needs to be tested:

/**
 * Add short URL to the DB
 * @param  {String} longUrl original URl passed by the user
 * @returns {string} shortUrl generated from the longUrl
 */
ex.addShortUrl = async longUrl => {
  debug("adding short url with the longURL %s", longUrl);

  let p = new Promise(acc => {
    //generate short id for the shortened Link
    let id = shortId.generate();

    //create & save new link instance
    links.create({ originalUrl: longUrl, shortUrl: id }, (err, link) => {
      if (err) {
        debug("ERROR: %s ,creating a new short URl in the DB with the longURL %s", err, longUrl);
      };
      debug("created link with the shortUrl %s", link.shortUrl);
      acc(link.shortUrl);
    });
  });
  let short = await p;
  //return shortUrl
  return short;
};

Heres my test:

describe('UrlAPI', () => {
    //addShortUrl test 
    describe('addShortUrl()', () => {
        it('addShortUrl should return a string', async () => {
            let addShortUrlResult = await urlApi.addShortUrl("https://www.testweb.com");
            assert.typeOf(addShortUrlResult, "string");
        });
    });
});

Aucun commentaire:

Enregistrer un commentaire