Skip to content

feat(expect/unstable): implement toThrowErrorMatchingSnapshot() - #7332

Open
anyingiit wants to merge 1 commit into
denoland:mainfrom
anyingiit:expect-to-throw-error-matching-snapshot
Open

anyingiit wants to merge 1 commit into
denoland:mainfrom
anyingiit:expect-to-throw-error-matching-snapshot

Conversation

@anyingiit

Copy link
Copy Markdown

Description

Implements expect(fn).toThrowErrorMatchingSnapshot(hint?), one of the two APIs that expect/mod.ts still lists as unavailable (tracking issue #3964 asks for one PR per API, so the inline variant is left for a follow-up).

Behaviour follows Jest:

  • the received function is called and the message of the thrown error is compared with the stored snapshot (-- --update writes it, exactly like toMatchSnapshot());
  • with .rejects, the rejection reason's message is used;
  • if the function does not throw, it fails with Received function did not throw;
  • .not is rejected with the same error as the other snapshot matchers;
  • it shares the per-test snapshot counter with toMatchSnapshot(), so both can be mixed in one test.

To avoid duplicating the snapshot bookkeeping, the body of toMatchSnapshot() (test path/name lookup, key/counter, assert vs. update mode) moved into a private matchSnapshot() helper that both matchers call. toMatchSnapshot() behaves as before, including the order of its checks; only the matcher name in the "Unable to determine test file path/name" errors is now parameterised. The new matcher is marked @experimental like toMatchSnapshot(), and mod.ts now lists it as supported.

Motivation / disclosure: I had some spare AI-assistant quota (Claude Code) and am using it to try to
help projects with open good-first-issues. The change was prepared with Claude Code and verified as listed
below. If it doesn't fit, isn't up to your bar, or you'd simply rather not take it — please feel free to
close it, no hard feelings at all 🙂

Related issue

Part of #3964

Checklist

  • Tests pass locally (deno test -A --doc --parallel --trace-leaks expect/ → 237 passed, 0 failed; the new expect/_to_throw_error_matching_snapshot_test.ts has 9 tests that all fail on main and pass with this change; deno lint, deno fmt --check, deno task lint:docs, lint:mod-exports, lint:export-names, lint:unstable-deps, lint:circular and test:browser all pass)
  • CHANGELOG.md is updated (if applicable) — n/a, release notes are generated from the PR title
  • Documentation is updated (if applicable) — JSDoc on Expected.toThrowErrorMatchingSnapshot and the supported/unsupported list in expect/mod.ts

@CLAassistant

CLAassistant commented Sep 25, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@codecov

codecov Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.75000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.04%. Comparing base (2958335) to head (9e31769).

Files with missing lines Patch % Lines
expect/_matchers.ts 93.47% 1 Missing and 2 partials ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main    #7332    +/-   ##
========================================
  Coverage   95.04%   95.04%            
========================================
  Files         619      618     -1     
  Lines       52012    51799   -213     
  Branches     9450     9411    -39     
========================================
- Hits        49433    49231   -202     
+ Misses       2031     2023     -8     
+ Partials      548      545     -3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants