简体   繁体   English

在测试 promise 拒绝时,在将 Mocha 与 chai-as-promised 一起使用时获得“不是一个 thenable”

[英]When testing for promise rejections, getting “is not a thenable” when using Mocha with chai-as-promised

Thanks in advance for any assistance on this!在此先感谢您提供任何帮助!

Struggling testing promise rejections using Mocha, Chai, and Chai as Promised I tried to accomplish this with async/await, but was unable to succeed, I decided to go to basics.努力测试 promise 拒绝使用 Mocha、Chai 和 Chai 作为 Promised 我尝试使用 async/await 来完成此操作,但未能成功,我决定 go 到基础知识。

I expected at LEAST test 3 to pass the mocha test.我希望至少test 3能够通过 mocha 测试。 What am I doing wrong?我究竟做错了什么?

I'm using the following code:我正在使用以下代码:

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();
  });
});

My console output:我的控制台 output:

 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)

You need to invoke errortest .您需要调用errortest Right now you just use the function object, and then it makes no sense to do .should.eventually.throw() .现在你只使用 function object,然后再做.should.eventually.throw()就没有意义了。 An object does not throw; object 不抛出; only code execution can.只有代码执行可以。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM