feat(object-matchers): Add object key matchers: toHaveKeys and toHaveOwnKeys; remove obsolete roadmap plan file

This commit is contained in:
2025-04-30 11:39:39 +00:00
parent 6ea5d643db
commit d1969ab658
5 changed files with 86 additions and 56 deletions

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartexpect',
version: '2.2.2',
version: '2.3.0',
description: 'A testing library to manage expectations in code, offering both synchronous and asynchronous assertion methods.'
}

View File

@ -130,4 +130,30 @@ export class ObjectMatchers<T extends object, M extends TExecutionType> {
`\nReceived: ${JSON.stringify(v, null, 2)}`
);
}
/**
* Assert object has the given keys (including inherited properties).
* @param keys Array of keys to check for presence.
*/
public toHaveKeys(keys: string[]) {
return this.assertion.customAssertion(
(v) => keys.every((key) => key in (v as any)),
(v) =>
`Expected object to have keys ${JSON.stringify(keys)}` +
`\nReceived keys: ${JSON.stringify(Object.keys(v), null, 2)}`
);
}
/**
* Assert object has the given own keys (excluding inherited properties).
* @param keys Array of own keys to check.
*/
public toHaveOwnKeys(keys: string[]) {
return this.assertion.customAssertion(
(v) => keys.every((key) => Object.prototype.hasOwnProperty.call(v as any, key)),
(v) =>
`Expected object to have own keys ${JSON.stringify(keys)}` +
`\nReceived own keys: ${JSON.stringify(Object.keys(v), null, 2)}`
);
}
}