From eff9c91b92585ab3bc72ba3b5a6e01bde515017e Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Wed, 15 Nov 2023 16:09:43 +0100 Subject: [PATCH] chore: make any array indexable in toContain --- packages/expect/src/jest-expect.ts | 6 ++++-- test/core/test/environments/jsdom.spec.ts | 11 +++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/expect/src/jest-expect.ts b/packages/expect/src/jest-expect.ts index a55e99832987..d4f9738e7182 100644 --- a/packages/expect/src/jest-expect.ts +++ b/packages/expect/src/jest-expect.ts @@ -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 | string | Node | DOMTokenList if (typeof Node !== 'undefined' && actual instanceof Node) { if (!(item instanceof Node)) @@ -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)) return this.contain(item) }) def('toContainEqual', function (expected) { diff --git a/test/core/test/environments/jsdom.spec.ts b/test/core/test/environments/jsdom.spec.ts index c808a7fc3544..ff0be317a71c 100644 --- a/test/core/test/environments/jsdom.spec.ts +++ b/test/core/test/environments/jsdom.spec.ts @@ -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)