Skip to content
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

Using fluture/test/assertions.js with Json is never reference-equal #35

Open
dotnetCarpenter opened this issue May 11, 2021 · 3 comments

Comments

@dotnetCarpenter
Copy link
Contributor

Using fluture/test/assertions.js with Json is never reference-equal when Json is imported from a commonjs module and esm module and compared with equivalence.

When using Json from 'fluture-express', if you test subject is a commonjs module, then the value in the Json object is not reference-equal and tests with equivalence from 'fluture/test/assertions.js' will fail even though the tests should pass.

Error:
	resolve ({"body": {"data": {"foo": "bar"}}, "head": []}) :: Future({ <resolved> {"body": {"data": {"foo": "bar"}}, "head": []} })
is not equivalent to:
	resolve ({"body": {"data": {"foo": "bar"}}, "head": []}) :: Future({ <resolved> {"body": {"data": {"foo": "bar"}}, "head": []} })

The inner values are not equal: Values have same structure but are not reference-equal

Example of error message when Json is not imported via esm.

However, note that it does not matter if your test data is from a commonjs module or EcmaScript module (esm).

A thorough run down is in the https://github.com/dotnetCarpenter/fluture-test-gotcha repository.

It boils down to strictEqual and show from 'sanctuary-show' not playing nice together. I guess that the output of show (a) changes depending on module format. But that is a guess.

// always success
deepStrictEqual (Json (data1), Json (data1));
deepStrictEqual (Json (data2), Json (data2));
// always failure
strictEqual (Json (data1), Json (data1));
strictEqual (Json (data2), Json (data2));

strictEqual.mjs and strictEqual.js

@dotnetCarpenter
Copy link
Contributor Author

equivalence checks the inner value of two Futures using assert.deepStrictEqual.
That in turn test the prototypes of the inner value.
Because Json has a CommonJS version and a ESM version, those two prototypes
will be different and equivalence will fail for two seemingly identical values.

@Avaq
Copy link
Member

Avaq commented May 14, 2021

We are working on a solution, which is twofold:

  1. Fix some issues with value equality #32
  2. Update the equals function to fall back to structural equality sanctuary-js/sanctuary-type-classes#154

Once those have both landed, I will update the default equivalence function to use the new Z.equals instead.

Until then, you can use makeEquivalence with a custom equality assertion that wouldn't treat these objects as differently. Perhaps this will do:

const equivalence = makeEquivalence (a => b => {
  if (Z.equals (a, b) || util.isDeepStrictEqual (a, b)) return;
  throw new AssertionError (...)
})

@dotnetCarpenter
Copy link
Contributor Author

Thanks! I will try it out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants