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

Support arrays in t.like() assertion #3185

Merged
merged 8 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/like-selector.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const isObject = selector => Reflect.getPrototypeOf(selector) === Object.prototype;

export function isLikeSelector(selector) {
return selector !== null
&& typeof selector === 'object'
&& Reflect.getPrototypeOf(selector) === Object.prototype
&& Reflect.ownKeys(selector).length > 0;
if (selector === null || typeof selector !== 'object') {
return false;
}

const keyCount = Reflect.ownKeys(selector).length;
return (Array.isArray(selector) && keyCount > 1) || (isObject(selector) && keyCount > 0);
}

export const CIRCULAR_SELECTOR = new Error('Encountered a circular selector');
Expand All @@ -18,7 +22,7 @@ export function selectComparable(lhs, selector, circular = new Set()) {
return lhs;
}

const comparable = {};
const comparable = Array.isArray(selector) ? [] : {};
for (const [key, rhs] of Object.entries(selector)) {
if (isLikeSelector(rhs)) {
comparable[key] = selectComparable(Reflect.get(lhs, key), rhs, circular);
Expand Down
6 changes: 6 additions & 0 deletions test-tap/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,12 @@ test('.like()', t => {
values: [{label: 'Difference (- actual, + expected):', formatted: /{\n-\s*a: 'foo',\n\+\s*a: 'bar',\n\s*}/}],
});

passes(t, () => assertions.like({a: [{a: 1, b: 2}]}, {a: [{a: 1}]}));

passes(t, () => assertions.like([1, 2, 3], [1, 2, 3]));

fails(t, () => assertions.like([1, 2, 3], [3, 2, 1]));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the desired behavior, or should t.like() only support arrays as properties on an object?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now you've opened a can of worms!

If I understand / remember correctly, the second argument recursively selects the entries of the first argument which are then compared against the second. So for objects {a: 1, b: 2} and {a: 3} we compare {a: 1} with {a: 3}. Swap the arguments and we compare {a: 3, b: undefined} with {a: 1, b: 2}.

For arrays then we can only compare the indexes for which the selector has a value: [1, 2] and [3] compares [1] with [3]. Swap the arguments and we compare [3, undefined] with [1, 2].

I think that's all consistent enough and so it'd be fine to support arrays as top-level arguments. What do you think?


t.end();
});

Expand Down