feat(expect): add toThrow, resolves, rejects, and strict value matchers (ENG-1286)
OK
2d ago+1348+1348+0123456789012345678901234567890123456789-148-148-012345678901234567890123456789
What
expect(value) gets the matchers a Jest or Playwright suite uses on plain values:
expect(() => parsePrice('abc')).toThrow(/not a price/); // also: no argument, a string, a class, an error, an asymmetric matcher
expect(order).toStrictEqual(new Order({ id: 7 })); // class types, undefined properties, array holes count
expect(rows).toContainEqual({ sku: 'a', status: 'paid' });
expect(error).toBeInstanceOf(NotFoundError);
expect(totals).toEqual({ lineTotal: expect.closeTo(59.99) });
await expect(loadOrder(7)).resolves.toMatchObject({ status: 'paid' });
await expect(loadOrder(-1)).rejects.toThrow('no such order');
.resolvesand.rejectscarry every value matcher, take.notbefore or after, and work underexpect.soft.- The four new matchers are also on
expect.poll, through the derived type. - An
Errorin any value matcher's failure message now reads[PaymentError: card declined] {"status":402}where it read{}or only its own properties.
Why
People who port a suite expect these on expect(value). The expect reference listed them under "Not included" and the Playwright migration page told readers to work around them. The owner decided on 2026-11-13 to build them. expect.extend stays out (ENG-1306), and so does toBeNaN, which no one asked for.
How it works
- The equality is the one
toEqualalready used,equalsfrom@vitest/expect.toStrictEqualpasses it Jest's four strict testers (packages/e2e/src/expect/values.ts:107). No new dependency. toContainEqualshares one helper with the collection form oftoContain, which already compared bytoEqualrules, so the two agree on a collection (values.ts:229). A string is refused.toThrowcalls the function one time and hands the thrown value tomatchesThrown(packages/e2e/src/expect/thrown.ts:70): substring, RegExp withlastIndexreset,instanceof, or the same message down thecausechain for an error. A function that returned a promise fails with a hint to userejects.resolvesandrejectsare getters on the value expectation.promised(values.ts:506) builds one async function per value matcher from the same name listexpect.polluses, awaits the promise insettled(values.ts:478), then runs the synchronous matcher on the value or the rejection. Afterrejects,toThrowchecks the rejection without a call.- Each promised call goes through
track(packages/e2e/src/expect/retry.ts:90), the phase trackingexpect.pollhad. A test that returns without awaitingresolvesfails withSTEP_NOT_AWAITEDat the line of the call. The hint text inpackages/core/src/expect/poll-scope.tsnow namesresolvesandrejectstoo. softenfollows.resolvesand.rejectsas it followed.not(packages/core/src/expect/soft.ts:61).expect.closeTois an owned class sharing the closeness rule withtoBeCloseTo(packages/e2e/src/expect/close-to.ts:23).@vitest/expecthas one but does not export it.- Types:
PromisedValueExpectation<T, Kept>(packages/core/src/types.ts:1488) is the oldPollExpectationshape with a name of its own.PollExpectation<T>is now an alias of it, andKeptisundefinedunderexpect.soft, where a kepttoMatchSchemafailure resolves to nothing. - Failure text:
formatmoved topackages/e2e/src/expect/format.ts.formatStrictusesutil.inspecton one line, because JSON drops whattoStrictEqualcompares: class names,undefinedproperties, holes. It cuts no string, so the runner redacts a secret whole, and prints an error as[TypeError: boom], without its stack.
Where this differs from Jest, on purpose:
rejects.toThrow('boom')passes onPromise.reject('boom'). Jest reports that nothing was thrown when the rejection is not an error.expect(asyncFn).toThrow()fails as in Jest, with a message that points atrejects.expect(asyncFn).not.toThrow()passes as in Jest.toContainon a collection already compares by deep equality here, sotoContainEqualadds the name, not a new comparison.
Self-review findings fixed before opening: a failed resolves no longer sets the rejection as cause, which made the report take its source line from the rejection's stack; errors keep their own properties in a message; strict output stays on one line and prints asymmetric matchers by description.
Issue
ENG-1286
Verified
Ran it locally: yes
- testbed, built CLI:
node node_modules/e2e/dist/cli/bin.js runontests/value-matchers.e2e.ts(in this PR, every matcher passing) and on a scratch file with one failing test per matcher.
✓ |web| tests/value-matchers.e2e.ts > the unit-style value matchers run in a test, awaited forms included 235ms
ASSERTION_FAILED: expected Order { id: 1 } to strictly equal { id: 1 }
ASSERTION_FAILED: expected { id: 1, note: undefined } to strictly equal { id: 1 }
ASSERTION_FAILED: expected { id: 'a', note: undefined } to strictly equal { id: Any<Number> }
ASSERTION_FAILED: expected [{"sku":"a"}] to contain an item equal to {"sku":"b"}
ASSERTION_FAILED: expected Order { id: 1 } to be an instance of Error
ASSERTION_FAILED: expected the function to throw, but it did not throw
ASSERTION_FAILED: expected the function to throw an error with a message containing "expired", but it threw [PaymentError: card declined]
ASSERTION_FAILED: the charge: expected the function to throw an instance of TypeError, but it threw [PaymentError: card declined]
ASSERTION_FAILED: expected the function to throw, but it returned a promise; for an async function use `await expect(promise).rejects.toThrow()`
ASSERTION_FAILED: expected {"total":0.31} to equal {"total":"CloseTo 0.3 (2 digits)"}
ASSERTION_FAILED: expected 1 to be 2
ASSERTION_FAILED: expected the promise to resolve, but it rejected with [PaymentError: card declined]
ASSERTION_FAILED: expected the promise to reject with an error with a message matching /expired/, but it rejected with [PaymentError: card declined]
INVALID_ARGUMENT: expect(...).resolves requires a promise, got 1
STEP_NOT_AWAITED: the test body returned before expect(...).resolves.toBe(...) finished; put `await` in front of every expect.poll, resolves, rejects, and toPass matcher
ASSERTION_FAILED: 2 soft assertions failed
1. expected 1 to be 2
2. expected the promise to reject, but it resolved with 1
- A failed
resolveson arequestcall that rejected points at the test line, not at the rejection:
ASSERTION_FAILED: expected the promise to resolve, but it rejected with [TestError: GET http://127.0.0.1:1/nope failed: fetch failed: bad port] {"category":"test","code":"ACTION_FAILED","retryable":false}
❯ tests-evm/fail.e2e.ts:4:3
4| await expect(request.get('http://127.0.0.1:1/nope')).resolves.toBeO…
- Unit tests in
packages/e2e/tests/unit/expect-unit-matchers.test.tswere mutation-checked: dropping the strict testers, the substring rule, the cause-chain length check, the half-unit rule ofcloseTo, the rejection flag, and the resolved-promise failure each turned at least one test red. - No terminal recording: the layout of the CLI output is unchanged, only the message text, shown above.
- No docs screenshot: the docs site does not build locally.
pnpm docs:checkpasses.
Summary by CodeRabbit
- New Features
- Added value assertions for strict equality, deep containment, instance checks, thrown errors, and approximate numbers.
- Added
.resolvesand.rejectsfor checking promise outcomes, including with negated, soft, and polling assertions.
- Documentation
- Expanded assertion references, migration guides, and test-writing examples with the new matchers and promise assertion guidance.