Skip to content

Commit

Permalink
fix(toHaveProperty): add a fix for .toHaveProperty('')
Browse files Browse the repository at this point in the history
  • Loading branch information
chentsulin committed Jan 23, 2022
1 parent 12fee45 commit 7c7012a
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/expect/src/utils.ts
Expand Up @@ -45,7 +45,11 @@ export const getPath = (
propertyPath: string | Array<string>,
): GetPath => {
if (!Array.isArray(propertyPath)) {
console.log('before', {propertyPath});

propertyPath = pathAsArray(propertyPath);

console.log('after', {propertyPath});
}

if (propertyPath.length) {
Expand Down Expand Up @@ -373,9 +377,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 7c7012a

Please sign in to comment.