Skip to content

Commit

Permalink
Return new assertion instead of this for overwriteProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
vieiralucas committed Sep 14, 2016
1 parent c9cb468 commit 21df19c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/chai/utils/overwriteProperty.js
Expand Up @@ -4,7 +4,9 @@
* MIT Licensed
*/

var chai = require('../../chai');
var flag = require('./flag');
var transferFlags = require('./transferFlags');

/**
* ### overwriteProperty (ctx, name, fn)
Expand Down Expand Up @@ -58,7 +60,13 @@ module.exports = function (ctx, name, getter) {
var result = getter(_super).call(this);
flag(this, 'keep_ssfi', false);

return result === undefined ? this : result;
if (result !== undefined) {
return result;
}

var newAssertion = new chai.Assertion();
transferFlags(this, newAssertion);
return newAssertion;
}
, configurable: true
});
Expand Down
47 changes: 47 additions & 0 deletions test/utilities.js
Expand Up @@ -543,6 +543,53 @@ describe('utilities', function () {
}
}
});

it('should return new assertion with flags copied over', function() {
var assertionConstructor;

chai.use(function (_chai, utils) {
assertionConstructor = _chai.Assertion;

_chai.Assertion.overwriteProperty('and', function (_super) {
return function blah () {
utils.flag(this, 'mySpecificFlag', 'value1');
utils.flag(this, 'ultraSpecificFlag', 'value2');
_super.call(this);
};
});

_chai.Assertion.addMethod('checkFlags', function() {
this.assert(
utils.flag(this, 'mySpecificFlag') === 'value1' &&
utils.flag(this, 'ultraSpecificFlag') === 'value2'
, 'expected assertion to have specific flags'
, "this doesn't matter"
);
});
});

var assertion1 = expect('foo');
var assertion2 = assertion1.is.and;

// Checking if a new assertion was returned
expect(assertion1).to.not.be.equal(assertion2);

// Check if flags were copied
assertion2.checkFlags();

// If it is, calling length on it should return an assertion, not a function
expect([1, 2, 3]).to.be.an.and.length.below(1000);

// Checking if it's really an instance of an Assertion
expect(assertion2).to.be.instanceOf(assertionConstructor);

// Test chaining `.length` after a property to guarantee it is not a function's `length`
expect([1, 2, 3]).to.be.a.and.with.length.above(2);
expect([1, 2, 3]).to.be.an.instanceOf(Array).and.have.length.below(4);

expect(expect([1, 2, 3]).be).to.be.an.instanceOf(assertionConstructor);
expect(expect([1, 2, 3]).and).to.be.an.instanceOf(assertionConstructor);
});
});

it('getMessage', function () {
Expand Down

0 comments on commit 21df19c

Please sign in to comment.