lundi 7 août 2017

Promises not running in mocha test

Promise not being called in mocha test

Hi - I am writing a mocha test that tests a method that calls a promise. However, the actual code calling the promise never runs. Here is the method I am trying to test:

static callPromise () {
  let opts = {
    method: 'get',
    url: 'some_url'
  }

  return requestPkg.callRoute(opts).then(result => {
    return result
  }).catch(err => {
    return err
  })
}

Here is my method to execute HTTP requests:

import request from 'request'

static callRoute(requestOpts) {
  const { method, url, headers, body, qs } = opts
  return new Promise((resolve, reject) => {
    let requestOpts = {
        method: method,
        url: url,
        headers: headers,
        body: body,
        qs: qs
    }
    // the lines below this never get executed for some reason...
    request(requestOpts, (err, response, body) => {
      err ? reject(err) : resolve(body)
    })
  })
}

And here is my actual test:

describe('Promise test', () => {
  it('should return data from promise', () => {
    Instance.callPromise().then(result => {
    console.log('--result--', result) // this does not get called
      expect(result.status).to.equal(200)
    })
  });
});

Does anyone know what I might be doing wrong? Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire