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

前端 未结 3 630
猫巷女王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:28

    This is from Jest documentation:

    Expect.assertions(number) verifies that a certain number of assertions are called during a test. This is often useful when testing asynchronous code, in order to make sure that assertions in a callback actually got called.

    So to put in other words, expect.assertions makes sure that the n number of assertions are made by the end of the test.

    It's good to use it especially when writing a new tests, so one can easily check that correct assertions are made during the test. Async tests often pass because the intended assertions were not made before the test-runner (Jest,Mocha etc.) thought the test was finished.

提交回复
热议问题