I'm getting this error AssertionError: expected [Function] to be a function
when I'm trying to whether a async function throws an error
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)
})
result:
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)
What did I do wrong?
I'm getting this error AssertionError: expected [Function] to be a function
when I'm trying to whether a async function throws an error
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)
})
result:
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)
What did I do wrong?
Share Improve this question asked Apr 14, 2017 at 13:23 Ivan LukasevychIvan Lukasevych 1831 gold badge1 silver badge9 bronze badges 1- github.com/chaijs/chai/issues/958 – robertklep Commented Apr 14, 2017 at 13:26
2 Answers
Reset to default 11This is a known issue that happens when you try to pass an async function.
To fix this issue, you can use Chai canary.
More information: https://github.com/chaijs/chai/issues/958
If you are using async function, you can also check if it's an AsyncFunction
in following way:
const myAsyncFunc = async () => {};
expect(myAsyncFunc).to.be.a('AsyncFunction');