gpt4 book ai didi

javascript - 如何通过管理 Promise(异步和等待)来绕过 5000ms 的 jest setTimeout 错误

转载 作者:行者123 更新时间:2023-12-02 22:46:40 26 4
gpt4 key购买 nike

我编写了一个 Async/Await 函数来返回驱动程序报告和分析的 promise 。我从三个不同的 Promise API 文件中提取了详细信息来进行分析。但是运行测试时我得到了错误 超时 - 在 jest.setTimeout.Error 指定的 5000 毫秒超时内未调用异步回调:

我在两天内重构了我的代码超过 3 次,但错误仍然存​​在。

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

有没有办法管理下面代码中的 promise 来绕过 Jest 错误?

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

代码

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

关于javascript - 如何通过管理 Promise(异步和等待)来绕过 5000ms 的 jest setTimeout 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58374299/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com