Testing Made Easy with Ava: No More Pulling Out Your Hair!
Tags: unittesting, integrationtesting, testing, library
Published: 2023-01-07 01:07:06

---
title: Testing Made Easy with Ava: No More Pulling Out Your Hair!
published: true
date: 2023-01-06 06:10:31 UTC
tags: UnitTesting,IntegrationTesting,Testing,Library
canonical_url: https://techdevblog.io/testing-made-easy-with-ava-no-more-pulling-out-your-hair/
---

 ![Testing Made Easy with Ava: No More Pulling Out Your Hair!](https://techdevblog.io/content/images/2023/01/image_24.png)

Welcome to our tutorial on [Ava](https://github.com/avajs/ava), a popular JavaScript testing library! And our very own favorite, here at [Tech Dev Blog](https://techdevblog.io/). If you're reading this, you're probably interested in learning how to use [Ava](https://github.com/avajs/ava) to test your code and make sure it's working properly. Well, you're in the right place! In this tutorial, we'll go over everything you need to know about using [Ava](https://github.com/avajs/ava), from installing it to writing your first test.

So, let's get started!

## Why [Ava](https://github.com/avajs/ava)?

In [the authors' own words](https://github.com/avajs/ava#why-ava) (emphasis my own):

- Minimal and fast
- Simple test syntax
- Runs tests **concurrently**
- Enforces writing atomic tests
- **No implicit globals**
- **Includes TypeScript definitions**
- [Magic assert](https://github.com/avajs/ava#magic-assert)
- [**Isolated environment _for each test file_**](https://github.com/avajs/ava/blob/main/docs/01-writing-tests.md#process-isolation)
- [Promise support](https://github.com/avajs/ava/blob/main/docs/01-writing-tests.md#promise-support)
- [Async function support](https://github.com/avajs/ava/blob/main/docs/01-writing-tests.md#async-function-support)
- [Observable support](https://github.com/avajs/ava/blob/main/docs/01-writing-tests.md#observable-support)
- [Enhanced assertion messages](https://github.com/avajs/ava/blob/main/docs/03-assertions.md#enhanced-assertion-messages)
- [**Automatic parallel test runs in CI**](https://github.com/avajs/ava#parallel-runs-in-ci)
- [TAP reporter](https://github.com/avajs/ava/blob/main/docs/05-command-line.md#tap-reporter)

## Installing [Ava](https://github.com/avajs/ava)

The first thing you'll need to do is install [Ava](https://github.com/avajs/ava). Don't worry, it's super easy! All you need to do is open up your terminal and run the following command:

```
npm install --save-dev ava

```

This will install [Ava](https://github.com/avajs/ava) as a dev dependency for your project.

## Writing Your First Test

Now that [Ava](https://github.com/avajs/ava) is installed, it's time to write your first test! In [Ava](https://github.com/avajs/ava), tests are simply functions that return a promise. If the promise is fulfilled, the test passes. If the promise is rejected, the test fails.

Here's an example of a simple test in [Ava](https://github.com/avajs/ava):

```
import test from 'ava';

test('my test', t => {
  t.is(2 + 2, 4);
});

```

This test checks that 2 + 2 is equal to 4. Which obviously is true. The `t` object that's passed to the test function is an object containing various utility functions for making assertions. In this case, we're using the `is` function, which checks that its two arguments are equal.

## Running Your Tests

Now that you've written your first test, it's time to run it! You can do this by simply running the following command in your terminal:

```
ava

```

This will run all the tests in your project. If your test passes, you'll see a message like this:

```
1 test passed

```

If your test fails, you'll see a message like this:

```
1 test failed

```

## Advanced Testing Techniques

Of course, [Ava](https://github.com/avajs/ava) is capable of much more than just simple assertions. Here are a few other things you can do with it:

### Async Tests

If your tests involve asynchronous code, you can use the `async` keyword to make your test function return a promise. Here's an example:

```
import test from 'ava';

test('my async test', async t => {
  const value = await someAsyncFunction();
  t.is(value, 'expected value');
});

```

### Testing Multiple Values

Sometimes you'll want to test that multiple values are correct. You can use the `deepEqual` function for this. Here's an example:

```
import test from 'ava';

test('my multiple value test', t => {
  t.deepEqual([1, 2, 3], [1, 2, 3]);
});

```

### Testing Thrown Errors

If you want to test that a function throws an error under certain circumstances, you can use the `throws` function. Here's an example:

```
import test from 'ava';

test('my error throwing test', t => {
  t.throws(() => {
    throw new Error('oops');
  });
});

```

### Testing for Performance

If you want to test that a particular function is running within a certain time limit, you can use the `plan` and `end` functions. Here's an example:

```
import test from 'ava';

test.cb('my performance test', t => {
  t.plan(1);
  t.end();
});

```

The `plan` function sets the number of assertions that will be made in the test. The `end` function ends the test. This is useful for performance testing, as it allows you to set a time limit for the test and fail it if it takes too long.

### Testing with Multiple Files

If you have a lot of tests, you might want to split them up into multiple files. [Ava](https://github.com/avajs/ava) makes it easy to do this! Just create a separate file for each group of tests and [Ava](https://github.com/avajs/ava) will automatically run them all. For example, you might have a `Math.test.js` file for your math-related tests and a `Strings.test.js` file for your string-related tests.

## Conclusion

We hope this tutorial has been helpful in getting you started with [Ava](https://github.com/avajs/ava)! As you can see, it's a powerful and easy-to-use testing library that can handle just about any testing scenario you might encounter. So go forth and start testing your code with confidence!

Happy coding!

<!--kg-card-end: markdown-->