Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 131 additions & 2 deletions client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,137 @@ You will also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.<br>
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
Opens the Playwright end-to-end tests in Playwright's interactive UI mode
(`playwright test --ui --headed`). The dev server must already be running —
see [Running the Playwright tests locally](#running-the-playwright-tests-locally).

### `npm run test:unit`

Runs the small Jest unit test suite in `src/__test__/` once and exits.

## Running the Playwright tests locally

The end-to-end tests in `tests/` are this client's main test suite, and they
are what CI runs on every pull request to `develop`, `main` and `master`.

**You don't need the API server or a database.** Each test mocks the API calls
it needs (`tests/helpers/mocks.ts`), and the shared fixture in
`tests/helpers/test.ts` answers every other `/api/**` call with a 502. That
fixture also blocks Mapbox map tiles and analytics, and returns a fixed Mapbox
geocoding result, so the tests don't depend on the network.

### One-time setup

From the `client/` folder:

```bash
npm ci
npx playwright install
```

`npx playwright install` downloads the Chromium, Firefox and WebKit browsers
Playwright uses. Run it again after a Playwright version bump if the tests
complain that a browser executable is missing. (On Linux, use
`npx playwright install --with-deps` to also install the system libraries.)

### Run the tests

Playwright does **not** start the app for you (the `webServer` block in
`playwright.config.ts` is commented out). The tests expect the app at
`http://localhost:3000`.

1. In one terminal, start the dev server and leave it running:

```bash
npm start
```

2. In a second terminal, run the tests:

```bash
npx playwright test
```

When the run finishes, open the HTML report (it opens automatically if a test
failed):

```bash
npx playwright show-report
```

### Useful variations

By default every test runs three times, once each in Chromium, Firefox and
WebKit. While iterating, it's usually faster to pick one browser:

```bash
npx playwright test --project=chromium
```

Run a single spec file, or only tests whose name matches some text:

```bash
npx playwright test tests/auth.spec.ts
```

```bash
npx playwright test -g "renders without crashing"
```

Watch the browser while the tests run:

```bash
npx playwright test --project=chromium --headed
```

Open the interactive UI mode, where you can pick tests, re-run them on save,
and step through each action (this is what `npm test` does):

```bash
npx playwright test --ui
```

Step through one test in the Playwright Inspector:

```bash
npx playwright test tests/auth.spec.ts --project=chromium --debug
```

### Matching what CI does

CI runs the tests against a production build served by `vite preview`, not the
dev server, because the dev server slows down when many tests run in parallel.
If a test passes locally but fails in CI (or the other way round), try the CI
setup. Stop `npm start` first, since both use port 3000:

```bash
npx vite build && npx vite preview --port 3000 --strictPort
```

Then run `npx playwright test` in another terminal. CI also retries failed
tests up to twice and records a trace on the retry; you can download the
`playwright-report` artifact from the failed GitHub Actions run and open it
with `npx playwright show-report <unzipped-folder>`.

### Writing new tests

Import `test` from `./helpers/test` instead of from `@playwright/test`, so your
test gets the mocked network setup described above:

```ts
import { expect } from "@playwright/test";
import test from "./helpers/test";
import mockRequests from "./helpers/mocks";

test("shows the home page", async ({ page }) => {
await mockRequests(page);
await page.goto("/");
await expect(page.getByText("Learn about this site")).toBeVisible();
});
```

Files in `tests/helpers/` are shared helpers, not specs, and Playwright skips
them.

### `npm run build`

Expand Down
Loading