Testing with Jest
Jest is an open source JavaScript testing framework developed by Facebook. It's commonly used to test React code, but can also be used to test Node.js applications. Jest provides a similar testing interface to Mocha, but Jest is more opinionated and bundles more features, such as a built-in assertion mechanism.
In this recipe, we will learn how to write and structure tests with Jest and also learn how we can report on test coverage.
Getting ready
We will be using Jest to test a program that converts any string input to uppercase:
- First, let's create and initialize our project directory:
$ mkdir testing-with-jest $ cd testing-with-jest $ npm init --yes
- We need a program to test. Create a file named
uppercase.js
:$ touch uppercase.js
- Add the following code to
uppercase.js
:module.exports = (string) => { return string.toUpperCase(); };
- We also need to create a directory named
test
to hold the tests. Within thetest...