lecture 17 of 170 completed

Reading a failing test

Timeout messages, assertion diffs and the command log. Failures are reports, not riddles.

When a test fails, most beginners do one of two things. They change something at random and run it again, or they ask a senior engineer. Both are slow.

The truth is that Cypress usually tells you exactly what went wrong. The skill is reading the message literally, word by word. Let me show you the three you will see most often.

The timeout message.

Timed out retrying after 4000ms: Expected to find element:
[data-testid="contact-line"], but never found it.

Read it in order: Cypress retried (so timing was not the problem), for 4 seconds, looking for this selector, and it never matched. Two possible causes, and you check them in this order:

  1. The selector is wrong. Open the app, inspect the element, compare the real attribute. This is the cause most of the time.
  2. The element never appears here at all. Then the app or your test's idea of the flow is wrong, also worth knowing.

What almost never helps: raising the timeout. If the element never appears, 40 seconds fails the same way, just slower.

The assertion diff.

expected '<span>' to have text '1', but the text was '0'

This one is a gift: the app really did something different from your claim. Either the claim is wrong (fix the test) or the app is broken (you found a bug). Deciding which is the whole job of a tester, and it starts with reading the diff, not re-running and hoping.

The command log. In the Cypress runner, every command is listed in order, and hovering one shows the app at that moment. That is time travel through your test. When a failure confuses you, walk the log backwards from the red step: what did the page actually look like one step earlier?

On TestAcademy there is a third verdict you will meet: "your test passed, but it also passed when we broke the app." That is not a failure to fix with selectors or waits. It means an assertion is missing or too weak. Go back to what the test claims.

More on debugging tools: Debugging in Cypress.

The final problem of this track: prove that logout really logs you out. Read every failure you hit on the way. Each one tells you your next step.

now prove it

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