lecture 4 of 150 completed

Conditions

if and else decide which lines run. A test that branches is usually hiding an assertion that never runs.

What you'll learn

  • How if, else if and else choose what runs
  • Why a check inside an if can be skipped without a failure
  • Where a condition belongs in test code, and where it does not

A Shelf test clicks the Reserve button and checks the confirmation. The test has passed every day for months. The Reserve button was removed from the page a long time ago.

Here is the shape of that test, with the page lookup replaced by a plain value:

const reserveButton = null;

if (reserveButton !== null) {
  console.log("clicked Reserve");
  console.log("checked the confirmation");
}
console.log("test finished");
test finished

An if statement runs a block of code only when its condition is true. The condition is the part in round brackets. The block is the part in curly brackets.

Here reserveButton is null, which is the value for "nothing here". The condition is false, so JavaScript skips the whole block. The click is skipped, and so is the check. The code reaches the end with no error, and a test with no error passes.

The author meant to be careful: "click the button if it is there". The result is a test that can never fail.

Say what you expect

A test should know what state the page is in. It should not ask. If the button must be there, a missing button is a failure.

const reserveButton = null;

if (reserveButton === null) {
  throw new Error("The Reserve button is missing");
}
console.log("clicked Reserve");
Error: The Reserve button is missing

throw stops the code and reports an error. A test runner marks the test as failed and shows your message.

In a real test you rarely write this if yourself. An assertion does the same job. This Playwright line fails the test when the button is not visible:

await expect(page.getByRole('button', { name: 'Reserve' })).toBeVisible();

Where conditions belong

Conditions are a normal part of helper code and setup. This one picks the address to test against:

const environment = "staging";
let baseUrl;

if (environment === "staging") {
  baseUrl = "https://staging.shelf.test";
} else if (environment === "production") {
  baseUrl = "https://shelf.test";
} else {
  baseUrl = "http://localhost:3000";
}
console.log(baseUrl);
https://staging.shelf.test

JavaScript checks each condition from the top. It runs the first block whose condition is true and skips the rest. else catches every case that nothing above matched.

This branch is fine because it happens before the test. Every path ends with a value for baseUrl, and the checks that follow always run.

Before you keep an if inside a test, ask what happens when its condition is false. If the answer is "nothing", an assertion is hidden in there. A senior reviewer will leave that exact comment on the pull request.

Remember this

An if skips its block without any error when the condition is false. Inside a test, state what you expect and let the test fail. Keep conditions for helpers and setup, and give every path a result.

Reference: if...else on MDN.

now prove it

Reading is the setup. Solve this exercise in the editor. We run your code on each case and compare what it prints with the expected output.