expect.hasAssertions()

expect.hasAssertions() verifies that at least one assertion is 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.

For example, let's say that we have a few functions that all deal with state. prepareState calls a callback with a state object, validateState runs on that state object, and waitOnState returns a promise that waits until all prepareState callbacks complete. We can test this with:

  1. test('prepareState prepares a valid state', () => {
  2. expect.hasAssertions();
  3. prepareState(state => {
  4. expect(validateState(state)).toBeTruthy();
  5. });
  6. return waitOnState();
  7. });

The expect.hasAssertions() call ensures that the prepareState callback actually gets called.