Chai: redux-saga select with selection creators

Created on 4 Jul 2016  路  5Comments  路  Source: chaijs/chai

Hi i am using chai to assert my unit tests in my redux application.
I got an issue where chai tells me that the objects are not the same, though if i use expect it works fine. I think this is because one property is a returned function of a function.

I made a fiddle to make things clear.

Is there a way to test such a thing with chai too? I would like to stay with chai, though this is a must have for me.

Most helpful comment

@Neophy7e I implemented something like this:

/**
 * Chai Plugin for testing redux saga combined with selector creator functions
 * This expectEqual is implemented like the implementation of expect (https://github.com/mjackson/expect)
 */
import { Assertion, util } from 'chai';
import whyNotStrictlyEqual from 'is-equal/why';

const whyNotEqual = (a, b) =>
  (a == b ? '' : whyNotStrictlyEqual(a, b));

const isEqual = (a, b) => whyNotEqual(a, b) === '';

Assertion.addMethod('expectEqual', function(expected, msg) {
  if (msg) {
    util.flag(this, 'message', msg);
  }
  util.expectTypes(this, ['object']);
  const actual = util.flag(this, 'object');
  this.assert(
    isEqual(actual, expected)
    , 'expected #{this} to expectEqual #{exp}\n' + whyNotStrictlyEqual(actual, expected)
    , 'expected #{this} to not expectEqual #{exp}'
    , expected
    , this._obj
    , util.flag(this, 'negate') ? true : false
  );
});

this is not 100% fully tested so its kinda a draft. though it worked for my use cases.

All 5 comments

Hey @JuHwon thanks for the issue.

It is indeed because you have two functions - which although they have the same behaviour, are different references. There are a few "workarounds" I suppose:

I don't think we want to change the behaviour to make two functions return true for deep equality, so this (calling .deep.equal) will likely never work.

Thanks for your answer. I've written a custom helper method for my use case.

@JuHwon I'm running through the same issue. What did you finally chose to do, to be able to compare the two objects ?

@Neophy7e I implemented something like this:

/**
 * Chai Plugin for testing redux saga combined with selector creator functions
 * This expectEqual is implemented like the implementation of expect (https://github.com/mjackson/expect)
 */
import { Assertion, util } from 'chai';
import whyNotStrictlyEqual from 'is-equal/why';

const whyNotEqual = (a, b) =>
  (a == b ? '' : whyNotStrictlyEqual(a, b));

const isEqual = (a, b) => whyNotEqual(a, b) === '';

Assertion.addMethod('expectEqual', function(expected, msg) {
  if (msg) {
    util.flag(this, 'message', msg);
  }
  util.expectTypes(this, ['object']);
  const actual = util.flag(this, 'object');
  this.assert(
    isEqual(actual, expected)
    , 'expected #{this} to expectEqual #{exp}\n' + whyNotStrictlyEqual(actual, expected)
    , 'expected #{this} to not expectEqual #{exp}'
    , expected
    , this._obj
    , util.flag(this, 'negate') ? true : false
  );
});

this is not 100% fully tested so its kinda a draft. though it worked for my use cases.

Thank you @JuHwon :) I'll let you know if I perform changes on it to improve it

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danthegoodman picture danthegoodman  路  3Comments

endymion00 picture endymion00  路  3Comments

ghost picture ghost  路  4Comments

andipavllo picture andipavllo  路  3Comments

corybill picture corybill  路  4Comments