Jest is a fast and safe testing framework with a focus on simplicity.
This lesson will walk you through how to add jest to your application by running yarn add --dev jest
followed by updating your package.json
to be able to run your tests on the console by using the yarn test
script.
This lesson covers how to create your first test by creating your first block that groups several related tests with describe
and then adding your first test method using either the it
or test
keyword.
Instructor: [0:00] To add Jest to your application, install it by running on the console, yarn add --dev jest. This will add Jest as a dev dependency on your package.json. To be able to run your tests, on your test script, add Jest to it. To make sure Jest is running, go back to the console and run yarn test to execute your tests.
[0:25] Since we don't have any tests created, we should have an error letting us know that no tests were found. You can create a test by using the pattern name_of_the_file_you_want_to_test.test.js. In this scenario, we called it index, since we're going to test the content of the index file.
[0:43] On that file, now you can create a describe block. This block will be used to group several tests related with sum. Inside your describe block, you can use either it or test to create a method to run your tests. Each test should have the responsibility to assert something about the code to be tested.
[1:02] In this test, we're going to validate if the sum of 2+1 returns 3. Since we are only setting up the test, let's add a comment inside of the method.
[1:12] Finally, you can run yarn test again on the console to see your test run. Here, we can see that our test that belongs to the sum block has been executed. Since we don't have any errors or failed assertions inside of our test, Jest will assume that the test has passed successfully.