Learn

As developers, we strive to make our code bulletproof from bugs that could break our applications. One process that can help us get closer to “unbreakable code” is testing while testing frameworks are libraries that provide functions that enable testing.

When developing and testing in JavaScript, we can use a testing framework called Jest. While there are many testing frameworks out there, Jest focuses on simplicity. Jest provides the two key ingredients needed for testing:

  1. An assertion library – an API of functions for validating a program’s functionality
  2. A test runner – a tool that executes tests and provides outputted test summaries

Unlike other testing frameworks which may require you to bundle these separately, Jest provides both, allowing us to begin testing right out of the box without much setup.

In this lesson, we will analyze and test the command-line application found in language_spoken.js that outputs the number of countries that an inputted language is spoken in. While analyzing this app, we learn some of the core features of Jest including:

  • Installing and configuring Jest
  • Creating tests using the test() function
  • Testing asynchronous code
  • Creating and using mocks within your tests

Let’s begin!

Note: If you have used other testing frameworks like Mocha (covered in the Learn JavaScript Unit Testing course), much of the syntax of Jest will be familiar to you. While you may continue to use Mocha to test your applications, consider the tradeoffs of each framework as you choose the framework that is right for you! Jest, for example, is frequently used when testing React applications.

Instructions

Before we begin, take a moment to explore the features of this command-line app. The app can be executed by running npm start in the terminal. Respond to the prompts in the terminal to see how many countries around the world speak a given language.

Then, take a look at language_spoken.js and the functions that it exports. Throughout the lesson, we will be writing tests for these functions, though it isn’t required that you fully understand how these functions are implemented in order to test them.

Take this course for free

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