Skip to content

fix(assert): hint that function properties compare by reference#7165

Open
LeSingh1 wants to merge 1 commit into
denoland:mainfrom
LeSingh1:assert-equals-function-hint
Open

fix(assert): hint that function properties compare by reference#7165
LeSingh1 wants to merge 1 commit into
denoland:mainfrom
LeSingh1:assert-equals-function-hint

Conversation

@LeSingh1
Copy link
Copy Markdown

Fixes #6878.

When assertEquals throws on inputs that only differ in a function property, the diff prints both sides as the same [Function: name] string. The failure then looks like "both sides are identical, why did this throw?".

Repro from the issue:

import { assertEquals } from "jsr:@std/assert";
assertEquals(
  { x: 1, y: () => console.log(2) },
  { x: 1, y: () => console.log(2) },
);

Before:

error: AssertionError: Values are not equal.

    [Diff] Actual / Expected

    {
      x: 1,
      y: [Function: y],
    }

After:

error: AssertionError: Values are not equal.
  Note: function properties are compared by reference, so two distinct
  functions print the same as `[Function: name]` but are not equal.

    [Diff] Actual / Expected

    {
      x: 1,
      y: [Function: y],
    }

Implementation: a small containsFunction walker checks own properties, array entries, Map values, and Set elements (cycle-safe via a WeakSet) and the hint is appended only when at least one operand contains a function. No change to equality semantics or to the diff itself.

Tests cover: the issue's object case, arrays of functions, and a negative case confirming the hint does not appear for function-free inputs. Full assert/ suite (140 tests) passes.

assertEquals throws on inputs that only differ in a function property,
but the diff prints both sides as the same [Function: name] string,
which makes the failure look like "both sides identical, why did it
throw?".

Detect function values anywhere in either operand (own properties,
array entries, Map values, Set elements; cycle-safe) and prepend a
short note explaining that functions compare by reference. No change
to the diff itself or to equality semantics.

Fixes denoland#6878
@codecov
Copy link
Copy Markdown

codecov Bot commented May 29, 2026

Codecov Report

❌ Patch coverage is 94.44444% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.57%. Comparing base (cdf74a8) to head (e0aa7b0).

Files with missing lines Patch % Lines
assert/equals.ts 94.44% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7165      +/-   ##
==========================================
- Coverage   94.57%   94.57%   -0.01%     
==========================================
  Files         636      636              
  Lines       52142    52178      +36     
  Branches     9401     9417      +16     
==========================================
+ Hits        49315    49349      +34     
  Misses       2249     2249              
- Partials      578      580       +2     

☔ View full report in Codecov by Sentry.
📢 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.

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.

@std/assert/equals - assertEquals() fails with function properties without displaying why

1 participant