chaijs / chai

BDD / TDD assertion framework for node.js and the browser that can be paired with any testing framework.
https://chaijs.github.io
MIT License
8.11k stars 694 forks source link

Feature Request: a hook to preprocess values that crash Chai diff generation #1620

Closed jedwards1211 closed 3 months ago

jedwards1211 commented 3 months ago

I've had two pain points with Chai assertions in projects that use Sequelize:

Note: the default truncateThreshold isn't preventing this. I am on Chai 4, and maybe Chai 5 somehow avoids these problems, but I'm unable to try Chai 5 yet since it's pure ESM.

It would be nice to systematically solve this problem once and for all. There are several ways I could work around this:

I don't really like any of these options; the latter two don't systematically eliminate the problem, because we would have to patch every existing test file (unless we rewire chai imports, I guess) and avoid naively using expect in new tests going forward. And also, modifying the input value could alter the assertion result -- I really just wish I could hook Chai itself to do this preprocessing between when the assertion fails and when it creates diffs.

I don't see any kind of option to do this in the docs; do you agree it would be a helpful feature?

jedwards1211 commented 3 months ago

Oh. Looking through the code, it seems like v4 just passes a showDiff flag to AssertionError, and I guess Mocha is what does the actual diffing?

jedwards1211 commented 3 months ago

Yep, I had to monkey-patch require('mocha/lib/utils').canonicalize.