Skip to content

Commit

Permalink
Merge pull request #47 from mrboj/override-include
Browse files Browse the repository at this point in the history
Override include on a par with equal
  • Loading branch information
mesaugat authored Jun 8, 2024
2 parents 68e63cd + 9c97063 commit 0075d89
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
31 changes: 24 additions & 7 deletions chai-exclude.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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'.
*/
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions chai-exclude.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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)
})
})

Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit 0075d89

Please sign in to comment.