> ## Content Index
> Fetch the complete content index at: https://www.jonathancreamer.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Dealing with async jest tests
- URL: https://www.jonathancreamer.com/dealing-with-async-jest-tests/
- Published: 2020-09-25T18:11:28.000Z
- Updated: 2020-09-25T18:16:18.000Z
- Description: There are several ways to deal with async tests in Jest. The done callback for callback or promise methods, returning promises, or async / await.
- Author: Jonathan Creamer
- Tags: jest, testing, JavaScript, async

In [jest](https://jestjs.io/?ref=jonathancreamer.com), there are a couple of different ways to handle tests which deal with async code.

Let's say way have a test that calls `fetchSomeAsyncThing()`.

```js
it('should call my api', () => {
  const result = fetchSomeAsyncThing();

  expect(result.foo).toBe('hello world')
});
```

This will fail.

Why?

Because of the way JavaScript handles async code.

When you call a function known to be async, whether it's something that returns a `Promise`, calls a `setTimeout`, etc, that function get pushed onto a call stack, and called wayyy after your tests have actually ran.

A stack is a data structure kinda like an array which basically means, the last thing that got put into it, is the first thing to come out. Read more about [stacks](https://github.com/trekhleb/javascript-algorithms/tree/master/src/data-structures/stack?ref=jonathancreamer.com) and [async Javacript.](https://blog.bitsrc.io/understanding-asynchronous-javascript-the-event-loop-74cd408419ff?ref=jonathancreamer.com) 

## Use the done callback

If your async function returns a callback, several of the jest functions provide a done callback which you can use like this...

```js
it('should call my api', (done) => {
  const result = fetchSomeAsyncThing((result) => {
    expect(result.foo).toBe('hello world');
      done();
  });
});
```

As you can see, the callback to the `it` method above returns a `done` function.

This `done` function will tell jest that your async method is done and it will wait for that all to finish before wrapping up that test.

## Use promises

If your async function returns promises you can do one of 2 things.

```js
it('should call my api', (done) => {
  const result = fetchSomeAsyncThing().then((result) => {
    expect(result.foo).toBe('hello world');
      done();
  });
});
```

Similar to above, use the `done` function inside the `.then`.

Or, you can simply return the entire promise.

```js
it('should call my api', () => {
  return fetchSomeAsyncThing((result) => {
    expect(result.foo).toBe('hello world');
  });
});
```

In this case, jest will realize that the return value of the test was itself a promise, and will therefore wait until that promise fully resolves before wrapping up the test.

## Use async / await

Once again, if you know that your async function returns a promise, you can use the `async` and `await` features of [modern Javascript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async%5Ffunction?ref=jonathancreamer.com).

```js
it('should call my api', async () => {
  const result = await fetchSomeAsyncThing();
  expect(result.foo).toBe('hello world');
});
```

The `async` / `await` method is a nice, clean way to read the code.