Mahesh
Mahesh

Reputation: 221

Unit test for function returning Promise

Hi I have following function returning promise

module.exports.getJwtToken = async() => {
    const httpSearchAddressUXConfig = {
      headers: {
        Accept: 'application/json',
        mock: false,
        'Content-Type': 'application/json',
      },
      data: reqBody,
      method: 'POST',
      url: `${config.app.entTokens.host}`, // need to get from env variables
      timeout: config.app.enterpriseHTTPTimeout
    };
    try {
      const token = await axios(httpSearchAddressUXConfig);
      return token.data;
    } catch (err) {
      throw err;
    }

I have following test case which fails with unhandled Promise rejection error

it('should find Jwt token ', async(done) => {

  const actualTokenfound = jwtTokenService.getJwtToken();
  return actualTokenfound
    .then(result => expect(result).toBe(Object))
    .then(done);

});

Any Suggestions ?

Upvotes: 1

Views: 1253

Answers (1)

Rafael Lima
Rafael Lima

Reputation: 518

If you define a async function, you don't need to use "done". I guess something like this it'll works.

it('should find Jwt token ', async () => {
  const actualTokenfound = await jwtTokenService.getJwtToken();
  expect(result).toBe(Object));
});

Upvotes: 2

Related Questions