# E2E Tests

We use end-to-end (E2E) tests to ensure that the application works as expected from the user's perspective.

E2E tests should _not_ test every possible edge case or error condition. Instead, they should test main user flows and ensure that the application works as expected in the most common scenarios. As a rule of thumb:

- develop E2E tests alongside the development of new features, and

- run the tests during any development/bugfixes to avoid regressions.

> The CI is configured to run the E2E tests on every push to a branch.

## Stateful vs. Stateless

The E2E tests are configured to run in two modes:

- **Stateful**: These tests can change the data. Each test should run a data reset before it runs. Hence, these tests are slower and should work with limited seed data.
- **Stateless**: The tests should not alter any data on the server - but rather tests the views. This allows for larger/more complex seed data.

## Quick start

#### Start Backend in E2E mode

```sh
# Start backend in E2E stateful mode
cd backend
pnpm e2e:stateful

# Start backend in E2E stateless mode
cd backend
pnpm e2e:stateless
```

#### Start Frontend in E2E mode

```sh
# Start frontend in E2E mode
cd frontend
# Builds and starts in production mode with test-specific settings. Use this to test the production build locally and ensure it behaves as expected, similar to the CI environment
pnpm e2e:build
```

#### Run E2E tests

> Tests need to be executed in a Docker Linux container so that the environment matches the one used in CI.

```sh
# Start a shell in Docker
pnpm docker

# Run E2E tests
pnpm e2e

# Test using Playwright UI
pnpm e2e:ui

# Run E2E tests and update snapshots
pnpm e2e:update-snapshots
```

## Writing tests

When writing tests, you can think of the AAA pattern: Arrange, Act, Assert:

- **Arrange**: Set up the test data and the environment
- **Act**: Perform the action that you want to test
- **Assert**: Check that the action had the expected effect

See the example tests provided in `e2e/specs/` for inspiration.

> When running `pnpm e2e`, the detailed test results are saved in `test-results/` directory!

## CI/CD

The `e2e` GitHub Action runs the E2E tests on every push to GitHub.

In case the tests fail, the GitHub Action will fail and the error messages will be displayed in the GitHub Actions tab. The _playwright-report_ can be downloaded from GitHub's Artifacts section and be consumed by running `pnpm playwright show-report`. In the [report](http://localhost:9323/) you can also analyze the [trace](https://playwright.dev/docs/trace-viewer).

## F.A.Q

### How to resolve Error: No Tests Found?

Playwright recognizes test files with patterns like `*.spec.ts`, `*.test.ts`, `test-*.ts`. Make sure that you are using the same naming convention in your test files.

### I start Playwright in UI mode. It says `Listening on http://0.0.0.0:3030`, what do I do next?

You can now navigate to http://127.0.0.1:3030 for the Playwright dashboard.
