Mocha.js 如何使用wait使用mocha测试异步代码

Mocha.js 如何使用wait使用mocha测试异步代码,mocha.js,Mocha.js,如何使用mocha测试异步代码?我想在摩卡咖啡中使用多个wait var assert=require('assert'); 异步函数callAsync1(){ //异步的东西 } 异步函数callAsync2(){ 返回true; } 描述(‘测试’、功能(){ 它('should resolve',async(done)=>{ 等待callAsync1(); 让res=wait callAsync2(); 断言。相等(res,true); 完成(); }); }); 这将产生以下错误:

如何使用mocha测试异步代码?我想在摩卡咖啡中使用多个
wait

var assert=require('assert');
异步函数callAsync1(){
//异步的东西
}
异步函数callAsync2(){
返回true;
}
描述(‘测试’、功能(){
它('should resolve',async(done)=>{
等待callAsync1();
让res=wait callAsync2();
断言。相等(res,true);
完成();
});
});
这将产生以下错误:

  1) test
       should resolve:
     Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.
      at Context.it (test.js:8:4)
如果我删除done(),我会得到:

您可以退还承诺: 摩卡咖啡;您只需
承诺返回
it()
。如果解决了,则测试通过,否则将失败

既然你能做到:

async function getFoo() {
  return 'foo'
}

describe('#getFoo', () => {
  it('resolves with foo', () => {
    return getFoo().then(result => {
      assert.equal(result, 'foo')
    })
  })
})
对于
it
,您不需要
done
async

如果您仍然坚持使用
async/await
: 无论哪种情况,都不要将
done
声明为参数 如果使用上述任何一种方法,则需要从代码中完全删除
done
。将
done
作为参数传递给
it()
会提示摩卡您打算最终调用它


done
方法仅用于测试基于回调或基于事件的代码

我想调用多个异步代码。这就是为什么我把
async
放在函数下面。此外,我想在打电话后做出断言,所以我不能返回承诺。我编辑了我的问题,明白了;编辑。不管怎样,当我说您不需要
done
,这意味着您甚至不应该在
it
回调中传递它。从代码中完全删除
done
。将
done
作为参数传递给Mocha,提示您最终打算调用它。
async function getFoo() {
  return 'foo'
}

describe('#getFoo', () => {
  it('resolves with foo', () => {
    return getFoo().then(result => {
      assert.equal(result, 'foo')
    })
  })
})
async function getFoo() {
  return 'foo'
}

describe('#getFoo', () => {
  it('returns foo', async () => {
    const result = await getFoo()
    assert.equal(result, 'foo')
  })
})