Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 13.6 — Testing Basics
JavaScript · Example 1 of 3

A tiny test helper finds a real bug

The regex [^a-z0-9] treats é as punctuation. The test found it; whether é should be kept is a product decision the test now records.

A test is a small piece of code that runs your code with a known input and checks the result with an assertion; a test runner such as Vitest or Jest runs all your tests and reports which pass, so bugs are caught by a machine every time you change something.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Using the test and assertEqual helpers from the first live example, write three tests for isEven(n) (return n % 2 === 0;): a normal even number, an odd number, and the edge case 0.

02

Write the Vitest version of this: capitalize("asha") should be "Asha", capitalize("") should be "", and capitalize(5) should throw a TypeError.

Explain it without notes

01

What is the difference between an assertion, a test and a test runner?

02

What makes a good unit test?

03

Why do you need toEqual instead of toBe for arrays and objects?

04

What is a regression test, and why write one before fixing a bug?

A tiny test helper finds a real bug

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

PASS joins words with dashes
PASS removes punctuation
PASS trims spaces at both ends
FAIL keeps accented letters → expected "café-menu" but got "caf-menu"
3 passed, 1 failed