【问题标题】:Chai: AssertionError: expected [Function] to be a function (when testing exceptions)Chai:AssertionError:预期 [Function] 是一个函数(测试异常时)
【发布时间】:2017-09-10 18:25:44
【问题描述】:

当我尝试判断异步函数是否引发错误时,我收到此错误 AssertionError: expected [Function] to be a function

it('has invalid password', async () => {
    const fakeData = { email: userData.email, password: 'something but not the password!.' }
    expect(async () => { await authService.authenticate(fakeData) }).to.throw(errors.UnauthenticatedError)
})

结果:

AssertionError: expected [Function] to be a function
  at Assertion.assertThrows (node_modules/chai/lib/chai/core/assertions.js:1273:32)
  at Assertion.ctx.(anonymous function) (node_modules/chai/lib/chai/utils/addMethod.js:41:25)
  at doAsserterAsyncAndAddThen (node_modules/chai-as-promised/lib/chai-as-promised.js:293:29)
  at Assertion.<anonymous> (node_modules/chai-as-promised/lib/chai-as-promised.js:252:17)
  at Assertion.ctx.(anonymous function) [as throw] (node_modules/chai/lib/chai/utils/overwriteMethod.js:49:33)
  at Context.it (dist/tests/unit/auth-service.spec.js:56:20)
  at Test._mocha2.default.Test.run (node_modules/mocha-http-detect/dist/index.js:84:21)

我做错了什么?

【问题讨论】:

标签: javascript node.js chai


【解决方案1】:

这是您尝试传递异步函数时发生的已知问题。

要解决此问题,您可以使用 Chai canary。

更多信息:https://github.com/chaijs/chai/issues/958

【讨论】:

  • 谢谢,这是个好主意。不幸的是,我无法更新到 chai@canary,因为 chai-as-promised 已被锁定以与 chai
  • @Kossgreim 尝试更改模块 package.json 中的 peerDependencies 字段?
【解决方案2】:

如果您使用的是异步功能,您还可以通过以下方式检查它是否为AsyncFunction

const myAsyncFunc = async () => {};

expect(myAsyncFunc).to.be.a('AsyncFunction');

【讨论】:

    猜你喜欢
    • 2019-01-23
    • 2012-04-05
    • 2015-08-09
    • 2011-07-31
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    相关资源
    最近更新 更多