Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return new assertion with flags copied over instead of this. Fix #791 #799

Merged
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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this create a circular dependency? Have we done this before? I feel like this might create issues

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently addMethod and addProperty have this too and there's been no issue reported about it. However, I'd like to have more time to check this before giving a confident response.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused how it's working.

  1. test/bootstrap/index.js requires index.js
  2. index.js requires lib/chai.js
  3. lib/chai.js requires lib/chai/utils/index.js
  4. lib/chai/utils/index.js requires lib/chai/utils/addProperty.js
  5. lib/chai/utils/addProperty.js requires lib/chai.js

Thus circular dependency. -scratches head-

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it works because chai.js only sets module.exports and this file only calls stuff from chai.js inside of the methods, so it all kind of works out eventually.

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's put braces around this.

}

var newAssertion = new chai.Assertion();
transferFlags(this, newAssertion);
return newAssertion;
}
, configurable: true
});
Expand Down