Command Palette

Search for a command to run...

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

Async tests and the forgotten await

The third test finished before the promise settled, so it passed with nothing checked. In Vitest or Jest, return or await the promise, or use await expect(promise).rejects.toThrow().

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?

Async tests and the forgotten await ES2017+

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

PASS finds user 1
PASS rejects an unknown id
PASS BUG: forgot to await, so it always passes
Uncaught (in promise) Error: User 9 not found