Skip to content

Commit

Permalink
Override include method - update overriding logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mrboj committed Oct 30, 2023
1 parent 96c1aaa commit 322e0d3
Showing 1 changed file with 24 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.addChainableMethod('include', removeKeysAndAssert, keepChainingBehavior)
Assertion.addChainableMethod('contain', removeKeysAndAssert, keepChainingBehavior)
Assertion.addChainableMethod('contains', removeKeysAndAssert, keepChainingBehavior)
Assertion.addChainableMethod('includes', removeKeysAndAssert, keepChainingBehavior)
}

module.exports = chaiExclude
Expand Down

0 comments on commit 322e0d3

Please sign in to comment.