lecture 4 of 200 completed

Your first test

The anatomy of a spec: describe, it, visit, act, assert. And what makes it a real test.

What you'll learn

  • The parts of a Cypress spec: describe, it, visit, act, check
  • How to name a test so a failure explains itself
  • Why a test without a check proves nothing

You click a button to turn on a lamp. The button works, but the lamp stays off. A test that clicks and then stops cannot see this bug. It passes anyway.

A Cypress test file is called a spec. Every spec has the same shape, and you will write it hundreds of times.

Reading example. Reading Desk is a fictional app. You can read this example here. The exercise below uses a different app.

describe('Reading Desk lamp', () => {
  it('turns the lamp on when you press the button', () => {
    cy.visit('/');
    cy.contains('button', 'Turn on lamp').click();
    cy.get('[data-testid="lamp-message"]').should('have.text', 'Lamp on');
  });
});

Read it as four layers:

  1. describe groups related tests. One describe block per page or feature is a good start.
  2. it is one test. Its name is a sentence that says what the app should do. You are writing a claim, not a label. "turns the lamp on when you press the button" tells a reader exactly what broke when it fails.
  3. Arrange and act. cy.visit('/') loads the page and waits for it to finish loading. Then you do what a user would do. Here, cy.contains('button', 'Turn on lamp') finds the button by its text, and .click() presses it.
  4. Assert. .should(...) states what must now be true. Here, the message must say "Lamp on". This line is the test. Without it, the code runs, clicks, finishes, and passes whether the lamp works or not.

That last point is the difference between code that runs and a test that proves something. A test with no check passes against a completely broken app.

A test file can contain fourteen tests, every one clicking through a form and finishing, with no checks anywhere. The suite is green. It has never tested anything.

When you solve problems here, we check this for real: we break the app on purpose, and your test must turn red.

Two small habits that pay off from day one:

  • One behaviour per it. When it fails, the name points straight at the problem.
  • Name tests as claims about the app, not steps ("shows an error", not "click button and check").

Your exercise. In Nimbus, submit the login form with both fields empty. Check that Email shows an error. Check that Password shows an error too. Use the same four layers as the lamp test.

These lessons assume you can write a simple loop. If you cannot, start with the JavaScript guide to loops. Come back when you can read each value in a list.

You can try the shop exercise for free. The subtotal forgets the quantity.

Course members can continue with Choose data that exposes a bug.

Remember this. After an action, check what the user should see. A test without a check proves nothing.

The official guide to your first spec: Writing your first end-to-end test.

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.