Mocha: 获取错误“错误:解析方法被过度指定。指定回调 * 或 * 返回 Promise;不能同时进行。”

创建于 2017-03-15  ·  6评论  ·  资料来源: mochajs/mocha

我有一些简单的东西。

` it("should call promise", async (done) => {

    const testPromise = new Promise((resolve, reject) => {

        setTimeout(() => {

            resolve("Hello World!");

        }, 200);
    });


    const result = await testPromise;

    done();

}`

但我不断收到错误消息“错误:解析方法被过度指定。指定回调返回一个承诺;不能两者兼而有之。”。

如果我取出 done() 则测试超时。

配置文件
{ "compilerOptions": { "module": "commonjs", "target": "ES6", "noImplicitAny": false, "types": ["node", "mocha","supertest"], "typeRoots": [ "./node_modules/@types" ] } }
mocha.opts
--require ts-node/register --reporter dot --watch-extensions tsx,ts test/**/*.ts

最有用的评论

可爱的找到了如何修复它在函数参数和底部删除完成并且它有效。 一旦你把 done 放在函数参数中,它就会失控

所有6条评论

可爱的找到了如何修复它在函数参数和底部删除完成并且它有效。 一旦你把 done 放在函数参数中,它就会失控

还要记住, async函数会自动返回一个promise。

似乎只要你async ,正如@ScottFreeCode所说,它返回承诺,所以我们只是跳过调用done() 。 作品。

删除完成对我有用!

前:

image

之后(工作!!):

image

获取响应函数:

image

我不得不从函数参数中删除done和函数调用的done()

   before(async function (done) {
        user = new User({ ...});
        await user.save();
        done()
    });

   before(async function () {
        user = new User({ ...});
        await user.save();
    });

这些对我有用

我遇到了同样的问题,我需要用户完成,因为我需要测试我的承诺是否失败。 所以我抓住了它,然后调用完成,但收到此错误。

it("...", async function (done) {
  try {
    await asyncFung(...);
    done("The call should have failed")
  } catch (error) {
    if (error instanceof MyError) {
      done()
    } else {
      done(error)
    }
  }
});
此页面是否有帮助?
0 / 5 - 0 等级