Interactive tutorial

Unit Testing JavaScript with Jest: Matchers, Setup, and Teardown

Learn to write and run JavaScript unit tests with Jest on your own computer. Choose appropriate matchers for values, objects, collections, and errors; create fresh test fixtures; and use setup and teardown hooks without introducing dependencies between tests. Finish by repairing and running a small shopping-cart test suite.

  • Level: Beginner
  • Duration: approx. 37 min
  • 8 steps
Unit Testing JavaScript with Jest: Matchers, Setup, and Teardown

What you will go through

  1. Step 1 of 8 6 min Run Your First Jest Test Create a local Jest project, run an empty-cart test, and interpret an intentional failure.
  2. Step 2 of 8 4 min Match Primitive Values Precisely Choose precise Jest assertions for primitive values, truthiness, negation, and approximate numbers.
  3. Step 3 of 8 4 min Compare Objects and Collections Choose assertions that distinguish object identity from contents and check the intended aspect of a collection.
  4. Step 4 of 8 4 min Test Expected Errors Test a rejected cart item by letting Jest catch the error and checking that the cart remains unchanged.
  5. Step 5 of 8 5 min Start Each Test with Fresh Data Give each test an independent cart fixture and check that test order no longer matters.
  6. Step 6 of 8 5 min Restore Shared State with Teardown Control a temporary currency setting and restore the original environment state after each test.
  7. Step 7 of 8 5 min Choose the Right Hook Scope Choose hook scopes for a fresh cart fixture and a shared currency baseline, then trace the lifecycle around two tests.
  8. Step 8 of 8 6 min Repair and Run an Independent Test Suite Repair a shopping-cart suite, run it in two test orders, and explain how its fixtures and cleanup prevent state leakage.

Video Courses

Courses related to this topic

See all video courses

Download the App now to have access to + 5000 free courses, exercises, certificates and lots of content without paying anything!

  • 100% free online courses from start to finish

    Thousands of online courses in video, ebooks and audiobooks.

  • More than 60 thousand free exercises

    To test your knowledge during online courses

  • Valid free Digital Certificate with QR Code

    Generated directly from your cell phone's photo gallery and sent to your email

Cursa app on the ebook screen, the video course screen and the course exercises screen, plus the course completion certificate