Learn

With our test() container set up, it is time to finish our first unit test by writing assertions to validate the various features of our code. To do this, Jest provides the expect() function.

The expect() function asserts how we expect our program to run and is used every time that we want to write a test. However, this function is rarely used alone – it can almost always be found in conjunction with matcher methods, like .toBe(), in the example below:

expect(2+2).toBe(4)

The value passed to expect() should be an expression that you want to test (2+2) while the matcher method determines how that expression will be validated and what the expected value of that expression is (.toBe(4)).

Let’s look again at the getIngredients method in the recipes module to see how expect() assertions fit into a test().

Remember, getIngredients() converts an object containing ingredients and their quantities for a recipe into an array of just the ingredients. To test this function, we can add testing logic to the callback passed to the test() function:

//file: __tests__/recipes.test.js // import the function to test import { getIngredients } from "./recipes.js"; test("Get only the ingredients list for Pesto", () => { //arrange const pestoRecipe = { 'Basil': '2 cups', 'Pine Nuts': '2 tablespoons', 'Garlic': '2 cloves', 'Olive Oil': '0.5 cups', 'Grated Parmesan': '0.5 cups' } const expectedIngredients = ["Basil", "Pine Nuts", "Garlic", "Olive Oil", "Grated Parmesan"] //act const actualIngredients = getIngredients(pestoRecipe); //assertions expect(actualIngredients).toEqual(expectedIngredients) });

In this example, we follow the Arrange, Act, Assert pattern in the callback passed to test():

  • Arrange: We first declare the input (pestoRecipe) to be passed to the function being tested (getIngredients()) as well as the expected output (expectedIngredients).
  • Act: We then pass the input variable into the function being tested and store the result in a new variable (actualIngredients).
  • Assert: Finally, we use the expect() assertion function and the .toEqual() matcher to compare the values of the expected output with the actual output.

Multiple expect() assertions can be made within a single call to test(). Regardless of the number of assertions made within a unit test, in order for the entire test to pass, all assertions must pass.

Now it’s your turn to try writing some assertions for the language_spoken app!

Note: The .toBe() matcher can be used to compare simple data types for equality while the .toEqual() matcher is used to perform deep equality comparisons.

Instructions

1.

Take a look at the language_spoken.test.js file. In the test() function you created last time, we created a container to test the countryExtractor() function. As the description string states, this function is designed to "convert array of country data objects to array of countries".

As you can see, the arrange and act portions have been taken care of. It’s your job to make the assertions.

Below the comment //assertions use the expect() function and pass the value we would like to test.

2.

Use the .toEqual() matcher method to define what value actualValue is expected to be.

3.

Run the test command in the terminal to see the result. You should see 1 passing test!

Take this course for free

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