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 3 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
16 changes: 11 additions & 5 deletions lib/like-selector.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
export function isLikeSelector(selector) {
return selector !== null
&& typeof selector === 'object'
&& Reflect.getPrototypeOf(selector) === Object.prototype
&& Reflect.ownKeys(selector).length > 0;
const isArrayOrObject = selector !== null && typeof selector === 'object';

if (!isArrayOrObject) {
return false;
}

const isArray = Array.isArray(selector);
const minimumKeysRequired = isArray ? 1 : 0;

return Reflect.ownKeys(selector).length > minimumKeysRequired;
tommy-mitchell marked this conversation as resolved.
Show resolved Hide resolved
}

export const CIRCULAR_SELECTOR = new Error('Encountered a circular selector');
Expand All @@ -18,7 +24,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
2 changes: 2 additions & 0 deletions test-tap/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,8 @@ 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}]}));

t.end();
});

Expand Down