React Nx Tutorial - Step 2: Add E2E Tests

Nx.dev Tutorial | React | Step 2: Add E2E Tests

By default, Nx uses Cypress to run E2E tests.

Open apps/todos-e2e/src/support/app.po.ts. It's a page object file that contains helpers for querying the page.

Add the following two helpers:

1export const getTodos = () => cy.get('li.todo');
2export const getAddTodoButton = () => cy.get('button#add-todo');

Next, update apps/todos-e2e/src/integration/app.spec.ts.

1import { getAddTodoButton, getTodos } from '../support/app.po';
2
3describe('TodoApps', () => {
4  beforeEach(() => cy.visit('/'));
5
6  it('should display todos', () => {
7    getTodos().should((t) => expect(t.length).equal(2));
8    getAddTodoButton().click();
9    getTodos().should((t) => expect(t.length).equal(3));
10  });
11});

This is not a great example of an E2E test, but it will suffice for the purposes of this tutorial.

If you have not done so already, stop the npx nx serve command and run npx nx e2e todos-e2e --watch.

A UI will open. Click the button in the top right corner that says "Run all specs". Keep the E2E tests running.

As you progress through the tutorial, you will work on making these E2E tests pass.

!!!!! What assertion fails? !!!!! Expect 0 to equal 2 Nothing fails. Everything works. Cannot find any elements matching 'li.todo' Cannot find any elements matching 'button#add-todo'