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.
- An assertion library – an API of functions for validating a program’s functionality
- 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
- Testing asynchronous code
- Creating and using mocks within your tests
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.