Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancing the toHaveProperty matcher to support array selection #12092

Merged
merged 8 commits into from Nov 29, 2021
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

- `[jest-core]` Add support for `testResultsProcessor` written in ESM ([#12006](https://github.com/facebook/jest/pull/12006))
- `[jest-diff, pretty-format]` Add `compareKeys` option for custom sorting of object keys ([#11992](https://github.com/facebook/jest/pull/11992))
- `[expect]` Enhancing the `toHaveProperty` matcher to support array selection ([#12090](https://github.com/facebook/jest/pull/12090))

### Fixes

Expand Down
24 changes: 24 additions & 0 deletions packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap
Expand Up @@ -3551,6 +3551,30 @@ Expected path: <g>"memo"</>
Expected value: not <g>[]</>
`;

exports[`.toHaveProperty() {pass: true} expect({"a": {"b": [[{"c": [{"d": 1}]}]]}}).toHaveProperty('a.b[0][0].c[0].d', 1) 1`] = `
<d>expect(</><r>received</><d>).</>not<d>.</>toHaveProperty<d>(</><g>path</><d>, </><g>value</><d>)</>

Expected path: <g>"a.b[0][0].c[0].d"</>

Expected value: not <g>1</>
`;

exports[`.toHaveProperty() {pass: true} expect({"a": {"b": [{"c": [{"d": 1}]}]}}).toHaveProperty('a.b[0].c[0].d', 1) 1`] = `
<d>expect(</><r>received</><d>).</>not<d>.</>toHaveProperty<d>(</><g>path</><d>, </><g>value</><d>)</>

Expected path: <g>"a.b[0].c[0].d"</>

Expected value: not <g>1</>
`;

exports[`.toHaveProperty() {pass: true} expect({"a": {"b": [{"c": {"d": [{"e": 1}, {"f": 2}]}}]}}).toHaveProperty('a.b[0].c.d[1].f', 2) 1`] = `
<d>expect(</><r>received</><d>).</>not<d>.</>toHaveProperty<d>(</><g>path</><d>, </><g>value</><d>)</>

Expected path: <g>"a.b[0].c.d[1].f"</>

Expected value: not <g>2</>
`;

exports[`.toHaveProperty() {pass: true} expect({"a": {"b": [1, 2, 3]}}).toHaveProperty('a,b,1') 1`] = `
<d>expect(</><r>received</><d>).</>not<d>.</>toHaveProperty<d>(</><g>path</><d>)</>

Expand Down
3 changes: 3 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Expand Up @@ -1885,6 +1885,9 @@ describe('.toHaveProperty()', () => {
[{a: {b: undefined}}, 'a.b', undefined],
[{a: {}}, 'a.b', undefined], // delete for breaking change in future major
[{a: {b: {c: 5}}}, 'a.b', {c: 5}],
[{a: {b: [{c: [{d: 1}]}]}}, 'a.b[0].c[0].d', 1],
[{a: {b: [{c: {d: [{e: 1}, {f: 2}]}}]}}, 'a.b[0].c.d[1].f', 2],
[{a: {b: [[{c: [{d: 1}]}]]}}, 'a.b[0][0].c[0].d', 1],
[Object.assign(Object.create(null), {property: 1}), 'property', 1],
[new Foo(), 'a', undefined],
[new Foo(), 'b', 'b'],
Expand Down
3 changes: 2 additions & 1 deletion packages/expect/src/matchers.ts
Expand Up @@ -44,6 +44,7 @@ import {
getObjectSubset,
getPath,
iterableEquality,
pathAsArray,
sparseArrayEquality,
subsetEquality,
typeEquality,
Expand Down Expand Up @@ -704,7 +705,7 @@ const matchers: MatchersObject = {

const expectedPathLength =
typeof expectedPath === 'string'
? expectedPath.split('.').length
? pathAsArray(expectedPath).length
: expectedPath.length;

if (expectedPathType === 'array' && expectedPathLength === 0) {
Expand Down
20 changes: 19 additions & 1 deletion packages/expect/src/utils.ts
Expand Up @@ -45,7 +45,7 @@ export const getPath = (
propertyPath: string | Array<string>,
): GetPath => {
if (!Array.isArray(propertyPath)) {
propertyPath = (propertyPath as string).split('.');
propertyPath = pathAsArray(propertyPath);
}

if (propertyPath.length) {
Expand Down Expand Up @@ -372,6 +372,24 @@ export const partition = <T>(
return result;
};

export const pathAsArray = (propertyPath: string): Array<any> => {
// 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 the first dot, if present.
if (propertyPath[0] === '.') {
iifawzi marked this conversation as resolved.
Show resolved Hide resolved
properties.push('');
}

propertyPath.replace(pattern, match => {
properties.push(match);
return match;
});

return properties;
};

// Copied from https://github.com/graingert/angular.js/blob/a43574052e9775cbc1d7dd8a086752c979b0f020/src/Angular.js#L685-L693
export const isError = (value: unknown): value is Error => {
switch (Object.prototype.toString.call(value)) {
Expand Down