What is a Test?

Learn what a test is and how it works in jest.

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

Press + to interact
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 ...