feat(expect): add toThrow, resolves, rejects, and strict value matchers (ENG-1286)

OK
2d ago
+1348-148

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');
  • .resolves and .rejects carry every value matcher, take .not before or after, and work under expect.soft.
  • The four new matchers are also on expect.poll, through the derived type.
  • An Error in 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

  1. The equality is the one toEqual already used, equals from @vitest/expect. toStrictEqual passes it Jest's four strict testers (packages/e2e/src/expect/values.ts:107). No new dependency.
  2. toContainEqual shares one helper with the collection form of toContain, which already compared by toEqual rules, so the two agree on a collection (values.ts:229). A string is refused.
  3. toThrow calls the function one time and hands the thrown value to matchesThrown (packages/e2e/src/expect/thrown.ts:70): substring, RegExp with lastIndex reset, instanceof, or the same message down the cause chain for an error. A function that returned a promise fails with a hint to use rejects.
  4. resolves and rejects are getters on the value expectation. promised (values.ts:506) builds one async function per value matcher from the same name list expect.poll uses, awaits the promise in settled (values.ts:478), then runs the synchronous matcher on the value or the rejection. After rejects, toThrow checks the rejection without a call.
  5. Each promised call goes through track (packages/e2e/src/expect/retry.ts:90), the phase tracking expect.poll had. A test that returns without awaiting resolves fails with STEP_NOT_AWAITED at the line of the call. The hint text in packages/core/src/expect/poll-scope.ts now names resolves and rejects too.
  6. soften follows .resolves and .rejects as it followed .not (packages/core/src/expect/soft.ts:61).
  7. expect.closeTo is an owned class sharing the closeness rule with toBeCloseTo (packages/e2e/src/expect/close-to.ts:23). @vitest/expect has one but does not export it.
  8. Types: PromisedValueExpectation<T, Kept> (packages/core/src/types.ts:1488) is the old PollExpectation shape with a name of its own. PollExpectation<T> is now an alias of it, and Kept is undefined under expect.soft, where a kept toMatchSchema failure resolves to nothing.
  9. Failure text: format moved to packages/e2e/src/expect/format.ts. formatStrict uses util.inspect on one line, because JSON drops what toStrictEqual compares: class names, undefined properties, 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 on Promise.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 at rejects. expect(asyncFn).not.toThrow() passes as in Jest.
  • toContain on a collection already compares by deep equality here, so toContainEqual adds 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 run on tests/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 resolves on a request call 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.ts were mutation-checked: dropping the strict testers, the substring rule, the cause-chain length check, the half-unit rule of closeTo, 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:check passes.

Summary by CodeRabbit

  • New Features
    • Added value assertions for strict equality, deep containment, instance checks, thrown errors, and approximate numbers.
    • Added .resolves and .rejects for 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.