Testing Made Easy with Ava: No More Pulling Out Your Hair!

Testing Made Easy with Ava: No More Pulling Out Your Hair!

Testing Made Easy with Ava: No More Pulling Out Your Hair! was initially published on Friday January 06 2023 on the Tech Dev Blog. For the latest up-to-date content, fresh out of the oven, visit https://techdevblog.io and subscribe to our newsletter!

Welcome to our tutorial on Ava, a popular JavaScript testing library! And our very own favorite, here at Tech Dev Blog. If you're reading this, you're probably interested in learning how to use 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, from installing it to writing your first test.

So, let's get started!

Why Ava?

In the authors' own words (emphasis my own):

Installing Ava

The first thing you'll need to do is install 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 as a dev dependency for your project.

Writing Your First Test

Now that Ava is installed, it's time to write your first test! In 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:

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 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 makes it easy to do this! Just create a separate file for each group of tests and 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! 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!

Testing Made Easy with Ava: No More Pulling Out Your Hair! was initially published on Friday January 06 2023 on the Tech Dev Blog. For the latest up-to-date content, fresh out of the oven, visit https://techdevblog.io and subscribe to our newsletter!

Did you find this article valuable?

Support Tech Dev Blog by becoming a sponsor. Any amount is appreciated!