-
Notifications
You must be signed in to change notification settings - Fork 7.3k
assert.deepEqual doing inadequate comparison #7161
Comments
As I think needed counterpart to Other test case that wrong for me: I hope that |
|
I think we could add a |
Can we mark |
as i see it, the CommonJS specs have a fault here—comparing two values with even considering that NodeJS i'm fully expecting someone to chime in once more with "it's frozen / 5 / locked / works as standardized", but i believe the standard is broken, NodeJS should fix that bit, and update the docs to tell people about the deviation. and yes, such a change will certainly break many tests, but then those test suites used to fail silently before, so that's a good thing. if anyone should disagree, please point out what useful properties a non-throwing |
to further encourage people to take it seriously when users say that assert.deepEqual( 3, '3' )
assert.deepEqual( [], {} )
assert.deepEqual( [{}], [[]] ) the first could conceivably fail with an exception to the effect that the very least action that should be taken is to amend the docs; they currently claim that i suggest to deprecate all affected equality-testing methods in red, then go and add an a further suggestion is to put a non-throwing |
Ideally the
Does this apply to any pair where either value fails the
This implies that all the remaining pairs are object pairs, which is incorrect no matter how the previous point is interpreted. For example, I'm surprised that whoever wrote and approved these specs didn't think of such obvious edge cases. I would suggest explicitly not following CommonJS and just doing whatever makes more sense. |
@seishun "I would suggest explicitly not following CommonJS and just doing whatever makes more sense."—i agree. there's good precedence in the JS community to implement standards because they're standards, not because they're particularly good standard. V8 does that with the ES specs, and at least you know what you get is pretty much 100% EcmaScript, not something vaguely similar like JScript. that said, it's up for discussion whether the relevant specs for i tend to lean towards the observation that (1) specs are not a God-given, they're man-made; (2) this particular part is very problematic; therefore (3) NodeJS should deliver a better implementation together with a set of rules that can in turn convince the CommonJS folks to turn around their cart for a significant improvement in their specs, v2. it just makes little sense to promote problematic software just because someone sat down and published a piece called 'the standard'. with JS that was a different story, and it's history. still, had they early on decided to ditch the earlier interpretation of your example js_type_of = function(x) {
return Object.prototype.toString.call(x);
};
isa_number = function(x) {
return (js_type_of(x)) === '[object Number]' && isFinite(x);
}; which yields i think we should be pragmatic and say that both sound deep and shallow equality testing is very much at the heart of every test-driven quality software, so let's do it right. |
so, which one? |
@vkurchatkin I mean in an ideal world it would follow CommonJS spec, but the spec is far from ideal. |
that would depend on community discussion. if the community feels i'm constantly in need of equality testing, and given i've some code to do type testing ready, it would mainly be a matter of forking https://github.com/jkroso/equals (or similar code base) and bundling everything. |
It would be nice to have a |
i've just published jsEq (npm: contributors are encouraged to create pull requests with more test cases and more library implementations or suggest such additions in the below. since JS the language lacks sane equality testing, it is probably a good idea to arrive at a community agreement on this important topic which comes fraught with a surprising number of hairy questions ( Update i've changed my mind about +0 vs –0 and updated the tests. read all about it on https://github.com/loveencounterflow/jseq |
As much as this can be covered by external modules, it's terribly silly that a core javascript assert module does not include this. |
`deepStrictEqual` works the same way as `strictEqual`, but uses `===` to compare primitives and requires prototypes of equal objects to be the same object. Fixes: nodejs/node-v0.x-archive#7161
`deepStrictEqual` works the same way as `strictEqual`, but uses `===` to compare primitives and requires prototypes of equal objects to be the same object. Fixes: nodejs/node-v0.x-archive#7161 Fixes: #620 PR-URL: #639 Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-by: Rod Vagg <[email protected]>
@joyent/node-coreteam ... recommend closing this unless we plan to backport the deepStrictEqual addition from io.js. |
Closing. There are other issues that deal with this and deepStrictEqual will come along with the io.js merge. |
I was bitten originally by what I believe to be a bug in nodeunit (I will create a pull request there if behavior is changed in node). Looking further nodeunit got this because it appears to have copied the assert.js code from node. The issue I specifically ran into is that the following sorts of values are considered equal (tested on 0.10.24 on OS X):
I then found that Mozilla had this problem previously with a Utils.deepEquals function as described in https://bugzilla.mozilla.org/show_bug.cgi?id=493363 so I modified the same test code from that bug, to work with node and adding 2 additional tests:
and found output:
which shows several things matching only because we're doing == instead of ===. Taking the assert.js from the v0.10 branch and applying this patch:
the results become:
which seems correct. Back to my original examples after applying this change, they also fail as I'd expect:
Any chance that this could get applied? Looking at master, it seems the issue still exists there as well though the typeof check has changed to using util.isObject instead so the patch won't apply as-is. It appears to be the same 1 character (and comment) fix there though.
The text was updated successfully, but these errors were encountered: