Skip to content

Commit

Permalink
feat(expect): toContainEqual (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aslemammad committed Dec 10, 2021
1 parent 0bef061 commit e6f384f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ declare global {
toMatch(expected: string | RegExp): void
toMatchObject(expected: any): void
toContain(item: any): void
toContainEqual(item: any): void
toBeTruthy(): void
toBeFalsy(): void
toBeNaN(): void
Expand Down
22 changes: 21 additions & 1 deletion src/integrations/chai/jest-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,30 @@ export function JestChaiExpect(): ChaiPlugin {
else
return this.match(expected)
})

utils.addMethod(proto, 'toContain', function(this: Chai.Assertion, item: any) {
return this.contain(item)
})

utils.addMethod(proto, 'toContainEqual', function(this: Chai.AssertionStatic, expected: any) {
const obj = utils.flag(this, 'object')
const index = Array.from(obj).findIndex((item) => {
try {
chai.assert.deepEqual(item, expected)
}
catch {
return false
}
return true
})

this.assert(
index !== -1,
'expected #{this} to deep equally contain #{exp}',
'expected #{this} to not deep equally contain #{exp}',
expected,
)
},
)
utils.addMethod(proto, 'toBeTruthy', function(this: Chai.AssertionStatic) {
const obj = utils.flag(this, 'object')
this.assert(
Expand Down
4 changes: 4 additions & 0 deletions test/core/test/jest-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ describe('jest-expect', () => {
expect(0).toBeFalsy()
expect('Hello').toMatch(/llo/)
expect('Hello').toMatch('llo')
expect('Hello').toContain('llo')
expect(['Hello']).toContain('Hello')
expect([{ text: 'Hello' }]).toContainEqual({ text: 'Hello' })
expect([{ text: 'Bye' }]).not.toContainEqual({ text: 'Hello' })
})

it('object', () => {
Expand Down

0 comments on commit e6f384f

Please sign in to comment.