lecture 11 of 180 completed

Checks that actually catch bugs

A weak assertion passes on a broken app. Here are the three weak ones you will write without noticing.

You already know a test with no assertion proves nothing. At this level the problem is different. Your test has assertions, they pass, and the app is still broken.

Here are the three weak checks that get through code review every day.

Weak check 1: you check something exists, not what it says.

await expect(page.getByTestId('cart-subtotal')).toBeVisible();

The subtotal is visible. Is it correct? This test does not know. If the price calculation breaks and shows $0.00, the test still passes.

Check the number your customer will pay:

await expect(page.getByTestId('cart-subtotal')).toHaveText('$78.00');

Weak check 2: you check one item, not the whole list.

You remove one product from the cart:

await expect(page.getByText('Trail Runner Backpack')).toBeHidden();

That product is gone. But what if the remove button emptied the whole cart? This test still passes. Add the count:

await expect(page.getByTestId('cart-line')).toHaveCount(2);

Weak check 3: you only test when things go well.

Most real bugs live in what an app should refuse:

await page.getByTestId('checkout-card').fill('1111');
await page.getByTestId('place-order-button').click();
await expect(page.getByTestId('card-error')).toContainText('valid card number');
await expect(page.getByTestId('order-confirmed')).toBeHidden();

Look at that last line. You check the error appeared and that the order did not go through. Do not skip it. I have seen a checkout that showed "Invalid card number" in red and created the order anyway. The test only checked the error message, so nobody noticed for two weeks.

Here is a habit that will improve every test you write. Before you finish a test, ask yourself:

If this feature were broken, would my test still pass?

If the answer is yes, your assertion is too weak. Make it stricter until the answer is no.

Remember this: check the value, check the whole list, and check what should not happen.

Reference: Assertions.

now prove it

Reading is the setup. Solve these problems in the editor. We break the app on purpose to check your test would catch it.