【问题标题】:When testing for promise rejections, getting "is not a thenable" when using Mocha with chai-as-promised在测试承诺拒绝时,在将 Mocha 与 chai-as-promised 一起使用时获得“不是一个 thenable”
【发布时间】:2020-09-14 20:30:55
【问题描述】:

在此先感谢您提供任何帮助!

使用 Mocha、Chai 和 Chai as Promised 努力测试 Promise 拒绝 我尝试使用 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)

【问题讨论】:

    标签: javascript node.js promise mocha.js chai


    【解决方案1】:

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

    所以,添加括号:errortest().should.eventually.throw()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-28
      • 2018-06-01
      • 1970-01-01
      • 2014-07-22
      • 2015-11-23
      • 1970-01-01
      相关资源
      最近更新 更多