-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve comparison/diff #121
Comments
It is a bit similar to the object equality question although here it relates on how things get serialized. The diff algorithm (a great third party library) works on the JSON specification. Dates, Prototypes, etc have nothing to do with the JSON specification so it serializes things first loosing information (how would you encode a constructor or a prototype in a JSON for example ? ). If you want to get full control you'd need to take the control on the serialization to enrich the information with JS meta : constructors, prototypes, etc; then find a format to print it properly and compare the result as strings only. That's a lot of work and you can't simply cover everything: for example a lot of reporting tools use the constructor but quid of objects created with Also It can't simply be consistent: your "serious" problem would not even fail in Jest. Ava, would report a constructor mismatch but if you write the code const LoginProto = {};
const UserProto ={};
const createLogin = ({name}) => Object.create(LoginProto, {name:{value:name}});
const createUser = ({name}) => Object.create(UserProto, {name:{value:name}}); and compare a "Login" with a "User" the test would not fail either whereas semantically it is the same code than the one written with classes JSdiff handles the complexity of the serialization and the diffing, it has its tradeoffs of course, but I am quite ok with it. Maybe we can work on something specific for native JS classes such Dates, Buffers, etc. But anyway if the test fails whereas you don't see any diff, it simply means you have a type mismatch and TS should catch it, should not it ? |
you can have a test playground in the following gist |
As reported in #120, I ran into a problem with
Assert.equal
, where the assertion decided the objects were equal, but thediffReporter
was unable to report any difference.I finally isolated the problem: one of (what I thought was) my JSON models had a
Date
in it - this is fine when passed throughJSON.stringify
which produces exactly the date format I was expecting, so this was rather tricky to isolate.I now have a repro:
So the assertion fails, but
diffReporter
disagrees and reports no difference:This applies to object comparisons only - so this behavior isn't consistent with value comparisons, for example:
This works as expected:
The test in
fast-deep-equal
where this fails isa && b && typeof a == 'object' && typeof b == 'object'
, becausetypeof b
isstring
and notobject
- so the library is right, aDate
is not equal to astring
, completely reasonable.So the problem is
diffReporter
, which doesn't recognize this difference.I suspect the problem is here:
zora/reporters/src/diff/diagnostic/equal.js
Lines 148 to 160 in a68f308
Here it decides that the objects are equal types because they're both
object
- it then takessameTypeDiff[expectedType]
whereexpectedType
isstring
, and so they get diffed as strings.diffStrings
passes this along todiffChars
from thediff
package:zora/reporters/src/diff/diagnostic/equal.js
Lines 32 to 46 in a68f308
My guess is, it was asked to diff a string, so it converts the
Date
to a string, and there you go.I'm unsure precisely how we can fix this, but I would think it starts with the type comparison in
diff/diagnostic/equal.js
, which, yes, compare the types, but something else is missing here.I call having a similar problem in the past, where I ended up doing a custom serialization to strings - something JSON-like but with types, so:
Would output something like:
Special formatting is still required for
Date
since it's a sort of "value-typed object".For the same reasons, for Node, there is
util.inspect
, which produces an output similar to this:Of course, we would need support in the browser as well, so we can't depend on Node's
util.inspect
- a quick search reveals this replacement package, which explains:One more use-case to demonstrate the seriousness of this problem:
Both of these tests will fail, reporting no difference.
The text was updated successfully, but these errors were encountered: