我有一个测试,等待一个异步函数,然后等待一个超时,然后等待另一个异步函数。
it('runs a snipe successfully', async () => {
const exitCode = await john.chat.sendMoneyInChat(channel.topicName, channel.name, "0.01", botUsername);
console.log('timeout?')
await timeout(3000);
console.log('timeout!');
console.log('running bal check')
let values;
const nbl = await croupier.checkWalletBalance(process.env.CROUPIER_RINGO_USERNAME);
expect(nbl).toEqual(123);
})
基于我的控制台。日志输出
afterAll
拆卸过程在安装完成后立即开始
timeout?
日志声明。换句话说,我不明白“
timeout!
“在控制台日志中,我看到afterAll teardown中的console.log语句。
有什么好处?
编辑:
多亏@Metalmi的帮助,我修复了一个bug。现在我的代码是:
it('runs a snipe successfully', async () => {
jest.useFakeTimers()
const exitCode = await john.chat.sendMoneyInChat(channel.topicName, channel.name, "0.01", botUsername);
console.log('timeout?')
jest.advanceTimersByTime(20000)
console.log('timeout.');
console.log('running bal check')
let values;
const nbl = await croupier.checkWalletBalance(process.env.CROUPIER_RINGO_USERNAME);
expect(nbl).toEqual(123);
});
以下是checkWalletBalance功能:
public checkWalletBalance(username: string): Promise<any> {
let balance: number = 0;
const self = this;
return new Promise(async (resolve) => {
try {
const acct = await self.bot1.wallet.lookup(username);
console.log("acct", acct);
const balances = await self.bot2.wallet.balances(acct.accountId);
console.log("balances", balances);
balances.forEach((acctDetail) => {
console.log(acctDetail.balance[0].amount);
balance += parseFloat(acctDetail.balance[0].amount);
});
resolve(balance);
} catch (e) {
console.log(e);
throw e;
}
});
}
我猜在Promise中使用异步函数会有一些问题?
笑话在控制台前就开始了。日志(“acct”,acct)发生在
checkWalletBalance
,所以还是有问题。