Skip to content

Commit 0075d89

Browse files
authored
Merge pull request #47 from mrboj/override-include
Override include on a par with equal
2 parents 68e63cd + 9c97063 commit 0075d89

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

chai-exclude.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ function chaiExclude (chai, utils) {
101101
}
102102

103103
/**
104-
* Override standard assertEqual method to remove the keys from other part of the equation.
104+
* Override standard assertion logic method to remove the keys from other part of the equation.
105105
*
106106
* @param {Object} _super
107107
* @returns {Function}
108108
*/
109-
function assertEqual (_super) {
109+
function removeKeysAndAssert (_super) {
110110
return function (val) {
111111
const props = utils.flag(this, 'excludingProps')
112112

@@ -123,6 +123,18 @@ function chaiExclude (chai, utils) {
123123
}
124124
}
125125

126+
/**
127+
* Keep standard chaining logic.
128+
*
129+
* @param {Object} _super
130+
* @returns {Function}
131+
*/
132+
function keepChainingBehavior (_super) {
133+
return function () {
134+
_super.apply(this, arguments)
135+
}
136+
}
137+
126138
/**
127139
* Add a new method 'deepEqualExcluding' to 'chai.assert'.
128140
*/
@@ -191,11 +203,16 @@ function chaiExclude (chai, utils) {
191203
utils.flag(this, 'excludingProps', props)
192204
})
193205

194-
Assertion.overwriteMethod('eq', assertEqual)
195-
Assertion.overwriteMethod('eql', assertEqual)
196-
Assertion.overwriteMethod('eqls', assertEqual)
197-
Assertion.overwriteMethod('equal', assertEqual)
198-
Assertion.overwriteMethod('equals', assertEqual)
206+
Assertion.overwriteMethod('eq', removeKeysAndAssert)
207+
Assertion.overwriteMethod('eql', removeKeysAndAssert)
208+
Assertion.overwriteMethod('eqls', removeKeysAndAssert)
209+
Assertion.overwriteMethod('equal', removeKeysAndAssert)
210+
Assertion.overwriteMethod('equals', removeKeysAndAssert)
211+
212+
Assertion.overwriteChainableMethod('include', removeKeysAndAssert, keepChainingBehavior)
213+
Assertion.overwriteChainableMethod('contain', removeKeysAndAssert, keepChainingBehavior)
214+
Assertion.overwriteChainableMethod('contains', removeKeysAndAssert, keepChainingBehavior)
215+
Assertion.overwriteChainableMethod('includes', removeKeysAndAssert, keepChainingBehavior)
199216
}
200217

201218
module.exports = chaiExclude

chai-exclude.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,11 @@ describe('chai-exclude', () => {
236236

237237
it('should also exclude a key from the other object', () => {
238238
expect({ a: 'a', b: 'b', c: 'c' }).excluding('a').to.deep.equal({ a: 'z', b: 'b', c: 'c' })
239+
expect({ a: 'a', b: 'b', c: 'c' }).excluding('a').to.deep.include({ a: 'z', b: 'b', c: 'c' })
240+
})
241+
242+
it('should also exclude a key from the other object and contain alias is used', () => {
243+
expect({ a: 'a', b: 'b', c: 'c' }).excluding('a').to.deep.contain({ a: 'z', b: 'b', c: 'c' })
239244
})
240245

241246
it('should exclude an array of keys from the object', () => {
@@ -327,6 +332,7 @@ describe('chai-exclude', () => {
327332
expectedObj.e = expectedObj
328333

329334
expect(initialObj).excluding('a').to.deep.equal(expectedObj)
335+
expect(initialObj).excluding('a').to.deep.include(expectedObj)
330336
})
331337
})
332338

@@ -368,6 +374,7 @@ describe('chai-exclude', () => {
368374
}
369375

370376
expect(initialObj).excludingEvery('a').to.deep.equal(expectedObj)
377+
expect(initialObj).excludingEvery('a').to.deep.include(expectedObj)
371378
})
372379

373380
it('should exclude a key from multiple levels of a given object when value is not an object', () => {

0 commit comments

Comments
 (0)