From 4501970540de8406f3ccc3a395e2a2785c64fa97 Mon Sep 17 00:00:00 2001 From: Lucas Vieira Date: Wed, 14 Sep 2016 21:41:29 -0300 Subject: [PATCH] Return new assertion instead of this for overwriteMethod --- lib/chai/utils/overwriteMethod.js | 10 +++++++- test/utilities.js | 38 ++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lib/chai/utils/overwriteMethod.js b/lib/chai/utils/overwriteMethod.js index 4c61c18..9e2c112 100644 --- a/lib/chai/utils/overwriteMethod.js +++ b/lib/chai/utils/overwriteMethod.js @@ -4,7 +4,9 @@ * MIT Licensed */ +var chai = require('../../chai'); var flag = require('./flag'); +var transferFlags = require('./transferFlags'); /** * ### overwriteMethod (ctx, name, fn) @@ -59,6 +61,12 @@ module.exports = function (ctx, name, method) { var result = method(_super).apply(this, arguments); 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; } }; diff --git a/test/utilities.js b/test/utilities.js index cd2029e..46232ae 100644 --- a/test/utilities.js +++ b/test/utilities.js @@ -341,16 +341,23 @@ describe('utilities', function () { }); describe('overwriteMethod', function () { + var assertionConstructor; + before(function() { chai.config.includeStack = false; - chai.use(function(_chai, _) { + chai.use(function(_chai, utils) { + assertionConstructor = _chai.Assertion; + _chai.Assertion.addMethod('four', function() { this.assert(this._obj === 4, 'expected #{this} to be 4', 'expected #{this} to not be 4', 4); }); _chai.Assertion.overwriteMethod('four', function(_super) { return function() { + utils.flag(this, 'mySpecificFlag', 'value1'); + utils.flag(this, 'ultraSpecificFlag', 'value2'); + if (typeof this._obj === 'string') { this.assert(this._obj === 'four', 'expected #{this} to be \'four\'', 'expected #{this} to not be \'four\'', 'four'); } else { @@ -358,6 +365,15 @@ describe('utilities', function () { } } }); + + _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" + ); + }); }); }); @@ -392,6 +408,26 @@ describe('utilities', function () { } } }); + + it('should return a new assertion with flags copied over', function () { + var assertion1 = expect('four'); + var assertion2 = assertion1.four(); + + // Checking if a new assertion was returned + expect(assertion1).to.not.be.equal(assertion2); + + // Check if flags were copied + assertion2.checkFlags(); + + // Checking if it's really an instance of an Assertion + expect(assertion2).to.be.instanceOf(assertionConstructor); + + // Test chaining `.length` after a method to guarantee it is not a function's `length` + expect('four').to.be.a.four().length.above(2); + + // Ensure that foo returns an Assertion (not a function) + expect(expect('four').four()).to.be.an.instanceOf(assertionConstructor); + }); }); it('addProperty', function () {