Testing Your JavaScript

You've been passing tests all course; now write your own. Learn why tests are a safety net, write tests with Vitest, test the edge cases, errors and async code, and test a page with jsdom.

Updated

Testing in JavaScript

  1. Your First Tests with Vitest (Journeyman): A unit test is a few lines of code that check one piece of your code and shout when it breaks, and Vitest's describe, it and expect let you write one, run it and read the result.
  2. Testing Edge Cases, Errors and Async Code (Freelancer): Bugs hide at the limits, in the errors and in the code that waits, so this lesson tests all three with Vitest, using toThrow, resolves and rejects, and fake timers that skip ahead in time.
  3. Testing a Page with jsdom (Freelancer): jsdom is a browser without a screen that runs inside your tests, so Vitest can build a little page, click its buttons with dispatchEvent and check what the page says, in a moment and without opening a browser.

Exercise: Ticket Booth Tests

  1. Exercise: Ticket Booth Tests (Freelancer): Write the Vitest tests for a movie theater's working ticket booth, from its prices and discounts to its page, and prove they're good by catching three bugs the check plants in the code.