diff --git a/lib/chai/utils/overwriteProperty.js b/lib/chai/utils/overwriteProperty.js index fa5452f0f..4abc67576 100644 --- a/lib/chai/utils/overwriteProperty.js +++ b/lib/chai/utils/overwriteProperty.js @@ -4,7 +4,9 @@ * MIT Licensed */ +var chai = require('../../chai'); var flag = require('./flag'); +var transferFlags = require('./transferFlags'); /** * ### overwriteProperty (ctx, name, fn) @@ -58,7 +60,12 @@ 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 }); diff --git a/test/utilities.js b/test/utilities.js index 7201eef6f..44896b800 100644 --- a/test/utilities.js +++ b/test/utilities.js @@ -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 () {