Chai: `.not` does not negate negated

Created on 27 Apr 2017  Â·  1Comment  Â·  Source: chaijs/chai

Simply speaking, actual result is

expect(null).to.be.null; // ok
expect(null).not.to.be.not.null; // Error: expected null not to be null

While from logical point of view, I would expect ¬¬p ⇔ p

Above example may look un-practical and obscure, but that's the same reason why it hit's in regular use of

chai.Assertion.addProperty('foo', function(){
    ...
    utils.transferFlags(this, newAssert, false);
    newAssert.not.null;
});
expect(bar).not.to.be.foo;

what makes transferring negation flag quite tricky.

Forgive me if I'm doing something dumb, I'm quite new to creating own assertions.

Most helpful comment

@tomalec One thing you could do is unset an existing negated flag from within your custom assertion:

utils.flag(this, 'negate', false);

Having two not flags cancel each other out is problematic in some situations, such as:

expect(42).to.not.equal(41).and.not.equal(43);

The typical counterargument to this is that maybe .not shouldn't cancel .not but rather some other keyword like .but or .and. This was discussed quite a bit in #785 and ultimately decided that there's problems with every approach (including the current one), and that any benefit gained by changing the behavior isn't great enough to be worth a breaking change.

>All comments

@tomalec One thing you could do is unset an existing negated flag from within your custom assertion:

utils.flag(this, 'negate', false);

Having two not flags cancel each other out is problematic in some situations, such as:

expect(42).to.not.equal(41).and.not.equal(43);

The typical counterargument to this is that maybe .not shouldn't cancel .not but rather some other keyword like .but or .and. This was discussed quite a bit in #785 and ultimately decided that there's problems with every approach (including the current one), and that any benefit gained by changing the behavior isn't great enough to be worth a breaking change.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

corybill picture corybill  Â·  4Comments

zzzgit picture zzzgit  Â·  3Comments

danthegoodman picture danthegoodman  Â·  3Comments

meeber picture meeber  Â·  4Comments

liborbus picture liborbus  Â·  4Comments