lecture 9 of 150 completed

Checks that actually catch bugs

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

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

Let's look at the three weak checks that get through code review every day.

Weak check 1: you check that something is there, not what it says.

cy.get('[data-testid="cart-subtotal"]').should('be.visible');

The subtotal is visible. Good. 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:

cy.get('[data-testid="cart-subtotal"]').should('have.text', '$78.00');

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

You remove one product from the cart:

cy.contains('Trail Runner Backpack').should('not.exist');

That product is gone. But what if the remove button deleted the whole cart? This test still passes. You proved one thing left, not that the others stayed.

Add the count:

cy.get('[data-testid="cart-line"]').should('have.length', 2);

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

Most real bugs live in what an app should refuse. Here is a checkout test that pays with a bad card number:

cy.get('[data-testid="checkout-card"]').type('1111');
cy.get('[data-testid="place-order-button"]').click();
cy.get('[data-testid="card-error"]').should('contain', 'valid card number');
cy.get('[data-testid="order-confirmed"]').should('not.exist');

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 simple habit that will improve every test you write. Before you finish a test, ask yourself one question:

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

If the answer is yes, your check 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.