简体   繁体   中英

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 ?

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));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM