eugef / node-mocks-http

Mock 'http' objects for testing Express routing functions
Other
753 stars 133 forks source link

Testing async middleware? #194

Closed dels07 closed 4 years ago

dels07 commented 5 years ago

Hi,

I have auth.handler middleware that call some async function that I having problem to test using mocha & chai, using done() not helping either here, example code:

middleware

async function authHandler(req, res, next) {
    const token = req.headers.authorization.split(' ')[1]
    const validCredential = await AuthService.verifyAccess(token)

    if (!validCredential) {
        next(new Error('Invalid credential')
    }

    next()
}

test

it('should return error', async () => {
    const token = 'invalid-token'
    const { req, res} = mockSetup(token)

    await authHandler(req, res, function next(error) => {
         expect(res.status).to.be.equal(500)
         expect(error.message).to.be.equal('invalid credential')
    })
})
github-actions[bot] commented 4 years ago

Stale issue message

chrise86 commented 4 years ago

@dels07 was your problem that the test was hanging? If so, I'm facing the same issue now - did you manage to resovle it?