mirror of
https://github.com/chaijs/chai
synced 2024-11-15 00:07:11 +00:00
Return new assertion instead of this for overwriteMethod
This commit is contained in:
parent
4101fdfbfd
commit
4501970540
2 changed files with 46 additions and 2 deletions
|
@ -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;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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 () {
|
||||
|
|
Loading…
Reference in a new issue