Files
e2e/docs/github.mdx
T

146 lines
6.4 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: Pull request comments
description: The @e2edev/github reporter posts one comment per run from GitHub Actions and keeps it current.
---
A run that fails in CI is a red check and a log to open. `@e2edev/github` is
a [reporter](/reference/reporters) that turns it into one pull request
comment: the counts, every test that did not pass with its error and
evidence, and links to the source lines and to the workflow run holding the
screenshots, traces, and recordings. A rerun edits the same comment. The same
text goes to the job summary, so it is there on a push too.
## Setup
```bash
npm install --save-dev @e2edev/github
```
```ts title="e2e.config.ts"
import type { E2EConfig } from 'e2e';
import { playwright } from '@e2edev/playwright';
import { github } from '@e2edev/github';
export default {
targets: [{ engine: playwright({ url: 'http://localhost:3000' }) }],
reporters: ['list', github()],
} satisfies E2EConfig;
```
Two lines in the workflow: the job may write pull request comments, and the
step that runs `e2e` gets the token.
```yaml title=".github/workflows/e2e.yml"
permissions:
contents: read
pull-requests: write
jobs:
e2e:
steps:
# ...checkout, install, browsers
- name: Run e2e
run: npx e2e run --reporter list,junit
env:
GITHUB_TOKEN: ${{ github.token }}
- name: Upload artifacts
if: ${{ !cancelled() }}
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: e2e-artifacts
path: .e2e/artifacts
```
Upload the artifacts on every non-cancelled outcome. The comment links to
the workflow run as the place to fetch them, and a flaky test that passed
on its second attempt can still leave evidence worth inspecting.
## The comment
```markdown
### 🔴 e2e: 1 failed, 1 flaky, 3 passed, 1 skipped
9 agent steps · 4 replayed from cache · 31 model calls · 128k tokens (62% cached) · $0.31
**🔴 tests/billing.e2e.ts › billing › upgrades to Pro**
**ASSERTION_FAILED** expected heading "Your cart" to be visible
Step 4 of 6, `agent.act` "Pick the Pro plan and pay", failed after 12 model calls in 38.0s: "The Pay button opened a page that still shows the Free plan."
Steps: ✓ open / › ✓ sign in as the owner › ✓ tap Upgrade › ✗ Pick the Pro plan and pay › 2 not run
Evidence: [screenshot, video, trace](https://github.com/acme/shop/actions/runs/123) · [tests/billing.e2e.ts:12](https://github.com/acme/shop/blob/1a2b3c/tests/billing.e2e.ts#L12)
**⚠️ tests/team.e2e.ts › invite a member by email**
Passed after 1 failed attempt; the last one hit **STEP_TIMEOUT** the invitation email took too long.
Evidence: [screenshot, trace](https://github.com/acme/shop/actions/runs/123) · [tests/team.e2e.ts:21](https://github.com/acme/shop/blob/1a2b3c/tests/team.e2e.ts#L21)
| | File | Tests | Agent | Time |
| --- | --- | --- | --- | --- |
| 🔴 | tests/billing.e2e.ts | 1 failed | 3 steps · 19 calls | 41.2s |
| ⚠️ | tests/team.e2e.ts | 1 flaky | 2 steps · 8 calls | 12.0s |
| ⏭️ | tests/checkout.e2e.ts | 1 skipped | | 0ms |
| 🟢 | tests/dashboard.e2e.ts | 1 passed | 1 step · 1 call | 2.1s |
| 🟢 | tests/todos.e2e.ts | 2 passed | 3 steps · 3 calls | 7.3s |
<details>
<summary>All 6 tests</summary>
**tests/billing.e2e.ts**
- 🔴 billing › upgrades to Pro (41.2s)
...
</details>
Screenshots, traces, and recordings: [run artifacts](https://github.com/acme/shop/actions/runs/123).
<sub>e2e 0.13.0 · 2m 15s · 1 target (web)</sub>
```
The headline carries the counts; the line under it what the run spent, when
it used the agent: agent steps, how many the trace cache replayed, model
calls, tokens with the share the provider served from its prompt cache, and
the estimated cost. Each test that failed or was flaky gets a block: the
error, the step it went wrong at with the agent's own explanation of what it
saw, the attempt's step timeline, the evidence, and a link to the line in the
test file that declared the test, at the pull request's head commit (a test
declared through a project helper that wraps `test()` links to the helper
call, not into the helper). Then one row per test file, worst first, and
every test folded away under it, grouped by file. A run-level error such as
`APP_UNREACHABLE` appears above the blocks. Titles, labels, and messages are
escaped and clipped, and the body stays under GitHub's size limit: past
thirty blocks the page says how many more did not pass, and when room runs
out the page is cut where it stands, the folded list first, and says so. The page is
[`renderMarkdownReport`](/reference/reporters), the one the built-in
`markdown` reporter writes to `.e2e/summary.md`.
The comment carries a hidden marker naming the config's `projectId`, the
workflow, and the job, so two configs or two jobs on one pull request each
keep their own comment. Matrix replicas of one job share all three, so give
each its own `key`:
```ts
reporters: ['list', github({ key: process.env.MATRIX_BROWSER })],
```
with `MATRIX_BROWSER: ${{ matrix.browser }}` in the step's `env`. Never use
the run id as the key: a rerun has a new one and would post a second comment.
## When nothing is posted
The reporter never changes the run's status or exit code. When it cannot
post, it says why in one row under the summary:
```
Report .e2e/report.json
GitHub not posted: set GITHUB_TOKEN in the step's env (GITHUB_TOKEN: ${{ github.token }}); written to the job summary
```
| Situation | What happens |
| --- | --- |
| Not on GitHub Actions | Nothing is written; the row says so. |
| A `push`, `schedule`, or `workflow_dispatch` run | The job summary is written; there is no pull request to comment on. |
| No `GITHUB_TOKEN` in the step's `env` | The job summary is written; the row shows the line to add. `GH_TOKEN` is read too. |
| A `pull_request` event from a fork | Its token is read-only, so the comment fails and the run prints the reason. A `pull_request_target` workflow has a writable token, with the care that event demands around secrets. |
| An `issue_comment` event | On a pull request's thread, the comment is posted without source links, since the checked-out commit is the default branch. |
| Missing `pull-requests: write` | The same failure, with the permission named. |
The reporter reads the token and the event payload when the run finishes,
never when the config loads, and the token never enters the report. GitHub
Enterprise Server works through `GITHUB_SERVER_URL` and `GITHUB_API_URL`.