A custom Playwright reporter takes about 26 lines, but the obvious way to count failures in it is wrong. On a 13-test suite with 2 failing tests, a reporter that counted failed results in onTestEnd reported 8 failures, in 5 of 5 runs.
I wrote a minimal reporter, then measured four places where the reporter API does not do what a first version expects. The reporters, the seeded suite and the raw output of every run are in the PoC repo.
The suite
- 13 tests against a small cart page built in the test file. 8 pass, 2 always fail, 2 fail on the first attempt and pass on retry, 1 is skipped.
-
retries: 2,workers: 4,fullyParallel: true. - Playwright 1.65.1 with Chromium, Node v24.14.1, macOS on arm64.
- 5 runs with three reporters at once: the minimal reporter, a probe that records every hook call to a JSON file, and a reporter with an async
onTestEnd. Then 1 run with--max-failures=1 --workers=1, and 2 runs withplaywright-smart-reporter.
A minimal reporter
A reporter is a class with optional hooks. This one prints one line per attempt and a summary at the end:
import type { FullConfig, Reporter, Suite, TestCase, TestResult, FullResult } from '@playwright/test/reporter'; export default class MinimalReporter implements Reporter { private suite!: Suite; onBegin(config: FullConfig, suite: Suite) { this.suite = suite; console.log(`Running ${suite.allTests().length} tests with ${config.workers} workers`); } onTestEnd(test: TestCase, result: TestResult) { const attempt = result.retry ? ` (retry ${result.retry})` : ''; console.log(`${result.status.padEnd(8)} ${test.title}${attempt}`); } onEnd(result: FullResult) { // test.outcome() looks at every attempt of a test, so each test counts once const counts = { expected: 0, unexpected: 0, flaky: 0, skipped: 0 }; for (const test of this.suite.allTests()) counts[test.outcome()]++; console.log(`passed ${counts.expected}, failed ${counts.unexpected}, flaky ${counts.flaky}, skipped ${counts.skipped}: ${result.status}`); } printsToStdio() { return true; } } Point the config at the file:
export default defineConfig({ reporter: [['./reporters/minimal-reporter.ts']], }); printsToStdio() tells Playwright whether the reporter writes to the terminal. The Playwright docs say to return false when it does not, so Playwright can add its own output. This one prints, so it returns true. The first run printed this:
Running 13 tests with 4 workers passed shows the heading passed lists three items passed shows the total passed places an order passed ignores a bad coupon passed applies a coupon passed logs the item count passed toggles gift wrap failed always fails: missing button failed always fails: wrong total failed flaky: fails on the first attempt failed flaky: passes on retry passed flaky: fails on the first attempt (retry 1) skipped skipped: saved carts passed flaky: passes on retry (retry 1) failed always fails: missing button (retry 1) failed always fails: wrong total (retry 1) failed always fails: missing button (retry 2) failed always fails: wrong total (retry 2) passed 8, failed 2, flaky 2, skipped 1: failed That output already shows the first trap.
Trap 1: onTestEnd runs once per attempt, not once per test
13 tests made 19 onTestEnd calls in every run. Each retry is a new TestResult for the same TestCase. 8 of the 19 calls had status: 'failed': 3 attempts for each of the 2 failing tests, and 1 for each of the 2 flaky tests.
So a reporter that counts failed results in onTestEnd reports 8 failures. The suite has 2 failing tests and 2 flaky ones. test.outcome() in onEnd gave the right answer in 5 of 5 runs: 2 failed, 2 flaky, 8 passed, 1 skipped. It returns expected, unexpected, flaky or skipped from all the attempts of a test.
The fix is to count tests, not results. Do the counting in onEnd with suite.allTests() and test.outcome(), as the minimal reporter does. Use onTestEnd for things that are about one attempt, such as a log line or an attachment.
Trap 2: the order of onTestEnd changes from run to run
The 19 onTestEnd calls came in a different order in each of the 5 runs: 5 runs, 5 distinct orders. With 4 workers, a test's result arrives when its worker finishes it. A reporter that writes results in arrival order produces a different file for the same suite every run. That breaks a diff of two reports. If the output needs a stable order, sort it in onEnd, for example by file and line from test.location.
Trap 3: Playwright does not wait for an async onTestEnd
This trap loses data without an error. I gave a reporter an async onTestEnd that waits 200 ms, as a stand-in for an upload or a file write, and counted how many of those steps had finished:
- When
onEndran: 18 of 19. - When
onExitran: still 18 of 19. - Same in 5 of 5 runs.
The last step had not finished when the run ended. The reporter types in 1.65.1 explain why. onTestEnd returns void, so Playwright does not await the Promise. onEnd and onExit can return a Promise. A reporter that uploads each result from onTestEnd can lose the last ones, and nothing in the output says so.
The types point to the fix: keep the Promises from onTestEnd in an array and await Promise.all() on them in onEnd. I did not measure that version. The 18 of 19 result depends on the 200 ms delay and on how long the last tests take. A longer step can leave more calls unfinished.
Trap 4: --max-failures hides tests from your hooks
With --max-failures=1 --workers=1, the run stopped after the first failing test used its 3 attempts. 9 of the 13 tests reached onTestEnd, in 11 calls: 8 passes and 3 attempts of always fails: wrong total. The other 4 tests got no onTestBegin and no onTestEnd call.
3 of those 4 would have run: the second always-failing test and both flaky tests. The 4th is the skipped test, which gets an onTestBegin and an onTestEnd call with status: 'skipped' in a full run.
A reporter that builds its list of tests from onTestEnd sees 9 tests and does not know that 4 are missing. test.outcome() in onEnd still sees all 13. It reports all 4 as skipped. So the run shows 4 skipped tests where a full run shows 1, and a failing test and two flaky tests disappear into that count. Playwright also made 1 onError call in that run.
In this run, the cut-off tests and the marked skip looked the same: no hook calls, outcome skipped. If your reporter shows a skipped count after --max-failures, say that the run stopped early instead of reporting the count as it is.
The other hooks are busy
Hook calls per run of 13 tests:
| Hook | Calls |
|---|---|
onBegin | 1 |
onTestBegin | 19 |
onTestEnd | 19 |
onStepBegin | 255 |
onStepEnd | 255 |
onStdOut | 1 |
onEnd | 1 |
onStepBegin and onStepEnd ran about 13 times as often as onTestEnd. Work you put in a step hook runs 255 times for 13 small tests. The one onStdOut call is the one console.log in a test.
What playwright-smart-reporter does with the same suite
playwright-smart-reporter is a reporter I maintain. I ran version 4.0.1 on the same suite, once in each mode, and read its JSON export.
| Default run | --max-failures=1 --workers=1 | |
|---|---|---|
| Total | 13 | 13 |
| Passed | 10 | 8 |
| Failed | 2 | 1 |
| Flaky | 2 | 0 |
| Skipped | 1 | 4 |
| Pass rate | 83 | 89 |
| Failure clusters | 2 | 1 |
- It counts tests, not attempts: 2 failed, not 8. It reports the 2 flaky tests as flaky and also counts them as passed, so passed is 10.
- The pass rate leaves skipped tests out: 10 of 12 in the default run, 8 of 9 in the stopped run.
- It groups failures into clusters. Here that is 2 clusters, one per failing test, because the two tests fail with different errors.
- It has the same blind spot as
test.outcome()in trap 4. The stopped run shows 4 skipped tests: the 3 that were cut off and the 1 marked skip. Its pass rate of 89 is higher than the 83 of the full run, because the 3 cut-off tests are out of the sum.
What didn't work
- The async fix in trap 3 comes from the reporter types. I did not run it.
- One seeded suite of 13 tests on one machine. The
onTestEndorder depends on worker scheduling, and the async result depends on the delay and on the timing of the last tests. - I ran
playwright-smart-reporteronce per mode, without history. Its flakiness scores across runs need history, so this test does not cover them.
What I'd do
- Count failures in
onEndwithtest.outcome(). Never count failed results inonTestEnd. - Sort the output in
onEndif anyone diffs it. - Await any async work from
onTestEndinonEnd. - With
--max-failures, do not trust a skipped count or a pass rate. The tests that were cut off count as skipped.
For a reporter that only prints, 26 lines are enough; for a reporter that counts, uploads or diffs, test it with retries, workers and --max-failures before you trust its numbers. For more on what reports leave out, see Playwright 1.65 onlyFailures.
The reporters, the suite and the raw results: poc-playwright-custom-reporter.
Originally published at https://garyparker.dev/blog/writing-a-custom-playwright-reporter

