Repository navigation
Description
- Related
- Show the browser trace timeline in test failure output #11592 (browser trace timeline in failure output, which lists this as a follow-up)
- Trace View: Failure-Only Trace Recording #10310 (failure-only trace recording)
- Unify and simplify timeout configuration #9751 (per-test async context for timeouts)
- Add bail option to describe block (support
test.step) #7787 (test.step) - Design notes and staged plan: https://gist.github.com/hi-ogawa/c1b9e1f4facd6d2b2671ce5379c1db89
Clear and concise description of the problem
#11592 prints a browser test's trace steps under its error. Node tests that wait on something could use the same. When expect.poll or vi.waitFor times out, the error shows what didn't match, but not what ran before it or how long each step took. That can help with flaky integration tests, which currently need console.log calls to get the same information.
Suggested solution
Record async steps in Node tests and print the same timeline under the error when a test fails, behind experimental.traceTimeline. For example:
import { expect, test, vi } from 'vitest'
import { api, db, search } from './helpers'
test('finds new user in search', async () => {
await vi.waitFor(() => search.ping())
await api.post('/users', { name: 'alice' })
await vi.waitUntil(() => db.users.findByName('alice'))
await expect.poll(() => search.query('alice')).toHaveLength(1)
}) FAIL search.test.ts > finds new user in search
AssertionError: expected [] to have a length of 1 but got +0
...
Trace:
step start duration location
vi.waitFor() +2ms 120ms search.test.ts:5:12
vi.waitUntil() +171ms 42ms search.test.ts:7:12
expect.poll().toHaveLength() +214ms 1001ms search.test.ts:8:50
- Steps are the async waits Vitest already knows about:
expect.poll, thenvi.waitForandvi.waitUntil.expect(...).resolves/.rejectsandtoMatchFileSnapshotare candidates too. - Only failed attempts keep their steps, like
'failure-only'in Trace View: Failure-Only Trace Recording #10310. A passing test keeps a few entries in memory and sends nothing, so the option costs about nothing on passing tests. That leaves room to enable it by default later. - Locations resolve like error stacks, so a step inside a helper file points at the helper, and a step inside
vi.defineHelperpoints at the line that called the helper. - The data is the same trace artifact browser mode records, so Vitest UI and the HTML reporter can show it once the trace viewer handles steps without a DOM snapshot, which feat(browser): add failure-only trace view mode #11612 does for failure-only browser traces.
vi.waitFor and a global expect.poll have no test of their own, so they're attributed to the test that's currently running. That's exact for sequential tests. Under test.concurrent the current test is ambiguous, so steps are skipped when more than one test is running. A test that times out but keeps running in the background can still leave a step in the next test's timeline. Vitest has the same limitation for console output and the global expect, and an AsyncLocalStorage-based test context would remove it, which #9751 needs for per-test timeouts as well.
Alternative
experimental.openTelemetry can export the same kind of timing, but reading it means setting up an SDK and a trace viewer and finding the test's spans there. The timeline shows the steps under the error, with no setup.
Additional context
Possible follow-ups:
- A way for tests and helpers to name their own steps, like
page.markin browser mode. This also covers most of what Add bail option to describe block (supporttest.step) #7787 asks for under the nametest.step. Custom matchers could record steps through the matcher context. - A
messageoption onvi.waitForandvi.waitUntilthat names the step and goes into the timeout error, such asvi.waitUntil(() => db.users.findByName('alice'), { message: 'user saved' }).expect.pollalready takesmessage, which could name its step the same way. - An
'on'mode that also records passing attempts, so the UI can show their steps, as browser mode has. - Hooks and fixtures as steps, since slow setup is a common cause of a timeout blamed on the test body.
- A per-test async context with
AsyncLocalStorage, shared with Unify and simplify timeout configuration #9751.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsApproved
Activity