Learn

Before we can begin testing our code with Jest, the jest package must first be installed and configured. We will download Jest via the Node Package Manager (NPM) by running the following command in our terminal:

npm install [email protected]^27 --save-dev

The npm install jest command installs the jest node package and @^27 specifies which version of the package will be installed. If no version is given, the latest version of Jest will be installed by default. The --save-dev flag specifies that Jest should be saved as a developer dependency. At this point, we should see "jest" included in the package.json file under "devDependencies":

"devDependencies": { "jest": "<version number>" }

Note: If you were to install Jest locally on your machine you would need to include the -g global flag to get access to the command line tools.

Now that we have Jest installed, let’s create a file to write our tests. The Jest API looks for files that are either inside of a __tests__/ directory or any file that ends in either .test.js or .specs.js.

It is a good practice to match the name of the test file to the file that you are wanting to test. For example, if you were to test a file called math.js, you might create a file in the __tests__/ directory called math.test.js.

Once you have your test files created (more on writing test logic in the upcoming exercises), Jest provides a terminal command to run test files individually: jest <filepath/filename>. For example, to run the math.test.js test file we might write:

jest __tests__/math.test.js

You can run individual files by specifying the filename after the jest command

We can also run tests on all of the files that have the .test.js or .spec.js extension or are within a __tests__/ folder by simply running the jest command on its own:

Runnign the jest command with no additional arguments will test all files in the __tests__/ directory

Instructions

1.

Before we can begin testing the language app we should install Jest. Use the proper command to install the jest package as a developer dependency. Make sure you install Jest version 27 — newer versions aren’t compatible with our app!

To confirm that you’ve done this properly, open up package.json and you should see Jest listed in the "devDependencies" section.

Press “Check Work” when you are done.

2.

Throughout the following exercises of this lesson we will be testing a command-line language app that outputs how many countries an inputted language is spoken in. The application can be found in the language_spoken.js file.

Given that file name, create a test file with an appropriate name.

Press “Check Work” when you are done.

3.

We’ll learn about how tests are written later on in this lesson, but for now, we’ll use a provided example test just to see what happens when it is tested. Place the following test code into the newly created __tests__/language_spoken.test.js file:

test("Jest properly installed and configured!", ()=>{})

Press “Check Work” when you are done.

4.

Now that we have a test written, let’s run it using the terminal to make sure that everything is running smoothly.

Run the Jest command on the individual test file that we created. You should see an output with a passing test!

Press “Check Work” when you are done.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?