升级后,Mocha甚至无法运行这里的简单测试代码
const assert = require('assert');
it('should complete this test', function (done) {
return new Promise(function (resolve) {
assert.ok(true);
resolve();
})
.then(done);
});
Run Code Online (Sandbox Code Playgroud)
我从这里拿了这个代码
我明白它现在抛出异常 Error: Resolution method is overspecified. Specify a callback * or * return a Promise; not both.
但是如何让它发挥作用?我不明白.我有
node -v 6.9.4
mocha -v 3.2.0
Run Code Online (Sandbox Code Playgroud)
如何运行此代码现在采用新的正确格式?
Igo*_*gor 35
刚落,
.then(done);
并更换function(done)
有function()
你正在返回一个Promise,所以调用done是多余的,因为它在错误消息中说
在老版本中,你必须使用回调,以防异常方法
it ('returns async', function(done) {
callAsync()
.then(function(result) {
assert.ok(result);
done();
});
})
Run Code Online (Sandbox Code Playgroud)
现在您可以选择返回Promise
it ('returns async', function() {
return new Promise(function (resolve) {
callAsync()
.then(function(result) {
assert.ok(result);
resolve();
});
});
})
Run Code Online (Sandbox Code Playgroud)
但使用两者都是误导性的(例如参见https://github.com/mochajs/mocha/issues/2407)
Mocha允许使用回调:
it('should complete this test', function (done) {
new Promise(function (resolve) {
assert.ok(true);
resolve();
})
.then(done);
});
Run Code Online (Sandbox Code Playgroud)
或返回承诺:
it('should complete this test', function () {
return new Promise(function (resolve) {
assert.ok(true);
resolve();
});
});
// Or in the async manner
it('should complete this test', async () => {
await Promise.resolve();
assert.ok(true);
});
Run Code Online (Sandbox Code Playgroud)
你不能两者都做。
我不得不done
从函数参数和done()
函数调用
之前删除
before(async function (done) {
user = new User({ ...});
await user.save();
done()
});
Run Code Online (Sandbox Code Playgroud)
后
before(async function () {
user = new User({ ...});
await user.save();
});
Run Code Online (Sandbox Code Playgroud)
这些对我有用
归档时间: |
|
查看次数: |
8792 次 |
最近记录: |