JavaScript · Advanced level

Advanced level · Module 1 of 6 · 22 guided hours

Testing JavaScript

Write unit, asynchronous and DOM tests, use test doubles wisely and practise test-driven development.

Learning objectives

  • Write unit tests with a JavaScript test runner
  • Test asynchronous code and code that uses the DOM
  • Use mocks and fakes appropriately and apply test-driven development

Topics

  • node:test or Vitest
  • assertions and test structure
  • testing promises and timers
  • mocks, stubs and fakes
  • DOM testing with jsdom
  • test-driven development

Lessons

  1. Lesson 1 Unit Testing JavaScript

    Write and run unit tests with a test runner, structure them clearly and run them on every change.

    test runnerassertionarrange–act–assertwatch mode

  2. Lesson 2 Testing Asynchronous Code and Using Test Doubles

    Test promises and async functions, control timers and replace slow or unreliable dependencies with test doubles.

    async testsassert.rejectsfake and mockdependency injection

  3. Lesson 3 Testing DOM Code

    Test code that renders and responds to events using a simulated DOM, focusing on what users see and do.

    jsdomrendering testssimulated eventsqueries by role and text

  4. Lesson 4 Test-Driven Development

    Write a failing test first, make it pass with the simplest code, then refactor — and know when this approach helps most.

    red–green–refactorsmallest steprefactoring with testsregression test

Practical activity

Add a test suite to the Intermediate budget app: calculations, the API client with a fake fetch and the rendering code with a simulated DOM.

Expected outcomes

  • Reliable automated tests
  • Code designed to be testable

Assessment

Practical: test a provided module to a coverage target; marked by detection of seeded bugs and test readability.

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.