柴最终错误地通过
Chai eventually erroneously passing
为什么with表示的测试最终通过了?我显然在语法或用例中遗漏了一些东西。
const chai = require('chai')
const chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
const {expect} = chai
function p() {
return new Promise( (resolve) => {
setTimeout( () => {
resolve(10);
},5000);
})
}
describe('Chai Eventually test', () => {
it('should fail!', () => {
expect(p()).to.eventually.equal(5)
});
it('should also fail!', async () => {
expect(await p()).to.equal(5)
});
})
运行 它与 mocha
我得到:
Chai Eventually test
✔ should fail!
1) should also fail!
1 passing (2s)
1 failing
1) Chai Eventually test
should also fail!:
AssertionError: expected 10 to equal 5
+ expected - actual
-10
+5
at Context.<anonymous> (chaiError.js:19:26)
1 passing (4ms)
这会立即发生,因此不会等待 promise 解决。
这里有几个选项。
最简单的就是 return
expect
:
it('should fail!', () => {
return expect(p()).to.eventually.equal(5); // <-- return this expectation
});
或者,您可以跳过 return
并使用传递给 chai 的 .notify
方法的 done
回调:
it('should fail using done!', (done) => { // <-- pass done into spec function
expect(p()).to.eventually.equal(5).notify(done); // <-- pass done to .notify()
});
并且 here's a StackBlitz 显示它在这些更改下正常工作(即失败)。
为什么with表示的测试最终通过了?我显然在语法或用例中遗漏了一些东西。
const chai = require('chai')
const chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
const {expect} = chai
function p() {
return new Promise( (resolve) => {
setTimeout( () => {
resolve(10);
},5000);
})
}
describe('Chai Eventually test', () => {
it('should fail!', () => {
expect(p()).to.eventually.equal(5)
});
it('should also fail!', async () => {
expect(await p()).to.equal(5)
});
})
运行 它与 mocha
我得到:
Chai Eventually test
✔ should fail!
1) should also fail!
1 passing (2s)
1 failing
1) Chai Eventually test
should also fail!:
AssertionError: expected 10 to equal 5
+ expected - actual
-10
+5
at Context.<anonymous> (chaiError.js:19:26)
1 passing (4ms)
这会立即发生,因此不会等待 promise 解决。
这里有几个选项。
最简单的就是 return
expect
:
it('should fail!', () => {
return expect(p()).to.eventually.equal(5); // <-- return this expectation
});
或者,您可以跳过 return
并使用传递给 chai 的 .notify
方法的 done
回调:
it('should fail using done!', (done) => { // <-- pass done into spec function
expect(p()).to.eventually.equal(5).notify(done); // <-- pass done to .notify()
});
并且 here's a StackBlitz 显示它在这些更改下正常工作(即失败)。