diff --git a/chai-exclude.js b/chai-exclude.js index 992b411..8bcc1c6 100644 --- a/chai-exclude.js +++ b/chai-exclude.js @@ -101,12 +101,12 @@ function chaiExclude (chai, utils) { } /** - * Override standard assertEqual method to remove the keys from other part of the equation. + * Override standard assertion logic method to remove the keys from other part of the equation. * * @param {Object} _super * @returns {Function} */ - function assertEqual (_super) { + function removeKeysAndAssert (_super) { return function (val) { const props = utils.flag(this, 'excludingProps') @@ -123,6 +123,18 @@ function chaiExclude (chai, utils) { } } + /** + * Keep standard chaining logic. + * + * @param {Object} _super + * @returns {Function} + */ + function keepChainingBehavior (_super) { + return function () { + _super.apply(this, arguments) + } + } + /** * Add a new method 'deepEqualExcluding' to 'chai.assert'. */ @@ -191,11 +203,16 @@ function chaiExclude (chai, utils) { utils.flag(this, 'excludingProps', props) }) - Assertion.overwriteMethod('eq', assertEqual) - Assertion.overwriteMethod('eql', assertEqual) - Assertion.overwriteMethod('eqls', assertEqual) - Assertion.overwriteMethod('equal', assertEqual) - Assertion.overwriteMethod('equals', assertEqual) + Assertion.overwriteMethod('eq', removeKeysAndAssert) + Assertion.overwriteMethod('eql', removeKeysAndAssert) + Assertion.overwriteMethod('eqls', removeKeysAndAssert) + Assertion.overwriteMethod('equal', removeKeysAndAssert) + Assertion.overwriteMethod('equals', removeKeysAndAssert) + + Assertion.overwriteChainableMethod('include', removeKeysAndAssert, keepChainingBehavior) + Assertion.overwriteChainableMethod('contain', removeKeysAndAssert, keepChainingBehavior) + Assertion.overwriteChainableMethod('contains', removeKeysAndAssert, keepChainingBehavior) + Assertion.overwriteChainableMethod('includes', removeKeysAndAssert, keepChainingBehavior) } module.exports = chaiExclude diff --git a/chai-exclude.test.js b/chai-exclude.test.js index 50196ae..a21d608 100644 --- a/chai-exclude.test.js +++ b/chai-exclude.test.js @@ -236,6 +236,11 @@ describe('chai-exclude', () => { it('should also exclude a key from the other object', () => { expect({ a: 'a', b: 'b', c: 'c' }).excluding('a').to.deep.equal({ a: 'z', b: 'b', c: 'c' }) + expect({ a: 'a', b: 'b', c: 'c' }).excluding('a').to.deep.include({ a: 'z', b: 'b', c: 'c' }) + }) + + it('should also exclude a key from the other object and contain alias is used', () => { + expect({ a: 'a', b: 'b', c: 'c' }).excluding('a').to.deep.contain({ a: 'z', b: 'b', c: 'c' }) }) it('should exclude an array of keys from the object', () => { @@ -327,6 +332,7 @@ describe('chai-exclude', () => { expectedObj.e = expectedObj expect(initialObj).excluding('a').to.deep.equal(expectedObj) + expect(initialObj).excluding('a').to.deep.include(expectedObj) }) }) @@ -368,6 +374,7 @@ describe('chai-exclude', () => { } expect(initialObj).excludingEvery('a').to.deep.equal(expectedObj) + expect(initialObj).excludingEvery('a').to.deep.include(expectedObj) }) it('should exclude a key from multiple levels of a given object when value is not an object', () => {