When I try to use deep.equal assertion between function
expect(() => {}).to.deep.equal(() => {});
I got a AssertionError
AssertionError: expected [Function] to deeply equal [Function]
+ expected - actual
Is this a expected behavior? When not just use .toString() to compare between functions?
> (() => {}).toString()
'() => {}'
Hey @chentsulin thanks for the issue.
I have two points to make on this:
SameValue). Functions are not iterables, they have no keys which could make a good quality comparison, and as such are given the SameValue treatment.// Are these the same?
const foo = (x) => x;
const bar = (x) => { return x };
function baz(x) { return x }
// They have the same behaviours but different string representations
foo.toString() === 'function (x) => x'
bar.toString() === 'function (x) => { return x }'
baz.toString() === 'function baz(x) { return x }'
// Are these the same?
const foo = (x) => {
// foo
return x;
}
const bar = (x) => {
// bar
return x;
}
// They also are identical in behaviour, but return different strings:
foo.toString() === '(x) => {\n // foo\n return x;\n}';
bar.toString() === '(x) => {''n // bar\n return x;\n}';
Hopefully this illustrates the minefield that would be comparing functions by source. If a function has the same source, it is likely the same reference.
However, I'm going to go on a hunch and suggest you take a look at #644 which might solve the problems you're having in your tests. We will eventually have a matcher API whereby you can make loose assertions inside deep.equal, including on functions.
I'll close this issue - because I'm pretty sure you want #644. If not, then I think realistically we won't support loose equality on functions because of the above 2 points. Feel free to continue the discussion here, or in #644 if you have ideas there.
This just had me waste two days on failing tests. This should be explicitly called out on the docs, it's easy to slide right past the implications. :(
Most helpful comment
This just had me waste two days on failing tests. This should be explicitly called out on the docs, it's easy to slide right past the implications. :(