Skip to content

[Compat] Mocha Assertion Compatibility for updating actual, expected values #261

@nev21

Description

@nev21

Mocha tries to mutate actual and expected

Mocha’s reporters were written in an era when every assertion library used mutable AssertionError objects. The assumption baked into Mocha’s design is:

If an error has actual and expected, Mocha is allowed to massage them for display.

Mocha may stringify them, normalize them, or even replace them with prettified versions.

Mocha may also add err.showDiff = true or override it.

This is legacy behavior that predates libraries like tripwire, which treat assertion failures as immutable objects with readonly properties.

Why this is considered a “Mocha compatibility issue”

Tripwire is behaving correctly for a modern assertion library: immutable error objects are a good thing.

Mocha is behaving like Mocha always has:
it mutates error objects to produce diffs.

The two philosophies collide.

This is why the issue is described as a “Mocha compatibility issue” — Mocha’s reporter assumes mutability, and tripwire does not allow it.

What workarounds are possible

  1. Disable Mocha’s diffing for these tests
    If you throw an error without actual/expected, Mocha won’t try to mutate anything.
    Tripwire may have a config flag for this — waiting on maintainer guidance is the right move.

  2. Wrap tripwire’s error in a mutable error
    You can catch the AssertionFailure and rethrow a plain Error with a message only.
    You lose diffs, but you gain compatibility.

  3. Patch Mocha’s reporter locally
    You can override the part of Mocha that mutates err.actual and err.expected. This is hacky but works.

Why Mocha hasn’t fixed this yet

Mocha’s diffing logic is deeply intertwined with its reporters and has been stable for years. Changing it risks breaking:

  • Node’s built‑in assert
  • Chai
  • Expect.js
  • Dozens of custom assertion libraries

So Mocha tends to preserve the old behavior unless a library becomes widely used enough to justify a change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    compatA compatibility issuemochaA mocha compatibility issue

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions