Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
0d3d498240 | |||
30604dc77b | |||
84fd23d6a4 | |||
e7941e7b99 | |||
ef5770e41a | |||
f08eea1f10 |
26
changelog.md
26
changelog.md
@ -1,5 +1,31 @@
|
||||
# Changelog
|
||||
|
||||
## 2025-04-30 - 2.3.3 - fix(tests)
|
||||
Fix test file naming inconsistencies
|
||||
|
||||
- Rename 'test/test.diffOutput.ts' to 'test/test.diffoutput.ts' to standardize filename casing
|
||||
- Rename 'test/test.propertyPath.ts' to 'test/test.propertypath.ts' for consistent file naming
|
||||
|
||||
## 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
|
||||
|
||||
- Added detailed 'Why SmartExpect' section outlining zero-config async support, modular footprint, enhanced messaging, rich built-in matchers, plugin extensibility, and TypeScript support
|
||||
- Clarified installation instructions regarding dependency setup
|
||||
- Updated changelog and commitinfo version to 2.3.1
|
||||
|
||||
## 2025-04-30 - 2.3.1 - fix(readme)
|
||||
Improve README documentation with detailed 'Why SmartExpect' benefits section
|
||||
|
||||
- Added a detailed section outlining zero-config async support, modular footprint, enhanced messaging, rich built-in matchers, plugin extensibility, and TypeScript support
|
||||
- Clarified installation instructions regarding dependency setup
|
||||
|
||||
## 2025-04-30 - 2.3.0 - feat(object-matchers)
|
||||
Add object key matchers: toHaveKeys and toHaveOwnKeys; remove obsolete roadmap plan file
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@push.rocks/smartexpect",
|
||||
"version": "2.3.0",
|
||||
"version": "2.3.3",
|
||||
"private": false,
|
||||
"description": "A testing library to manage expectations in code, offering both synchronous and asynchronous assertion methods.",
|
||||
"main": "dist_ts/index.js",
|
||||
|
12
readme.md
12
readme.md
@ -8,9 +8,19 @@ To install `@push.rocks/smartexpect`, use the following command in your terminal
|
||||
```bash
|
||||
npm install @push.rocks/smartexpect --save
|
||||
```
|
||||
|
||||
This will add `@push.rocks/smartexpect` to your project's dependencies. Make sure you're inside your project directory before running this command.
|
||||
|
||||
## Why SmartExpect?
|
||||
|
||||
SmartExpect is designed to be a minimal, promise-first assertion library with clear, descriptive messaging and easy extensibility:
|
||||
|
||||
- **Zero-config asynchronous support**: chain `.resolves` / `.rejects` directly off `expect()`, add timeouts with `.withTimeout(ms)`, and get clear errors if you mix sync matchers with non-Promises.
|
||||
- **Lean, modular footprint**: only depends on `fast-deep-equal`, `@push.rocks/smartpromise`, and `@push.rocks/smartdelay`; pure ESM, tree-shakable, works in Node & browser.
|
||||
- **Better out-of-the-box messaging**: automatic `.not` inversion (e.g. “Expected 5 not to be greater than 3”) and unified “Expected… / Received…” JSON diffs for object/array mismatches.
|
||||
- **Rich built-in matchers**: numbers (`toBeNaN`, `toBeWithinRange`), objects (`toHaveKeys`, `toHaveOwnKeys`, shorthand `toHaveOwnProperty`), strings/arrays (`toBeEmpty`, `toInclude`, `toHaveLength`), functions (`toThrowErrorMatching`, `toThrowErrorWithMessage`), dates, and more.
|
||||
- **Plugin-style extensibility**: add custom matchers with `expect.extend({ myMatcher })` without monkey-patching.
|
||||
- **First-class TypeScript support**: full `.d.ts` declarations, generic types for sync vs async chains, and autocomplete in editors.
|
||||
|
||||
## Usage
|
||||
|
||||
`@push.rocks/smartexpect` is a TypeScript library designed to manage expectations in your code effectively, improving testing readability and maintainability. Below are various scenarios showcasing how to use this library effectively across both synchronous and asynchronous code paths.
|
||||
|
14
test/test.propertypath.ts
Normal file
14
test/test.propertypath.ts
Normal file
@ -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();
|
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartexpect',
|
||||
version: '2.3.0',
|
||||
version: '2.3.3',
|
||||
description: 'A testing library to manage expectations in code, offering both synchronous and asynchronous assertion methods.'
|
||||
}
|
||||
|
@ -62,11 +62,16 @@ export class ObjectMatchers<T extends object, M extends TExecutionType> {
|
||||
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;
|
||||
},
|
||||
|
Reference in New Issue
Block a user