Skip to content

Commit

Permalink
chore: make any array indexable in toContain
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Nov 15, 2023
1 parent 255ad01 commit eff9c91
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/expect/src/jest-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
return this.match(expected)
})
def('toContain', function (item) {
const actual = this._obj
const actual = this._obj as Iterable<unknown> | string | Node | DOMTokenList

if (typeof Node !== 'undefined' && actual instanceof Node) {
if (!(item instanceof Node))
Expand All @@ -191,7 +191,9 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
actual.value,
)
}

// make "actual" indexable to have compatibility with jest
if (actual != null && typeof actual !== 'string')
utils.flag(this, 'object', Array.from(actual as Iterable<unknown>))
return this.contain(item)
})
def('toContainEqual', function (expected) {
Expand Down
11 changes: 11 additions & 0 deletions test/core/test/environments/jsdom.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ test('toContain correctly handles DOM nodes', () => {
const external = document.createElement('div')
wrapper.appendChild(child)

const parent = document.createElement('div')
parent.appendChild(wrapper)
parent.appendChild(external)

document.body.appendChild(parent)
const divs = document.querySelectorAll('div')

expect(divs).toContain(wrapper)
expect(divs).toContain(parent)
expect(divs).toContain(external)

expect(wrapper).toContain(child)
expect(wrapper).not.toContain(external)

Expand Down

0 comments on commit eff9c91

Please sign in to comment.