在测试承诺拒绝时,使用摩卡与柴如承诺时会"is not a thenable"



提前感谢您对此的任何帮助!

努力测试承诺拒绝使用摩卡、柴和柴如承诺 我试图用async/await来完成这个,但无法成功,我决定去基础。

我希望至少有test 3人通过摩卡测试。我做错了什么?

我正在使用以下代码:

const chai = require('chai');
const chaiAsPromised = require("chai-as-promised")
chai.use(chaiAsPromised)
const should = chai.should()
describe.only('Testing promise tests', () => {
const errortest = () => Promise.reject('rejecting...')
it('test 1', () => {
Promise.reject('rejecting...').should.throw();
});
it('test 2', () => {
errortest.should.throw();
});
it('test 3', () => {
errortest.should.eventually.throw();
});
});

我的控制台输出:

0 passing (10ms)
3 failing
1) Testing promise tests
test 1:
AssertionError: expected {} to be a function
at Context.<anonymous> (test/promise.test.js:17:42)
at processImmediate (internal/timers.js:456:21)
2) Testing promise tests
test 2:
AssertionError: expected [Function: errortest] to throw an error
at Context.<anonymous> (test/promise.test.js:22:27)
at processImmediate (internal/timers.js:456:21)
3) Testing promise tests
test 3:
TypeError: [Function: errortest] is not a thenable.
at assertIsAboutPromise (node_modules/chai-as-promised/lib/chai-as-promised.js:31:19)
at Assertion.<anonymous> (node_modules/chai-as-promised/lib/chai-as-promised.js:53:13)
at Assertion.propertyGetter (node_modules/chai/lib/chai/utils/addProperty.js:62:29)
at Object.get (<anonymous>)
at Object.proxyGetter [as get] (node_modules/chai/lib/chai/utils/proxify.js:98:22)
at Context.<anonymous> (test/promise.test.js:26:22)
at processImmediate (internal/timers.js:456:21)

您需要调用errortest。现在你只使用函数对象,然后做.should.eventually.throw()就没有意义了。物体不会抛出;只有代码执行可以。

因此,添加括号:errortest().should.eventually.throw()

最新更新