Skip to content

Commit

Permalink
Merge pull request #799 from vieiralucas/consistency-return-new-asser…
Browse files Browse the repository at this point in the history
…tion

Return new assertion with flags copied over instead of this. Fix #791
  • Loading branch information
lucasfcosta committed Sep 19, 2016
2 parents dfbcf8f + 5d11228 commit 7683356
Show file tree
Hide file tree
Showing 5 changed files with 493 additions and 229 deletions.
12 changes: 10 additions & 2 deletions lib/chai/utils/addChainableMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
* Module dependencies
*/

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

/*!
* Module variables
Expand Down Expand Up @@ -82,7 +83,14 @@ module.exports = function (ctx, name, method, chainingBehavior) {
if (old_ssfi)
flag(this, 'ssfi', assert);
var result = chainableBehavior.method.apply(this, arguments);
return result === undefined ? this : result;

if (result !== undefined) {
return result;
}

var newAssertion = new chai.Assertion();
transferFlags(this, newAssertion);
return newAssertion;
};

// Use `__proto__` if available
Expand Down
19 changes: 17 additions & 2 deletions lib/chai/utils/overwriteChainableMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
* MIT Licensed
*/

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

/**
* ### overwriteChainableMethod (ctx, name, method, chainingBehavior)
*
Expand Down Expand Up @@ -43,12 +46,24 @@ module.exports = function (ctx, name, method, chainingBehavior) {
var _chainingBehavior = chainableBehavior.chainingBehavior;
chainableBehavior.chainingBehavior = function () {
var result = chainingBehavior(_chainingBehavior).call(this);
return result === undefined ? this : result;
if (result !== undefined) {
return result;
}

var newAssertion = new chai.Assertion();
transferFlags(this, newAssertion);
return newAssertion;
};

var _method = chainableBehavior.method;
chainableBehavior.method = function () {
var result = method(_method).apply(this, arguments);
return result === undefined ? this : result;
if (result !== undefined) {
return result;
}

var newAssertion = new chai.Assertion();
transferFlags(this, newAssertion);
return newAssertion;
};
};
10 changes: 9 additions & 1 deletion lib/chai/utils/overwriteMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
* MIT Licensed
*/

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

/**
* ### overwriteMethod (ctx, name, fn)
Expand Down Expand Up @@ -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;
}
};
10 changes: 9 additions & 1 deletion lib/chai/utils/overwriteProperty.js
Original file line number Diff line number Diff line change
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

0 comments on commit 7683356

Please sign in to comment.