期望承诺解决或拒绝并没有正确地通过 mocha 和 chai-as-promised 的测试失败
expecting promised to resolve or reject doesn't properly fail a test with mocha and chai-as-promised
使用 Mocha and chai-as-promised,我正在尝试测试我的承诺是否得到正确解决和拒绝。但是 chai-as-promised 给出的 expect
函数没有正确地导致测试失败。示例:
test.js
const chai = require('chai')
chai.use(require('chai-as-promised'))
const expect = chai.expect
describe('foo', () => {
it('resolve expected', () => {
expect(new Promise((res,rej) => {res()})).to.be.fulfilled
})
it('resolve unexpected', () => {
expect(new Promise((res,rej) => {res()})).to.be.rejected
})
it('reject expected', () => {
expect(new Promise((res,rej) => {rej()})).to.be.rejected
})
it('reject unexpected', () => {
expect(new Promise((res,rej) => {rej()})).to.be.fulfilled
})
})
当我执行mocha test.js
:
foo
✓ resolve expected
✓ resolve unexpected
(node:2659) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): AssertionError: expected promise to be rejected but it was fulfilled with undefined
(node:2659) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
✓ reject expected
✓ reject unexpected
(node:2659) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 4): AssertionError: expected promise to be fulfilled but it was rejected with undefined
4 passing (10ms)
您可以看到断言错误似乎已被抛出,但 mocha 并未发现它们。我怎样才能让 mocha 识别失败?
正如我们确认的那样,为了将来参考,问题是您没有在每个测试中return断言。
it('reject expected', () => {
return expect(new Promise((res,rej) => {rej()})).to.be.rejected
})
it('reject unexpected', () => {
return expect(new Promise((res,rej) => {rej()})).to.be.fulfilled
})
这就是为什么测试通过了,但后来最终 return 打印了 "unhandled" promise。 return
关键字通知mocha等待异步函数到resolve/reject.
或者使用 chai-as-promised 库,您可以尝试使用 chai.should 并调用 .notify 来指示其 'done'。像这样:
const chai = require('chai');
chai.use(require('chai-as-promised'));
chai.should();
const expect = chai.expect;
describe('foo', () => {
it('resolve expected', () => {
expect(new Promise((res, rej) => { res(); })).to.be.fulfilled;
});
it('resolve unexpected', (done) => {
// expect(new Promise((res, rej) => { res(); })).to.be.rejected;
new Promise((res, rej) => { res(); }).should.be.rejected.and.notify(done);
});
it('reject expected', (done) => {
// expect(new Promise((res, rej) => { rej(); })).to.be.rejected;
new Promise((res, rej) => { rej(); }).should.be.rejected.and.notify(done);
});
it('reject unexpected', (done) => {
// expect(new Promise((res, rej) => { rej(); })).to.be.fulfilled;
new Promise((res, rej) => { rej(); }).should.be.fulfilled.and.notify(done);
});
});
这样 mocha 将识别测试失败:
foo
√ resolve expected
1) resolve unexpected
√ reject expected
2) reject unexpected
2 passing (37ms) 2 failing
1) foo
resolve unexpected:
AssertionError: expected promise to be rejected but it was fulfilled with undefined
2) foo
reject unexpected:
AssertionError: expected promise to be fulfilled but it was rejected with undefined
检查 chai-as-promised 文档以供进一步参考 here
it ('creates images', async () => {
assert.equal(imageCount,1)
const event = result.logs[0].args
assert.equal(event.id.toNumber(),imageCount.toNumber(),'id is correct')
assert.equal(event.hash,hash,'Hash is correct')
assert.equal(event.description,'Image Description','description is correct')
assert.equal(event.tipAmount,'0','tip amount is correct')
assert.equal(event.author, author, 'author is correct')
await decentragram.uploadImage('','Image description', { from: author }).should.be.rejected;
await decentragram.uploadImage('Image hash','', { from:author }).should.be.rejected;
})
面临同样的问题here.But同意[luke-stoward][1]
解决方案:添加return
it ('creates images', async () => {
return
assert.equal(imageCount,1)
const event = result.logs[0].args
assert.equal(event.id.toNumber(),imageCount.toNumber(),'id is correct')
assert.equal(event.hash,hash,'Hash is correct')
assert.equal(event.description,'Image Description','description is correct')
assert.equal(event.tipAmount,'0','tip amount is correct')
assert.equal(event.author, author, 'author is correct')
await decentragram.uploadImage('','Image description', { from: author }).should.be.rejected;
await decentragram.uploadImage('Image hash','', { from:author }).should.be.rejected;
})
使用 Mocha and chai-as-promised,我正在尝试测试我的承诺是否得到正确解决和拒绝。但是 chai-as-promised 给出的 expect
函数没有正确地导致测试失败。示例:
test.js
const chai = require('chai')
chai.use(require('chai-as-promised'))
const expect = chai.expect
describe('foo', () => {
it('resolve expected', () => {
expect(new Promise((res,rej) => {res()})).to.be.fulfilled
})
it('resolve unexpected', () => {
expect(new Promise((res,rej) => {res()})).to.be.rejected
})
it('reject expected', () => {
expect(new Promise((res,rej) => {rej()})).to.be.rejected
})
it('reject unexpected', () => {
expect(new Promise((res,rej) => {rej()})).to.be.fulfilled
})
})
当我执行mocha test.js
:
foo
✓ resolve expected
✓ resolve unexpected
(node:2659) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): AssertionError: expected promise to be rejected but it was fulfilled with undefined
(node:2659) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
✓ reject expected
✓ reject unexpected
(node:2659) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 4): AssertionError: expected promise to be fulfilled but it was rejected with undefined
4 passing (10ms)
您可以看到断言错误似乎已被抛出,但 mocha 并未发现它们。我怎样才能让 mocha 识别失败?
正如我们确认的那样,为了将来参考,问题是您没有在每个测试中return断言。
it('reject expected', () => {
return expect(new Promise((res,rej) => {rej()})).to.be.rejected
})
it('reject unexpected', () => {
return expect(new Promise((res,rej) => {rej()})).to.be.fulfilled
})
这就是为什么测试通过了,但后来最终 return 打印了 "unhandled" promise。 return
关键字通知mocha等待异步函数到resolve/reject.
或者使用 chai-as-promised 库,您可以尝试使用 chai.should 并调用 .notify 来指示其 'done'。像这样:
const chai = require('chai');
chai.use(require('chai-as-promised'));
chai.should();
const expect = chai.expect;
describe('foo', () => {
it('resolve expected', () => {
expect(new Promise((res, rej) => { res(); })).to.be.fulfilled;
});
it('resolve unexpected', (done) => {
// expect(new Promise((res, rej) => { res(); })).to.be.rejected;
new Promise((res, rej) => { res(); }).should.be.rejected.and.notify(done);
});
it('reject expected', (done) => {
// expect(new Promise((res, rej) => { rej(); })).to.be.rejected;
new Promise((res, rej) => { rej(); }).should.be.rejected.and.notify(done);
});
it('reject unexpected', (done) => {
// expect(new Promise((res, rej) => { rej(); })).to.be.fulfilled;
new Promise((res, rej) => { rej(); }).should.be.fulfilled.and.notify(done);
});
});
这样 mocha 将识别测试失败:
foo
√ resolve expected
1) resolve unexpected
√ reject expected
2) reject unexpected
2 passing (37ms) 2 failing
1) foo
resolve unexpected:
AssertionError: expected promise to be rejected but it was fulfilled with undefined
2) foo
reject unexpected:
AssertionError: expected promise to be fulfilled but it was rejected with undefined
检查 chai-as-promised 文档以供进一步参考 here
it ('creates images', async () => {
assert.equal(imageCount,1)
const event = result.logs[0].args
assert.equal(event.id.toNumber(),imageCount.toNumber(),'id is correct')
assert.equal(event.hash,hash,'Hash is correct')
assert.equal(event.description,'Image Description','description is correct')
assert.equal(event.tipAmount,'0','tip amount is correct')
assert.equal(event.author, author, 'author is correct')
await decentragram.uploadImage('','Image description', { from: author }).should.be.rejected;
await decentragram.uploadImage('Image hash','', { from:author }).should.be.rejected;
})
面临同样的问题here.But同意[luke-stoward][1]
解决方案:添加return
it ('creates images', async () => {
return
assert.equal(imageCount,1)
const event = result.logs[0].args
assert.equal(event.id.toNumber(),imageCount.toNumber(),'id is correct')
assert.equal(event.hash,hash,'Hash is correct')
assert.equal(event.description,'Image Description','description is correct')
assert.equal(event.tipAmount,'0','tip amount is correct')
assert.equal(event.author, author, 'author is correct')
await decentragram.uploadImage('','Image description', { from: author }).should.be.rejected;
await decentragram.uploadImage('Image hash','', { from:author }).should.be.rejected;
})