Add Jest to Your Application and Create Your First Test

Daniel Afonso
InstructorDaniel Afonso
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

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.

egghead
egghead
~ 41 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today