我有一些简单的东西。
` 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 放在函数参数中,它就会失控
还要记住, async
函数会自动返回一个promise。
似乎只要你async
,正如@ScottFreeCode所说,它返回承诺,所以我们只是跳过调用done()
。 作品。
删除完成对我有用!
前:
之后(工作!!):
获取响应函数:
我不得不从函数参数中删除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)
}
}
});
最有用的评论
可爱的找到了如何修复它在函数参数和底部删除完成并且它有效。 一旦你把 done 放在函数参数中,它就会失控