From 12fee45b8b1dd04522d6108bc16d5519ab42d91c Mon Sep 17 00:00:00 2001 From: chentsulin Date: Mon, 17 Jan 2022 14:22:03 +0800 Subject: [PATCH 1/3] chore: add a failed test for .toHaveProperty('') --- packages/expect/src/__tests__/matchers.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index 20b48ca6053a..6868d107d135 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -1898,6 +1898,7 @@ describe('.toHaveProperty()', () => { [new E('div'), 'nodeType', 1], ['', 'length', 0], [memoized, 'memo', []], + [{'': 1}, '', 1], ].forEach(([obj, keyPath, value]) => { test(`{pass: true} expect(${stringify( obj, From 399e47209b338a91d71f21aaf88b33e30e989024 Mon Sep 17 00:00:00 2001 From: chentsulin Date: Sun, 23 Jan 2022 14:15:58 +0800 Subject: [PATCH 2/3] fix(expect): add a fix for .toHaveProperty('') --- packages/expect/src/utils.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/expect/src/utils.ts b/packages/expect/src/utils.ts index 725289ebb3a4..9e546c303c26 100644 --- a/packages/expect/src/utils.ts +++ b/packages/expect/src/utils.ts @@ -373,9 +373,15 @@ export const partition = ( }; export const pathAsArray = (propertyPath: string): Array => { + const properties: Array = []; + + if (propertyPath === '') { + properties.push(''); + return properties; + } + // will match everything that's not a dot or a bracket, and "" for consecutive dots. const pattern = RegExp('[^.[\\]]+|(?=(?:\\.)(?:\\.|$))', 'g'); - const properties: Array = []; // Because the regex won't match a dot in the beginning of the path, if present. if (propertyPath[0] === '.') { From 78beae9aa73c0d6b8ef5199c43addabff1ee6e83 Mon Sep 17 00:00:00 2001 From: chentsulin Date: Sun, 23 Jan 2022 15:58:03 +0800 Subject: [PATCH 3/3] chore: update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2cdded6df47d..a81342083d9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Fixes +- `[expect]` Add a fix for `.toHaveProperty('')` ([#12251](https://github.com/facebook/jest/pull/12251)) + ### Chore & Maintenance - `[*]` Update graceful-fs to ^4.2.9 ([#11749](https://github.com/facebook/jest/pull/11749))