feat(expect): add expect.fn() mock functions with awaited assertions#41884
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Pull request overview
Adds an expect.fn() mock function facility to Playwright Test, including retrying/awaited mock assertions, plus protocol/serialization support so page-side callback stubs can synchronously consume configured return values while still routing calls back for recording.
Changes:
- Introduce
expect.fn()and 15 retrying mock matchers (toHaveBeenCalled*,toHaveReturned*,toHaveResolved*) with fail-fast when outcomes become terminal. - Extend the serialized value wire format with
fn_rvto carry mock return values alongside callback bindings. - Add coverage tests for in-process mocks and page-exposed callback mocks.
Reviewed changes
Copilot reviewed 13 out of 16 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| utils/generate_types/overrides-test.d.ts | Adds public type surface for MockFunction, mock matchers, and expect.fn(). |
| utils/generate_types/index.js | Ensures MockFunction is included in generated type outputs. |
| tests/playwright-test/expect-fn.spec.ts | New test suite validating mock call/result tracking and retrying assertions. |
| tests/page/page-evaluate-callback.spec.ts | Adds end-to-end tests for exposing expect.fn() into the page and recording calls/results. |
| packages/protocol/src/validator.ts | Accepts new fn_rv shape in protocol validation. |
| packages/protocol/src/structs.d.ts | Extends SerializedValue typing with fn_rv. |
| packages/protocol/src/serializers.ts | Deserializes fn_rv and attaches callback return values to the dummy function. |
| packages/protocol/spec/serialized.yml | Documents the fn_rv field in the wire format spec. |
| packages/playwright/types/test.d.ts | Updates generated public types to include mocks and matchers. |
| packages/playwright/src/matchers/mockFunctions.ts | Implements mock function state tracking and retrying mock matchers. |
| packages/playwright/src/matchers/matchers.ts | Extracts reusable timeout failure message formatting. |
| packages/playwright/src/matchers/expectLibrary.ts | Re-exports equals for reuse in mock matcher comparisons. |
| packages/playwright/src/matchers/expect.ts | Wires expect.fn() and mock matchers into the expect implementation and expect.poll restrictions. |
| packages/playwright-core/src/client/jsHandle.ts | Serializes callback return values (fn_rv) when passing functions into the page. |
| packages/isomorphic/utilityScriptSerializers.ts | Parses/serializes fn_rv in the browser-side serializer and defines callback-return-value helpers/constants. |
| packages/isomorphic/index.ts | Re-exports utility script serializers from the isomorphic index. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
11c9a60 to
63f1481
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
expect.fn() creates a mock function that records calls, results and settled results. Mock assertions like toHaveBeenCalledWith() and toHaveResolvedWith() are asynchronous and retried until they pass or the expect timeout is reached, so mocks can be handed to concurrent code, e.g. exposed into the page via page.evaluate(). Assertions whose outcome can no longer change fail fast instead of waiting. Implementations are async-only because they run in the test process. Values set with mockReturnValue() are serialized along with the function and consumed synchronously by the page-side callback stub.
Test results for "MCP"3 failed 7757 passed, 1249 skipped Merge workflow run. |
Test results for "tests 1"1 failed 4 flaky50191 passed, 1189 skipped Merge workflow run. |
|
Hi, I'm the Playwright bot and I took a first look at the CI failures here. 🔴 One failure looks caused by this PR
DetailsThis PR adds Caused by this PR
Pre-existing flake / infra The latest merged MCP report lists three failures; the two below flip verdict across many unrelated PRs in the DB, and
Uncertain
Triaged by the Playwright bot - agent run |
|
Hi, I'm the Playwright bot and I took another look at CI after the latest push. 🟡 No failure I can pin on this PR, but one is unprovenThe DetailsThe PR adds Fixed since the last report
Pre-existing flake / infra
Uncertain
Triaged by the Playwright bot - agent run |
Summary
expect.fn(implementation?)creates a mock function that recordsmock.calls/mock.results/mock.settledResults/mock.lastCall, withmockClear/mockReset(vitest semantics),mockImplementation[Once],mockReturnValue[Once],mockResolvedValue[Once],mockRejectedValue[Once],mockName.toHaveBeenCalled*,toHaveReturned*,toHaveResolved*) that retry until they pass or the expect timeout is reached, with fail-fast when the outcome can no longer change.mockReturnValue()values are serialized along with the function (newfn_rvnode in the wire format) and consumed synchronously by the page-side callback stub, while every call is still routed back for recording.