Skip to content

Commit

Permalink
fix: support .toHaveProperty('') (#12251)
Browse files Browse the repository at this point in the history
  • Loading branch information
chentsulin committed Jan 28, 2022
1 parent 056e5ef commit d1bc333
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

### Fixes

- `[expect]` Add a fix for `.toHaveProperty('')` ([#12251](https://github.com/facebook/jest/pull/12251))
- `[jest-environment-node]` Add `atob` and `btoa` ([#12269](https://github.com/facebook/jest/pull/12269))

### Chore & Maintenance
Expand Down
1 change: 1 addition & 0 deletions packages/expect/src/__tests__/matchers.test.js
Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion packages/expect/src/utils.ts
Expand Up @@ -373,9 +373,15 @@ export const partition = <T>(
};

export const pathAsArray = (propertyPath: string): Array<any> => {
const properties: Array<string> = [];

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<string> = [];

// Because the regex won't match a dot in the beginning of the path, if present.
if (propertyPath[0] === '.') {
Expand Down

0 comments on commit d1bc333

Please sign in to comment.