.resolves

Use resolves to unwrap the value of a fulfilled promise so any other matcher can be chained. If the promise is rejected the assertion fails.

For example, this code tests that the promise resolves and that the resulting value is 'lemon':

  1. test('resolves to lemon', () => {
  2. // make sure to add a return statement
  3. return expect(Promise.resolve('lemon')).resolves.toBe('lemon');
  4. });

Note that, since you are still testing promises, the test is still asynchronous. Hence, you will need to tell Jest to wait by returning the unwrapped assertion.

Alternatively, you can use async/await in combination with .resolves:

  1. test('resolves to lemon', async () => {
  2. await expect(Promise.resolve('lemon')).resolves.toBe('lemon');
  3. await expect(Promise.resolve('lemon')).resolves.not.toBe('octopus');
  4. });