From e7941e7b990b14316c998e8ef379728660fcba3a Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Wed, 30 Apr 2025 18:00:29 +0000 Subject: [PATCH] fix(object): Update toHaveProperty matcher to support nested property paths using dot notation --- changelog.md | 7 +++++++ test/test.propertyPath.ts | 14 ++++++++++++++ ts/00_commitinfo_data.ts | 2 +- ts/namespaces/object.ts | 11 ++++++++--- 4 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 test/test.propertyPath.ts diff --git a/changelog.md b/changelog.md index 3e26bc1..ad271c6 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,12 @@ # Changelog +## 2025-04-30 - 2.3.2 - fix(object) +Update toHaveProperty matcher to support nested property paths using dot notation + +- Changed toHaveProperty implementation to split property strings on '.' and traverse nested objects +- Fixed value comparison for nested properties by comparing the final drilled value instead of direct property access +- Added tests for nested property access in test/propertyPath.ts + ## 2025-04-30 - 2.3.1 - fix(readme) Improve README documentation with detailed 'Why SmartExpect' benefits section diff --git a/test/test.propertyPath.ts b/test/test.propertyPath.ts new file mode 100644 index 0000000..a88260f --- /dev/null +++ b/test/test.propertyPath.ts @@ -0,0 +1,14 @@ +import { tap, expect as tExpect } from '@push.rocks/tapbundle'; +import * as smartexpect from '../dist_ts/index.js'; + +tap.test('toHaveProperty nested path via dot notation', async () => { + const testObject = { level1: { level2: { level3: 'value' } } }; + // Existence check + smartexpect.expect(testObject).object.toHaveProperty('level1.level2.level3'); + // Value check + smartexpect.expect(testObject).object.toHaveProperty('level1.level2.level3', 'value'); + // Negation for missing deep property + smartexpect.expect(testObject).not.object.toHaveProperty('level1.level2.missing'); +}); + +export default tap.start(); \ No newline at end of file diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 79f388e..96b5521 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/smartexpect', - version: '2.3.1', + version: '2.3.2', description: 'A testing library to manage expectations in code, offering both synchronous and asynchronous assertion methods.' } diff --git a/ts/namespaces/object.ts b/ts/namespaces/object.ts index 438ac12..45a0995 100644 --- a/ts/namespaces/object.ts +++ b/ts/namespaces/object.ts @@ -62,11 +62,16 @@ export class ObjectMatchers { return this.assertion.customAssertion( (v) => { const obj = v as any; - if (!(property in obj)) { - return false; + const path = property.split('.'); + let current = obj; + for (const key of path) { + if (current == null || !(key in current)) { + return false; + } + current = current[key]; } if (arguments.length === 2) { - return plugins.fastDeepEqual(obj[property], value); + return plugins.fastDeepEqual(current, value); } return true; },