Necessary to use expect.assertions() if you're awaiting any async function calls?

前端 未结 3 629
猫巷女王i
猫巷女王i 2020-12-30 03:41

I\'ve found a lot of this sort of thing when refactoring our Jest test suites:

it(\'calls the API and throws an error\', async () => {
  expect.assertions         


        
3条回答
  •  [愿得一人]
    2020-12-30 04:25

    To ensure that the assertions in the catch block of an async/await test are adequately tested, expect.assertions(n) must be declared as shown in your code snippet. Such declaration is unnecessary for async/await tests without the catch block.

    It seems quite unintuitive but it is simply the way it is. Perhaps, for certain reasons well deep within the javascript runtime, the test environment can detect when an await'ed' promise successfully resolved but cannot detect same for await'ed' promises that failed to resolve. The creators of the test environment would likely know verbatim why such is the case.

提交回复
热议问题