# Cypress

Run the end-to-end and component tests of every package with a Cypress config.

The Cypress plugin gives every package that uses Cypress an `e2e` task, and a `component` task when it has component tests.

## Add it

```sh
axle add cypress
```

This writes it into the root `axle.toml`:

```toml
[plugins.cypress]
source = "npm:@axle-rs/plugin-cypress@0.1.0"
hash = "sha256-…"
```

## Tasks

A package or project with a `cypress.config` file, `.ts`, `.mts`, `.cts`, `.js`, `.mjs` or `.cjs`, gets a task for each section the config has:

| Task        | Command                   | Settings                                                                |
| ----------- | ------------------------- | ----------------------------------------------------------------------- |
| `e2e`       | `cypress run`             | with an `e2e` section; not cached, `outputs` the screenshots and videos |
| `component` | `cypress run --component` | with a `component` section; the same settings                           |

Each command runs through the repository's package manager, such as `pnpm exec cypress run`, so it uses the version the package installs.

Tests that reach a running server or a browser aren't cached by default, since a passing result says nothing about the next run; the `cache` setting turns caching on. The `CYPRESS_` variables reach the tests without changing a task's hash.

To run the app first, have the task wait for its server in the package's `axle.toml`, with `dependsOn = ["dev"]` and a [ready check](https://axlers.dev/docs/reference/configuration.md#tasks) on `dev`.

## Scripts the repository already has

A package's own script wins. When its `package.json` has a script with a task's name that runs something else, such as `"e2e": "start-server-and-test dev 3000 cypress run"`, the plugin leaves that task alone and Axle runs the script. A script that only runs the same command is replaced by the plugin's task, which runs the same command and adds its settings. The repository's `axle.toml` changes a task as it would any other, and wins over the plugin.

## Settings

| Setting     | Default       | Meaning                                                     |
| ----------- | ------------- | ----------------------------------------------------------- |
| `e2e`       | `"e2e"`       | the name of the end-to-end task, or `false` to leave it out |
| `component` | `"component"` | the name of the component test task, or `false`             |
| `cache`     | `false`       | whether the tests are cached                                |

```toml
[plugins.cypress]
source = "npm:@axle-rs/plugin-cypress@0.1.0"
hash = "sha256-…"
component = "ct"
cache = true
```

The plugin's answers are kept until a `cypress.config.*`, a `package.json`, `pnpm-workspace.yaml` or a lockfile changes.
