如何处理 Jest 测试用例中的 Promise 拒绝?

How to handle the Promise rejects in the test cases in Jest?

我正在开发一个节点 JS 项目,我正在为其编写测试用例。 代码-

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 是一种执行查询的方法。注释行工作正常但是当我添加 rejects.toMatch 时,测试用例在下面说 -

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)

用于解决案例 -

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
      }
    ])
  })

错误-

 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)

进一步检查,测试用例通过但给出错误 -

(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'"

我试着在线搜索并尝试了各种方法,但总是出现同样的错误。

有什么建议吗?

expect(response).rejects 假设 response 是一个 Promise。但是,您已经在使用 await,因此 response 不是 Promise - 它是该 Promise 的 resolution value
删除 await,或者(如果您收到回复而不是承诺拒绝)保留 await 但停止匹配 .rejects.

如果您决定继续使用 expect(...).rejects,您可能需要等待断言:

await expect(...).rejects.toMatch(...);