Reputation: 345
I am working on a node JS project for which I am writing test cases. Code -
jest.mock('../../utils/db2.js')
const request = require('supertest')
const executeDb2Query = require('../../utils/db2.js')
const app = require('../../app')
describe('check connect test', () => {
beforeEach(() => {
jest.resetModules()
})
it('check Bad Pool', async () => {
executeDb2Query.mockImplementation(() =>
Promise.reject(new Error('Unable to connect to db2'))
)
const response = await request(app).get(
'/api/v1/abc/xyz/123456'
)
expect(response).rejects.toMatch('Unable to connect to db2')
//expect(response.body.msg).toEqual('Unable to connect to db2')
expect(response.status).toEqual(500)
})
executeDb2query is a method which executes the query. The commented line is working fine but when I add rejects.toMatch , the test case fails saying below -
check connect test › check Bad Pool
expect(received).rejects.toMatch()
Matcher error: received value must be a promise or a function returning a promise
Received has type: object
Received has value: {"header": {"connection": "close", "content-length": "2", "content-security-policy-report-only"
)
> 24 | expect(response).rejects.toMatch('Unable to connect to db2')
| ^
25 |
26 | //expect(response.body.msg).toEqual('Unable to connect to db2')
27 | expect(response.status).toEqual(500)
at Object.toMatch (node_modules/expect/build/index.js:226:11)
For resolves case -
it('check Good Pool', () => {
executeDb2Query.mockImplementation(() =>
Promise.resolve([
{
isAvailable: true
}
])
)
const response = request(app).get('/api/v1/abc/xyz/090703')
expect(response).resolves.toStrictEqual([
{
isAvailable: true
}
])
})
Error -
expect(received).resolves.toStrictEqual()
Matcher error: received value must be a promise
Received has value: undefined
35 | )
36 | const response = request(app).get('/api/v1/abc/xyz/090703')
> 37 | expect(response.body).resolves.toStrictEqual([
| ^
38 | {
39 | isAvailable: true
40 | }
at Object.toStrictEqual (node_modules/expect/build/index.js:185:11)
On further checking, test cases are passing but giving out errors -
(node:23978) UnhandledPromiseRejectionWarning: Error: expect(received).rejects.toThrow()
Received promise resolved instead of rejected
Resolved to value: {"header": {"connection": "close", "content-length": "1499", "content-security-policy-report-only"
(node:23978) UnhandledPromiseRejectionWarning: Error: expect(received).resolves.toStrictEqual(expected) // deep equality
Expected: [{"isAvailable": true}]
Received: {"header": {"connection": "close", "content-length": "1499", "content-security-policy-report-only": "default-src 'self'"
I tried searching online and tried various things but keep getting the same error.
Any suggestions?
Upvotes: 1
Views: 8047
Reputation: 2448
expect(response).rejects
assumes response
to be a Promise. However, you're already using await
, so response
is not a Promise - it is the resolution value of that promise.
Remove the await
, or (if you're getting a response rather than a promise rejection) keep the await
but stop matching on .rejects.
If you decide to keep using expect(...).rejects
, you may need to await the assertion:
await expect(...).rejects.toMatch(...);
Upvotes: 5