如何通过管理承诺(异步和等待)绕过 5000 毫秒的 jest setTimeout 错误

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

我写了一个 Async/Await 函数来 return 承诺驱动程序报告和分析。 我有三个不同的 promise API 文件,我从中提取了详细信息以进行分析。但是运行开玩笑地测试我得到了错误 Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Error:

我在两天内重构了我的代码 3 次以上,但错误 returns.

我想知道如何管理我的承诺,也许有些地方做得不好,我热衷于优化。

有没有办法管理下面代码中的 promise 以绕过玩笑错误?

任何其他建议将不胜感激。 注意:抱歉,我有 post 所有代码以便更好地了解。

代码

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;

使用jest.setTimeout(30000);增加超时。它将全局增加超时。

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

或者您可以使用这样的用户测试示例

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