Self-answer
I found the problem why the test failing.
forEach()
method should not be used with async/await
.
so I just changed the code using Promise.all()
and map()
await Promise.all(
covidStatus.map(async (status) => {
const exist = await covidRepository.findOne({
where: { city: status.city },
});
if (!exist) {
const newData = covidRepository.create({
city: status.city,
totalCases: status.totalCases,
increasedCases: status.increasedCases,
date: $standardTime,
});
return covidRepository.save(newData);
} else {
return covidRepository.save([
{
id: exist.id,
totalCases: status.totalCases,
increasedCases: status.increasedCases,
date: $standardTime,
},
]);
}
})
);
Or
for (const status of covidStatus) {
const exist = await covidRepository.findOne({
where: { city: status.city },
});
if (!exist) {
const newData = covidRepository.create({
city: status.city,
totalCases: status.totalCases,
increasedCases: status.increasedCases,
date: $standardTime,
});
return covidRepository.save(newData);
} else {
return covidRepository.save([
{
id: exist.id,
totalCases: status.totalCases,
increasedCases: status.increasedCases,
date: $standardTime,
},
]);
}
}
I found this solution from Here.
using forEach and async/await, behaves different for node and Jest
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…