Search⌘ K
AI Features

What is a Test?

Explore the fundamentals of front-end testing by understanding what a test is, how to write assertions, and how to use Jest testing framework features like test grouping and lifecycle hooks for reliable code verification.

We'll cover the following...

Test

A test is a code that throws an error when the actual result of something does not match the expected output.

Example

Javascript (babel-node)
const sum = (a, b) => a + b;
const subtract = (a, b) => a - b;
let result, expected;
result = sum(3, 7);
expected = 10;
if (result !== expected) {
throw new Error(`${result} is not equal to ${expected}`);
}
result = subtract(7, 3);
expected = 4;
if (result !== expected) {
throw new Error(`${result} is not equal to ${expected}`);
}

The result !== expected part is called an assertion. You can change the variable expected to see how the assertion fails.

Jest

Jest ...