Search code examples
javascriptasync-awaitjestjses6-promiseecmascript-2016

How to bypass jest setTimeout error of 5000ms by managing promises (Async and Await)


I wrote an Async/Await function to return promises for drivers report and analysis. I have three different promise API files I extracted details from to do my analysis. However running test ith jest I get the error Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Error:

I have refactored my code more than three times in two days but the error returns.

I will like to know how to manage my promises, perhaps there is something am not doing well and I am keen on this for optimization.

Is there a way to manage the promises in the code below to bypass the jest error?

any other suggestion will be highly appreciated. NB: sorry I have post all the code for better insight.

code

const { getTrips } = require('api');
const { getDriver } = require('api')
const { getVehicle } = require('api')

/**
 * This function should return the data for drivers in the specified format
 *
 * Question 4
 *
 * @returns {any} Driver report data
 */

async function driverReport() {
  // Your code goes here
  let trip = await getTrips()
  trip = trip.map(item => {
    item.billedAmount = parseFloat(item.billedAmount.toString().replace(',', '')).toFixed(2);
    return item;
  })
  let getId = trip.reduce((user, cur) => {
    user[cur.driverID] ? user[cur.driverID] = user[cur.driverID] + 1 : user[cur.driverID] = 1
    return user
  }, {})
  // console.log(getId)

  let mapId = Object.keys(getId)
  // console.log(mapId)

  let eachTripSummary = mapId.reduce((acc, cur) => {
    let singleTrip = trip.filter(item => item.driverID == cur)
    acc.push(singleTrip)
    return acc
  }, [])
  // eachTripSummary = eachTripSummary[0]
  // console.log(eachTripSummary)

  // console.log(trip)
  let reducedReport = eachTripSummary.reduce(async(acc, cur) =>{
    
    acc = await acc
    // console.log(acc)
    let user = {}

    let cash = cur.filter(item => item.isCash == true)
    // console.log(cash.length)
    let nonCash = cur.filter(item => item.isCash == false)

    let driverSummary = await getDriverSummary(cur[0]['driverID'])
    let trips = []
    let customer = {}
    cur[0].user ? (customer['user'] = cur[0]['user']['name'], customer['created'] = cur[0]['created'], customer['pickup'] = cur[0]['pickup']['address'],
      customer['destination'] = cur[0]['destination']['address'], customer['billed'] = cur[0]['billedAmount'], customer['isCash'] = cur[0]['isCash']) : false
    trips.push(customer)

    let vehicles = []
    if(driverSummary == undefined){
      // console.log(cur)
      user = {
        id: cur[0]['driverID'],
        vehicles: vehicles,
        noOfCashTrips: cash.length,
        noOfNonCashTrips: nonCash.length,
        noOfTrips: cur.length,
        trips: trips
      }
      acc.push(user)
      // console.log(user)
      return acc
    }
    let driverInfo = driverSummary[0]
    let vehicleInfo = driverSummary[1]
    let { name, phone } = driverInfo
    let { plate, manufacturer } = vehicleInfo[0]
    // console.log(plate)
    let vpm = {
      plate,
      manufacturer
    }
    vehicles.push(vpm)
      // console.log(cash.length)
      user ={
        fulName: name,
        phone,
        id: cur[0]['driverID'],
        vehicles: vehicles,
        noOfCashTrips: cash.length,
        noOfNonCashTrips: nonCash.length,
        noOfTrips: cur.length, 
        trips: trips
      }
    
      acc.push(user)
      // console.log(acc)

    return acc
  }, [])
  // reducedReport.then(data =>{console.log(data)})
  return reducedReport

}

async function getDriverSummary(param) {

  let driverDetails = await getDriver(param)
  .then(data => {return data}).catch(err => {return err})
  // console.log(driverDetails)
  let vehicleDetails;
  let { vehicleID } = driverDetails
  if(driverDetails != "Error" & vehicleID != undefined){
    
    // console.log(vehicleID)
    vehicleDetails = vehicleID.map(async item => {
      let vehicleSummary = getVehicle(item)
      return vehicleSummary
    })
    // console.log(await vehicleDetails)
    
    return await Promise.all([driverDetails, vehicleDetails])

  }
}


driverReport().then(data => {
  console.log(data)
})




module.exports = driverReport;


Solution

  • Use jest.setTimeout(30000); to increase the timeout. It will increase the timeout globally.

    // jest.config.js
    module.exports = {
      setupTestFrameworkScriptFile: './jest.setup.js'
    }
    
    // jest.setup.js
    jest.setTimeout(30000)
    

    Or you can use user test example like this

    describe("...", () => {
        test(`...`, async () => {
            ...
        }, 30000);
    });