- Jest parallelizes test runs across workers to maximize performance.
- Jest easily creates code coverage reports using
—-coveragewith no extra setup or libraries needed.
- Provides a powerful mocking library for functions and modules.
- Works with TypeScript and integrates seamlessly with babel.
.test.js extension or place your tests files in a
First, create a folder and then we would create a package.json file using:
The installation should look like this:
You have created a
package.json file that would keep record of the project information and also dependencies.
Let’s install the jest dependency.
Check that your package.json looks like this:
Let’s have a quick example. Create a new file and name it
concat.js. Add this code:
Then, create a file named
concat.test.js. This is where we would put our test code:
Run the test using
npm test. Here is what you’ll see:
You have just written your first test. Yaay!
Introduction to Jest expect API
expect gives you access to a number of
matchers that let you validate different things. When writing a test, we want to make sure that the result is as expected. Jest expect API contains a lot of methods you can use in your application. We can’t explore all, but we would look into at
This is very useful when testing asynchronous code to ensure that an assertions in a callback was called.
expect.assertions() is used to verify if the number of callback returned is the expected number. This is a sample test that uses expect.assertions() to ensure that all callbacks are called before knowing if the test passed or failed.
expect.assertions(3) makes sure that the 3 callbacks are called before passing or falling the test.
.toBe() basically just checks that value is what you expect it to be. We’ve used this in our quick example. Here’s another one:
profile.test.js passes because profile is an object and we expect the result of the test to be an object.
I know I said that we would only be exploring two methods, but let’s take a look at a some more in action. Matchers enable us test values in various ways. I’ll list some here:
toEqualrecursively checks every field of an object or array.
toBeNullmatches only null
toBeUndefinedmatches only undefined
toBeDefinedis the opposite of
toMatchchecks strings against regular expressions.
toContainchecks if an array contains a particular item. Let’s see them action. Create a file and save it as
matchers.test.js. Add this code and run using
Here’s the outcome:
We can see that all our test passed successfully. You can manipulate values and see the outcome. There is no better way to learn than to actively try it out!
Testing Asynchronous Code
Handling asynchronous code can be difficult; Testing it? More difficult. Let’s look at how Jest is used to test asynchronous code.
done as an argument so that Jest will wait until the
done callback is called before finishing the test. Should you omit
done() from the code, the test will not work as it should because the callback function won”t be called at all.
If your code makes use of promises, just return a promise in your test and Jest would handle the asynchronous call and wait for it to resolve. If the promise is resolved, the test will pass; if not, the test will fail.
Let’s say that
getMessage, returns a promise with the string ”Learn Web Development on Scotch.io”, We can test it simply with this:
If you don’t use the
return keyword, your test will end before it even does anything meaningful.
Jest also has other ways of testing asynchronous code. It offers
.rejects usage in writing tests for asynchronous code. For more on testing asynchronous code, check here
Built-in Code Coverage Report
Jest can collect code coverage information from the
project/directory you’re working in including untested files. To view built-in code coverage report, Install Jest globally. Run:
Here’s the result:
In a later tutorial, I would breakdown testing asynchronous code using Jest into pieces you can easily understand incase you didn’t get the idea here. If you have any question, feel free to ask. I would be glad to hear it!