簡體   English   中英

如何處理 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 的解析值
刪除await ,或者(如果您收到響應而不是承諾拒絕)保留await但停止匹配 .rejects。

如果您決定繼續使用expect(...).rejects ,您可能需要等待斷言:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM