Compare commits

..

34 Commits

Author SHA1 Message Date
dbec1d3e4a 2.4.2
Some checks failed
Default (tags) / security (push) Failing after 18s
Default (tags) / test (push) Failing after 9s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2025-05-01 08:57:21 +00:00
ff9170ab67 fix(cleanup): Remove unused scratch files 2025-05-01 08:57:21 +00:00
b68011b79d 2.4.1
Some checks failed
Default (tags) / security (push) Failing after 18s
Default (tags) / test (push) Failing after 9s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2025-04-30 19:37:20 +00:00
ff795f6fe0 fix(Assertion): Improve toHaveProperty alias by forwarding arguments correctly for intuitive object property assertions 2025-04-30 19:37:19 +00:00
62cf7f5db5 2.4.0
Some checks failed
Default (tags) / security (push) Failing after 18s
Default (tags) / test (push) Failing after 9s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2025-04-30 18:24:28 +00:00
0351da2878 feat(object): add toHaveOwnProperty method and improve property-path matching in object assertions 2025-04-30 18:24:28 +00:00
0d3d498240 2.3.3
Some checks failed
Default (tags) / security (push) Failing after 9s
Default (tags) / test (push) Failing after 8s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2025-04-30 18:02:07 +00:00
30604dc77b fix(tests): Fix test file naming inconsistencies 2025-04-30 18:02:07 +00:00
84fd23d6a4 2.3.2
Some checks failed
Default (tags) / security (push) Failing after 19s
Default (tags) / test (push) Failing after 8s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2025-04-30 18:00:29 +00:00
e7941e7b99 fix(object): Update toHaveProperty matcher to support nested property paths using dot notation 2025-04-30 18:00:29 +00:00
ef5770e41a 2.3.1 2025-04-30 11:52:27 +00:00
f08eea1f10 fix(readme): Improve README documentation 2025-04-30 11:52:27 +00:00
2b803e6d57 2.3.0 2025-04-30 11:39:40 +00:00
d1969ab658 feat(object-matchers): Add object key matchers: toHaveKeys and toHaveOwnKeys; remove obsolete roadmap plan file 2025-04-30 11:39:39 +00:00
6ea5d643db 2.2.2 2025-04-29 12:27:02 +00:00
6f1326a8da fix(license-files): Remove legacy license file and add license.md to update file naming. 2025-04-29 12:27:01 +00:00
f099d0f98d 2.2.1 2025-04-29 12:25:04 +00:00
a3d5892a13 fix(readme): Update usage examples and full matcher reference in README 2025-04-29 12:25:04 +00:00
0d9fa72b29 2.2.0 2025-04-29 12:08:58 +00:00
8cb70b6afe feat(generics): Improve assertion and matcher type definitions by adding execution mode generics for better async/sync support 2025-04-29 12:08:57 +00:00
81bd8bfb13 2.1.2 2025-04-29 11:44:05 +00:00
dd4037677d fix(ts/index.ts): Remove deprecated expectAsync function and advise using .resolves/.rejects on expect for async assertions 2025-04-29 11:44:04 +00:00
d0c2d04595 2.1.1 2025-04-29 11:42:41 +00:00
db49492af6 fix(Assertion): Improve chainability by fixing return types in assertion methods 2025-04-29 11:42:41 +00:00
855e20a217 2.1.0 2025-04-28 20:42:58 +00:00
9b488a87a0 feat(core): Add new matchers and improve negation messaging 2025-04-28 20:42:58 +00:00
1847838ac3 2.0.1 2025-04-28 19:58:32 +00:00
91a3dc43d3 fix(assertion-matchers): Refactor matcher implementations to consistently use customAssertion for improved consistency and clarity. 2025-04-28 19:58:32 +00:00
4eac4544a5 2.0.0 2025-04-28 19:10:27 +00:00
47458118a6 BREAKING CHANGE(docs): Update documentation and examples to unify async and sync assertions, add custom matcher guides, and update package configuration 2025-04-28 19:10:27 +00:00
6f1e37cf56 1.6.1 2025-03-04 12:32:00 +00:00
ed9a9b7f2c fix(build): Corrected package.json and workflow dependencies and resolved formatting issues in tests. 2025-03-04 12:32:00 +00:00
5801d34f18 1.6.0 2025-03-04 12:20:06 +00:00
f0ab180902 feat(assertion): Enhanced the assertion error messaging and added new test cases. 2025-03-04 12:20:06 +00:00
28 changed files with 4770 additions and 2770 deletions

View File

@ -119,6 +119,6 @@ jobs:
run: |
npmci node install stable
npmci npm install
pnpm install -g @gitzone/tsdoc
pnpm install -g @git.zone/tsdoc
npmci command tsdoc
continue-on-error: true

View File

@ -1,5 +1,133 @@
# Changelog
## 2025-05-01 - 2.4.2 - fix(cleanup)
Remove unused scratch files
- Deleted scratch-alias.js, scratch-alias2.js, scratch-alias3.js, scratch-alias4.js, scratch-alias5.js, and scratch.js
- Clean up temporary alias and scratch test files
## 2025-04-30 - 2.4.1 - fix(Assertion)
Improve toHaveProperty alias by forwarding arguments correctly for intuitive object property assertions
- Updated the toHaveProperty method in the Assertion class to check the number of arguments and call the appropriate object matcher
- Added several scratch alias files to demonstrate and test the alias usage
- Enhanced test cases in test/propertypath to cover alias behavior
## 2025-04-30 - 2.4.0 - feat(object)
add toHaveOwnProperty method and improve property-path matching in object assertions
- Added 'toHaveOwnProperty' as a direct method on Assertion to check for own properties
- Enhanced property path evaluation in 'toHaveProperty' to handle nested keys more robustly
- Renamed test file to maintain consistent naming for expect.any tests
- Introduced scratch.js for manual testing and debugging of property matchers
## 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
- Implemented toHaveKeys matcher to check for both own and inherited properties
- Implemented toHaveOwnKeys matcher to check for own properties only
- Added tests for key matchers in test/test.keys.ts
- Removed readme.plan.md as it is now obsolete
## 2025-04-29 - 2.2.2 - fix(license-files)
Remove legacy license file and add license.md to update file naming.
- Removed the old 'license' file.
- Added 'license.md' with updated file structure.
## 2025-04-29 - 2.2.1 - fix(readme)
Update usage examples and full matcher reference in README
- Removed deprecated 'expectAsync' from import example
- Fixed formatting in documentation
- Added comprehensive full matcher reference section
## 2025-04-29 - 2.2.0 - feat(generics)
Improve assertion and matcher type definitions by adding execution mode generics for better async/sync support
- Updated ts/index.ts to import and use TExecutionType in the expect function
- Modified Assertion class to use a generic execution mode (M) for improved type inference
- Revised all matcher namespaces (array, boolean, date, function, number, object, string, type) to accept the new generic parameter
- Enhanced async/sync distinction for assertion methods like resolves and rejects
## 2025-04-29 - 2.1.2 - fix(ts/index.ts)
Remove deprecated expectAsync function and advise using .resolves/.rejects on expect for async assertions
- Deleted the redundant expectAsync export in ts/index.ts
- Users should now call expect(...).resolves or expect(...).rejects for asynchronous assertions
## 2025-04-29 - 2.1.1 - fix(Assertion)
Improve chainability by fixing return types in assertion methods
- Update runCheck method to explicitly return the correct chainable type for both async and sync assertions
- Ensure customAssertion propagates the chainable Assertion instance
- Refactor internal promise handling for clarity and consistency
## 2025-04-28 - 2.1.0 - feat(core)
Add new matchers and improve negation messaging
- Added expect.any() and expect.anything() matchers for enhanced object pattern matching
- Introduced new number matchers: toBeNaN(), toBeFinite(), and toBeWithinRange()
- Implemented alias toBeEmpty() for both string and array matchers
- Enhanced function matchers with toThrowErrorMatching() and toThrowErrorWithMessage()
- Improved negation messaging to provide clearer failure messages (e.g. 'Expected 5 not to be greater than 3')
- Enhanced object assertions with a toHaveOwnProperty() shorthand that outputs unified diff-style messages
## 2025-04-28 - 2.0.1 - fix(assertion-matchers)
Refactor matcher implementations to consistently use customAssertion for improved consistency and clarity.
- Updated ArrayMatchers, BooleanMatchers, DateMatchers, FunctionMatchers, NumberMatchers, ObjectMatchers, StringMatchers, and TypeMatchers to use customAssertion directly.
- Aligned Assertion class aliases to delegate to the namespaced matchers with the new customAssertion pattern.
## 2025-04-28 - 2.0.0 - BREAKING CHANGE(docs)
Update documentation and examples to unify async and sync assertions, add custom matcher guides, and update package configuration
- Added packageManager field in package.json
- Revised documentation in readme.md to use .resolves/.rejects instead of expectAsync
- Included detailed examples for custom matchers and updated API usage
- Added readme.plan.md outlining the future roadmap
- Updated tests to import the built library from dist_ts
## 2025-03-04 - 1.6.1 - fix(build)
Corrected package.json and workflow dependencies and resolved formatting issues in tests.
- Fixed incorrect global npm package reference for tsdoc installation in workflow file.
- Updated dependencies in package.json for consistency in package naming.
- Resolved inconsistent formatting and spacing in test files.
## 2025-03-04 - 1.6.0 - feat(assertion)
Enhanced the assertion error messaging and added new test cases.
- Improved error messages by incorporating path and value/placeholders in assertions.
- Added detailed testing of new assertion functionalities.
- Additional test cases for comprehensive coverage of new features.
## 2025-03-04 - 1.5.0 - feat(Assertion)
Add toBeTypeOf assertion method

View File

@ -1,4 +1,4 @@
Copyright (c) 2022 Lossless GmbH (hello@lossless.com)
Copyright (c) 2022 Task Venture Capital GmbH (hello@lossless.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -1,6 +1,6 @@
{
"name": "@push.rocks/smartexpect",
"version": "1.5.0",
"version": "2.4.2",
"private": false,
"description": "A testing library to manage expectations in code, offering both synchronous and asynchronous assertion methods.",
"main": "dist_ts/index.js",
@ -10,14 +10,14 @@
"license": "MIT",
"scripts": {
"test": "(tstest test/ --web)",
"build": "(tsbuild --web)",
"build": "(tsbuild tsfolders)",
"buildDocs": "tsdoc"
},
"devDependencies": {
"@gitzone/tsbuild": "^2.1.66",
"@gitzone/tsbundle": "^2.0.8",
"@gitzone/tsrun": "^1.2.44",
"@gitzone/tstest": "^1.0.77",
"@git.zone/tsbuild": "^2.2.1",
"@git.zone/tsbundle": "^2.2.5",
"@git.zone/tsrun": "^1.3.3",
"@git.zone/tstest": "^1.0.96",
"@push.rocks/tapbundle": "^5.5.6",
"@types/node": "^22.13.9"
},
@ -62,5 +62,6 @@
"onlyBuiltDependencies": [
"mongodb-memory-server"
]
}
},
"packageManager": "pnpm@10.7.0+sha512.6b865ad4b62a1d9842b61d674a393903b871d9244954f652b8842c2b553c72176b278f64c463e52d40fff8aba385c235c8c9ecf5cc7de4fd78b8bb6d49633ab6"
}

5031
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

346
readme.md
View File

@ -1,5 +1,5 @@
# @push.rocks/smartexpect
manage expectations in code
Manage expectations in code with precise, readable assertions
## Install
@ -8,19 +8,29 @@ 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.
### Getting Started
First, import `@push.rocks/smartexpect` into your TypeScript file:
First, import `@push.rocks/smartexpect` into your TypeScript file:
```typescript
import { expect, expectAsync } from '@push.rocks/smartexpect';
import { expect } from '@push.rocks/smartexpect';
```
### Synchronous Expectations
@ -30,14 +40,16 @@ You can employ `expect` to create synchronous assertions:
```typescript
import { expect } from '@push.rocks/smartexpect';
// String type assertion
// Type assertions
expect('hello').toBeTypeofString();
// Negated String type assertion
expect(1).not.toBeTypeofString();
// Boolean type assertion
expect(42).toBeTypeofNumber();
expect(true).toBeTypeofBoolean();
expect(() => {}).toBeTypeOf('function');
expect({}).toBeTypeOf('object');
// Negated assertions
expect(1).not.toBeTypeofString();
expect('string').not.toBeTypeofNumber();
// Equality assertion
expect('hithere').toEqual('hithere');
@ -51,78 +63,308 @@ expect('hithere').toMatch(/hi/);
### Asynchronous Expectations
For asynchronous operations, use `expectAsync` to return a promise:
For asynchronous code, use the same `expect` function with the `.resolves` or `.rejects` modifier:
```typescript
import { expectAsync } from '@push.rocks/smartexpect';
import { expect } from '@push.rocks/smartexpect';
const asyncStringFetcher = async (): Promise<string> => {
return 'async string';
};
const asyncTest = async () => {
await expectAsync(asyncStringFetcher()).toBeTypeofString();
await expectAsync(asyncStringFetcher()).toEqual('async string');
// Add a timeout to prevent hanging tests
await expect(asyncStringFetcher()).resolves.withTimeout(5000).type.toBeTypeofString();
await expect(asyncStringFetcher()).resolves.toEqual('async string');
};
asyncTest();
```
### Advanced Usage
### Navigating Complex Objects
- **Properties and Deep Properties:** Assert the existence of properties and their values.
```typescript
const testObject = { level1: { level2: 'value' } };
You can navigate complex objects using the `property()` and `arrayItem()` methods:
// Property existence
expect(testObject).toHaveProperty('level1');
// Deep Property existence
expect(testObject).toHaveDeepProperty(['level1', 'level2']);
```
```typescript
const complexObject = {
users: [
{ id: 1, name: 'Alice', permissions: { admin: true } },
{ id: 2, name: 'Bob', permissions: { admin: false } }
]
};
- **Conditions and Comparisons:** Allow more intricate assertions like greater than, less than, or matching specific conditions.
// Navigate to a nested property
expect(complexObject)
.property('users')
.arrayItem(0)
.property('name')
.toEqual('Alice');
```typescript
// Greater Than
expect(5).toBeGreaterThan(3);
// Check nested permission
expect(complexObject)
.property('users')
.arrayItem(0)
.property('permissions')
.property('admin')
.toBeTrue();
```
// Less Than
expect(3).toBeLessThan(5);
### Advanced Assertions
// Custom conditions
expect(7).customAssertion(value => value > 5, 'Value is not greater than 5');
```
#### Properties and Deep Properties
- **Arrays and Objects:** Work seamlessly with arrays and objects, checking for containment, length, or specific values.
Assert the existence of properties and their values:
```typescript
const testArray = [1, 2, 3];
```typescript
const testObject = { level1: { level2: 'value' } };
// Containment
expect(testArray).toContain(2);
// Property existence
expect(testObject).toHaveProperty('level1');
// Array length
expect(testArray).toHaveLength(3);
// Property with specific value
expect(testObject).toHaveProperty('level1.level2', 'value');
// Object matching
expect({ name: 'Test', value: 123 }).toMatchObject({ name: 'Test' });
```
// Deep Property existence
expect(testObject).toHaveDeepProperty(['level1', 'level2']);
```
### Handling Promises and Async Operations
#### Conditions and Comparisons
`@push.rocks/smartexpect` gracefully integrates with asynchronous operations, providing a `expectAsync` function that handles promise-based assertions. This keeps your tests clean and readable, irrespective of the nature of the code being tested.
Perform more intricate assertions:
### Best Practices
```typescript
// Numeric comparisons
expect(5).toBeGreaterThan(3);
expect(3).toBeLessThan(5);
expect(5).toBeGreaterThanOrEqual(5);
expect(5).toBeLessThanOrEqual(5);
expect(0.1 + 0.2).toBeCloseTo(0.3, 10); // Floating point comparison with precision
- **Readability:** Favor clarity and readability by explicitly stating your expectations. `@push.rocks/smartexpect`'s API is designed to be fluent and expressive, making your tests easy to write and, more importantly, easy to read.
// Truthiness checks
expect(true).toBeTrue();
expect(false).toBeFalse();
expect('non-empty').toBeTruthy();
expect(0).toBeFalsy();
- **Comprehensive Coverage:** Utilize the full spectrum of assertions provided to cover a broad set of use cases, ensuring your code behaves as expected not just in ideal conditions but across various edge cases.
// Null/Undefined checks
expect(null).toBeNull();
expect(undefined).toBeUndefined();
expect(null).toBeNullOrUndefined();
- **Maintainability:** Group related assertions together to improve test maintainability. This makes it easier to update tests as your codebase evolves.
// Custom conditions
expect(7).customAssertion(value => value % 2 === 1, 'Value is not odd');
```
Through judicious use of `@push.rocks/smartexpect`, you can enhance the reliability and maintainability of your test suite, making your codebase more robust and your development workflow more efficient.
#### Arrays and Collections
Work seamlessly with arrays and collections:
```typescript
const testArray = [1, 2, 3];
// Array checks
expect(testArray).toBeArray();
expect(testArray).toHaveLength(3);
expect(testArray).toContain(2);
expect(testArray).toContainAll([1, 3]);
expect(testArray).toExclude(4);
expect([]).toBeEmptyArray();
expect(testArray).toHaveLengthGreaterThan(2);
expect(testArray).toHaveLengthLessThan(4);
// Deep equality in arrays
expect([{ id: 1 }, { id: 2 }]).toContainEqual({ id: 1 });
```
#### Strings
String-specific checks:
```typescript
expect('hello world').toStartWith('hello');
expect('hello world').toEndWith('world');
expect('hello world').toInclude('lo wo');
expect('options').toBeOneOf(['choices', 'options', 'alternatives']);
```
#### Functions and Exceptions
Test function behavior and exceptions:
```typescript
const throwingFn = () => { throw new Error('test error'); };
expect(throwingFn).toThrow();
expect(throwingFn).toThrow(Error);
const safeFn = () => 'result';
expect(safeFn).not.toThrow();
```
#### Date Assertions
Work with dates:
```typescript
const now = new Date();
const past = new Date(Date.now() - 10000);
const future = new Date(Date.now() + 10000);
expect(now).toBeDate();
expect(now).toBeAfterDate(past);
expect(now).toBeBeforeDate(future);
```
### Debugging Assertions
The `log()` method is useful for debugging complex assertions:
```typescript
expect(complexObject)
.property('users')
.log() // Logs the current value in the assertion chain
.arrayItem(0)
.log() // Logs the first user
.property('permissions')
.log() // Logs the permissions object
.property('admin')
.toBeTrue();
```
### Customizing Error Messages
You can provide custom error messages for more meaningful test failures:
```typescript
expect(user.age)
.setFailMessage('User age must be at least 18 for adult content')
.toBeGreaterThanOrEqual(18);
```
### Custom Matchers
You can define your own matchers via `expect.extend()`:
```typescript
expect.extend({
toBeOdd(received: number) {
const pass = received % 2 === 1;
return {
pass,
message: () =>
`Expected ${received} ${pass ? 'not ' : ''}to be odd`,
};
},
});
// Then use your custom matcher in tests:
expect(3).toBeOdd();
expect(4).not.toBeOdd();
```
- Matcher functions receive the value under test (`received`) plus any arguments.
- Must return an object with `pass` (boolean) and `message` (string or function) for failure messages.
### Full Matcher Reference
Below is a comprehensive list of all matchers and utility functions available in `@push.rocks/smartexpect`.
#### Modifiers and Utilities
- `.not` Negates the next matcher in the chain.
- `.resolves` Switches to async mode, expecting the promise to resolve.
- `.rejects` Switches to async mode, expecting the promise to reject.
- `.withTimeout(ms)` Sets a timeout (in milliseconds) for async assertions.
- `.timeout(ms)` (deprecated) Alias for `.withTimeout(ms)`.
- `.property(name)` Drill into a property of an object.
- `.arrayItem(index)` Drill into an array element by index.
- `.log()` Logs the current value and assertion path for debugging.
- `.setFailMessage(message)` Override the failure message for the current assertion.
- `.setSuccessMessage(message)` Override the success message for the current assertion.
- `.customAssertion(fn, message)` Execute a custom assertion function with a message.
- `expect.extend(matchers)` Register custom matchers globally.
- `expect.any(constructor)` Matcher for values that are instances of the given constructor.
- `expect.anything()` Matcher for any defined value (not null or undefined).
#### Basic Matchers
- `.toEqual(expected)` Deep (or strict for primitives) equality.
- `.toBeTrue()` Value is strictly `true`.
- `.toBeFalse()` Value is strictly `false`.
- `.toBeTruthy()` Value is truthy.
- `.toBeFalsy()` Value is falsy.
- `.toBeNull()` Value is `null`.
- `.toBeUndefined()` Value is `undefined`.
- `.toBeNullOrUndefined()` Value is `null` or `undefined`.
- `.toBeDefined()` Value is not `undefined`.
#### Number Matchers
- `.toBeGreaterThan(value)`
- `.toBeLessThan(value)`
- `.toBeGreaterThanOrEqual(value)`
- `.toBeLessThanOrEqual(value)`
- `.toBeCloseTo(value, precision?)`
- `.toEqual(value)` Strict equality for numbers.
- `.toBeNaN()`
- `.toBeFinite()`
- `.toBeWithinRange(min, max)`
#### String Matchers
- `.toStartWith(prefix)`
- `.toEndWith(suffix)`
- `.toInclude(substring)`
- `.toMatch(regex)`
- `.toBeOneOf(arrayOfValues)`
- `.toHaveLength(length)`
- `.toBeEmpty()` Alias for empty string.
#### Array Matchers
- `.toBeArray()`
- `.toHaveLength(length)`
- `.toContain(value)`
- `.toContainEqual(value)`
- `.toContainAll(arrayOfValues)`
- `.toExclude(value)`
- `.toBeEmptyArray()`
- `.toBeEmpty()` Alias for empty array.
- `.toHaveLengthGreaterThan(length)`
- `.toHaveLengthLessThan(length)`
#### Object Matchers
- `.toEqual(expected)` Deep equality for objects.
- `.toMatchObject(partialObject)` Partial deep matching (supports `expect.any` and `expect.anything`).
- `.toBeInstanceOf(constructor)`
- `.toHaveProperty(propertyName, value?)`
- `.toHaveDeepProperty(pathArray)`
- `.toHaveOwnProperty(propertyName, value?)`
- `.toBeNull()`
- `.toBeUndefined()`
- `.toBeNullOrUndefined()`
#### Function Matchers
- `.toThrow(expectedError?)`
- `.toThrowErrorMatching(regex)`
- `.toThrowErrorWithMessage(message)`
#### Date Matchers
- `.toBeDate()`
- `.toBeBeforeDate(date)`
- `.toBeAfterDate(date)`
#### Type Matchers
- `.toBeTypeofString()`
- `.toBeTypeofNumber()`
- `.toBeTypeofBoolean()`
- `.toBeTypeOf(typeName)`
## Best Practices
- **Human-readable assertions**: The fluent API is designed to create tests that read like natural language sentences.
- **Precise error messages**: When tests fail, the error messages provide detailed information about what went wrong, including expected vs. actual values.
- **Property path navigation**: Use the property path methods to navigate complex objects without creating temporary variables.
- **Comprehensive testing**: Take advantage of the wide range of assertion methods to test various aspects of your code.
- **Debugging with log()**: Use the `log()` method to see intermediate values in the assertion chain during test development.
## License and Legal Information
@ -141,4 +383,4 @@ Registered at District court Bremen HRB 35230 HB, Germany
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.

221
test/test.both.ts Normal file
View File

@ -0,0 +1,221 @@
import { tap } from '@push.rocks/tapbundle';
// Import the built library (dist_ts) so all matcher implementations are available
import * as smartexpect from '../dist_ts/index.js';
tap.test('basic type assertions', async () => {
// String type checks
smartexpect.expect('hello').type.toBeTypeofString();
smartexpect.expect(1).not.type.toBeTypeofString();
// Boolean type checks
smartexpect.expect(true).type.toBeTypeofBoolean();
smartexpect.expect(false).type.toBeTypeofBoolean();
smartexpect.expect(1).not.type.toBeTypeofBoolean();
// Number type checks
smartexpect.expect(42).type.toBeTypeofNumber();
smartexpect.expect(true).not.type.toBeTypeofNumber();
// Generic type checks with new method
smartexpect.expect(() => {}).type.toBeTypeOf('function');
smartexpect.expect(class Test {}).type.toBeTypeOf('function');
smartexpect.expect({}).type.toBeTypeOf('object');
smartexpect.expect(Symbol('test')).type.toBeTypeOf('symbol');
});
tap.test('async tests', async (toolsArg) => {
const deferred = toolsArg.defer();
toolsArg.delayFor(1000).then(() => {
deferred.resolve('hello');
});
// Using .resolves to test promise resolution with timeout
await smartexpect.expect(deferred.promise).resolves.withTimeout(2000).type.toBeTypeofString();
await smartexpect.expect(deferred.promise).resolves.not.type.toBeTypeofBoolean();
// Test async timeout handling
const longOperation = toolsArg.defer();
toolsArg.delayFor(3000).then(() => {
longOperation.resolve('completed');
});
try {
// Assert that resolution must occur within timeout
await smartexpect.expect(longOperation.promise).resolves.withTimeout(1000).toBeDefined();
throw new Error('Should have timed out');
} catch (err) {
// Successfully caught timeout error from .withTimeout
console.log('Successfully caught timeout:', err.message);
}
});
tap.test('equality and matching assertions', async () => {
// Basic equality
smartexpect.expect('hithere').object.toEqual('hithere');
smartexpect.expect('hithere').not.object.toEqual('hithere2');
// Object equality
const obj1 = { a: 1, b: { c: true } };
const obj2 = { a: 1, b: { c: true } };
const obj3 = { a: 1, b: { c: false } };
smartexpect.expect(obj1).object.toEqual(obj2);
smartexpect.expect(obj1).not.object.toEqual(obj3);
// RegExp matching
smartexpect.expect('hithere').string.toMatch(/hi/);
smartexpect.expect('hithere').string.toMatch(/^hithere$/);
smartexpect.expect('hithere').not.string.toMatch(/ho/);
// String inclusion
smartexpect.expect('hithere').string.toInclude('hit');
smartexpect.expect('hithere').not.string.toInclude('missing');
// String start/end
smartexpect.expect('hithere').string.toStartWith('hi');
smartexpect.expect('hithere').string.toEndWith('ere');
});
tap.test('object property assertions', async () => {
const testObject = {
topLevel: 'hello',
nested: {
prop: 42,
deeplyNested: {
array: [1, 2, 3],
},
},
};
// Basic property checks
smartexpect.expect(testObject).object.toHaveProperty('topLevel');
smartexpect.expect(testObject).object.toHaveProperty('topLevel', 'hello');
smartexpect.expect(testObject).not.object.toHaveProperty('missing');
// Drill-down property navigation
smartexpect.expect(testObject).property('nested').object.toHaveProperty('prop', 42);
smartexpect
.expect(testObject)
.property('nested')
.property('deeplyNested')
.property('array')
.array.toBeArray();
// Deep property checks
smartexpect.expect(testObject).object.toHaveDeepProperty(['nested', 'deeplyNested', 'array']);
// Array item navigation
smartexpect
.expect(testObject)
.property('nested')
.property('deeplyNested')
.property('array')
.arrayItem(0)
.number.toEqual(1); // numeric equality via number namespace
});
tap.test('numeric comparison assertions', async () => {
// Greater/less than
smartexpect.expect(4).number.toBeGreaterThan(3);
smartexpect.expect(4).number.toBeLessThan(5);
smartexpect.expect(4).number.toBeGreaterThanOrEqual(4);
smartexpect.expect(4).number.toBeLessThanOrEqual(4);
// Approximate equality
smartexpect.expect(0.1 + 0.2).number.toBeCloseTo(0.3, 10);
});
tap.test('array assertions', async () => {
const obj1 = { id: 1 };
const obj2 = { id: 2 };
const testArray = [1, 'two', obj1, true];
// Basic array checks
smartexpect.expect(testArray).array.toBeArray();
smartexpect.expect(testArray).array.toHaveLength(4);
// Content checks
smartexpect.expect(testArray).array.toContain('two');
smartexpect.expect(testArray).array.toContain(obj1);
smartexpect.expect(testArray).not.array.toContain(obj2);
// Array with equal items (not same reference)
smartexpect.expect([{ a: 1 }, { b: 2 }]).array.toContainEqual({ a: 1 });
// Multiple values
smartexpect.expect(testArray).array.toContainAll([1, 'two']);
smartexpect.expect(testArray).array.toExclude('missing');
// Empty array
smartexpect.expect([]).array.toBeEmptyArray();
// Length comparisons
smartexpect.expect(testArray).array.toHaveLengthGreaterThan(3);
smartexpect.expect(testArray).array.toHaveLengthLessThan(5);
});
tap.test('boolean assertions', async () => {
// True/False
smartexpect.expect(true).boolean.toBeTrue();
smartexpect.expect(false).boolean.toBeFalse();
// Truthy/Falsy
smartexpect.expect('something').boolean.toBeTruthy();
smartexpect.expect(0).boolean.toBeFalsy();
// Null/Undefined
smartexpect.expect(null).object.toBeNull();
smartexpect.expect(undefined).object.toBeUndefined();
smartexpect.expect(null).object.toBeNullOrUndefined();
smartexpect.expect(undefined).object.toBeNullOrUndefined();
});
tap.test('function assertions', async () => {
// Function that throws
const throwingFn = () => {
throw new Error('test error');
};
smartexpect.expect(throwingFn).function.toThrow();
smartexpect.expect(throwingFn).function.toThrow(Error);
// Function that doesn't throw
const nonThrowingFn = () => 'safe';
smartexpect.expect(nonThrowingFn).not.function.toThrow();
});
tap.test('date assertions', async () => {
const now = new Date();
const past = new Date(Date.now() - 10000);
const future = new Date(Date.now() + 10000);
smartexpect.expect(now).date.toBeDate();
smartexpect.expect(now).date.toBeAfterDate(past);
smartexpect.expect(now).date.toBeBeforeDate(future);
});
tap.test('custom assertions', async () => {
// Custom validation logic
smartexpect.expect(42).customAssertion((value) => value % 2 === 0, 'Expected number to be even');
// With fail message
smartexpect.expect('test').setFailMessage('Custom fail message for assertion').string.toHaveLength(4);
});
tap.test('logging and debugging', async () => {
// Using log() for debugging
const complexObject = {
level1: {
level2: {
value: 'nested value',
},
},
};
// This logs the current value in the chain for debugging
smartexpect
.expect(complexObject)
.property('level1')
.property('level2')
.log()
.property('value')
.object.toEqual('nested value');
});
export default tap.start();

32
test/test.diffoutput.ts Normal file
View File

@ -0,0 +1,32 @@
import { tap, expect as tExpect } from '@push.rocks/tapbundle';
import * as smartexpect from '../dist_ts/index.js';
tap.test('diff-like output for object.toEqual mismatch', async () => {
const a = { x: 1, y: 2 };
const b = { x: 1, y: 3 };
try {
smartexpect.expect(a).object.toEqual(b);
throw new Error('Assertion did not throw');
} catch (err: any) {
const msg: string = err.message;
tExpect(msg.includes('Expected objects to be deeply equal')).toBeTrue();
tExpect(msg.includes('Received:')).toBeTrue();
tExpect(msg.includes('"y": 2')).toBeTrue();
}
});
tap.test('diff-like output for array.toContainEqual mismatch', async () => {
const arr = [{ id: 1 }, { id: 2 }];
const item = { id: 3 };
try {
smartexpect.expect(arr).array.toContainEqual(item);
throw new Error('Assertion did not throw');
} catch (err: any) {
const msg: string = err.message;
tExpect(msg.includes('Expected array to contain equal to')).toBeTrue();
tExpect(msg.includes('Received:')).toBeTrue();
tExpect(msg.includes('"id": 1')).toBeTrue();
}
});
export default tap.start();

38
test/test.expectany.ts Normal file
View File

@ -0,0 +1,38 @@
import { tap } from '@push.rocks/tapbundle';
import * as smartexpect from '../dist_ts/index.js';
tap.test('expect.any and expect.anything basic usage', async () => {
const obj = { a: 1, b: 'two', d: new Date() };
// Using expect.any to match types
smartexpect.expect(obj).object.toMatchObject({
a: smartexpect.expect.any(Number),
b: smartexpect.expect.any(String),
d: smartexpect.expect.any(Date),
});
// Using expect.anything to match any defined value
smartexpect.expect(obj).object.toMatchObject({
a: smartexpect.expect.anything(),
b: smartexpect.expect.anything(),
d: smartexpect.expect.anything(),
});
});
tap.test('expect.any mismatch and anything null/undefined rejection', async () => {
const obj = { a: 1, b: null };
// Mismatch for expect.any
try {
smartexpect.expect(obj).object.toMatchObject({ a: smartexpect.expect.any(String) });
throw new Error('Expected mismatch for expect.any did not throw');
} catch (err) {
// success: thrown on mismatch
}
// anything should reject null or undefined
try {
smartexpect.expect(obj).object.toMatchObject({ b: smartexpect.expect.anything() });
throw new Error('Expected anything() to reject null or undefined');
} catch (err) {
// success: thrown on null
}
});
export default tap.start();

51
test/test.keys.ts Normal file
View File

@ -0,0 +1,51 @@
import { tap, expect as tExpect } from '@push.rocks/tapbundle';
import * as smartexpect from '../dist_ts/index.js';
// Positive: toHaveKeys should match both own and inherited properties
tap.test('toHaveKeys includes own and inherited properties', async () => {
class Parent { a = 1; }
class Child extends Parent { b = 2; }
const child = new Child();
smartexpect.expect(child).object.toHaveKeys(['a', 'b']);
});
// Positive: toHaveOwnKeys should match only own properties
tap.test('toHaveOwnKeys includes only own properties', async () => {
class Parent { a = 1; }
class Child extends Parent { b = 2; }
const child = new Child();
smartexpect.expect(child).object.toHaveOwnKeys(['b']);
});
// Negative: toHaveKeys should fail if any key is missing
tap.test('toHaveKeys fails when keys are missing', async () => {
const obj = { a: 1 };
try {
smartexpect.expect(obj).object.toHaveKeys(['a', 'b']);
throw new Error('Assertion did not throw');
} catch (err: any) {
tExpect(err.message.includes('Expected object to have keys')).toBeTrue();
}
});
// Negative: toHaveOwnKeys should fail when own keys are missing (inherited keys not counted)
tap.test('toHaveOwnKeys fails when own keys are missing', async () => {
// 'a' is inherited via the prototype, not an own property
class Parent {}
Parent.prototype.a = 1;
class Child extends Parent {
constructor() {
super();
this.b = 2;
}
}
const child = new Child();
try {
smartexpect.expect(child).object.toHaveOwnKeys(['a', 'b']);
throw new Error('Assertion did not throw');
} catch (err: any) {
tExpect(err.message.includes('Expected object to have own keys')).toBeTrue();
}
});
export default tap.start();

22
test/test.negation.ts Normal file
View File

@ -0,0 +1,22 @@
import { tap, expect as tExpect } from '@push.rocks/tapbundle';
import * as smartexpect from '../dist_ts/index.js';
tap.test('negation message for numeric matcher', async () => {
try {
smartexpect.expect(5).not.toBeGreaterThan(3);
throw new Error('Assertion did not throw');
} catch (err: any) {
tExpect(err.message).toEqual('Expected number not to be greater than 3');
}
});
tap.test('negation message for string matcher', async () => {
try {
smartexpect.expect('hello').not.string.toInclude('he');
throw new Error('Assertion did not throw');
} catch (err: any) {
tExpect(err.message).toEqual('Expected string not to include "he"');
}
});
export default tap.start();

17
test/test.propertypath.ts Normal file
View File

@ -0,0 +1,17 @@
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' }}, publicTest: 'hi' };
smartexpect.expect(testObject).object.toHaveProperty('publicTest');
smartexpect.expect(testObject).toHaveProperty('publicTest');
// 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();

View File

@ -1,52 +0,0 @@
import { tap } from '@push.rocks/tapbundle';
import * as smartexpect from '../ts/index.js';
tap.test('sync tests', async () => {
smartexpect.expect('hello').toBeTypeofString();
smartexpect.expect(1).not.toBeTypeofString();
smartexpect.expect(true).toBeTypeofBoolean();
smartexpect.expect(true).not.toBeTypeofNumber();
});
tap.test('async tests', async (toolsArg) => {
const deferred = toolsArg.defer();
toolsArg.delayFor(4000).then(() => {
deferred.resolve('hello');
});
await smartexpect.expectAsync(deferred.promise).timeout(5000).toBeTypeofString();
await smartexpect.expectAsync(deferred.promise).not.toBeTypeofBoolean();
});
tap.test('should check equality', async () => {
smartexpect.expect('hithere').toEqual('hithere');
smartexpect.expect('hithere').not.toEqual('hithere2');
});
tap.test('should check for regexp matching', async () => {
smartexpect.expect('hithere').toMatch(/hi/);
smartexpect.expect('hithere').not.toMatch(/ho/);
});
tap.test('should correctly state property presence', async () => {
const testObject = {
aprop: 'hello',
};
smartexpect.expect(testObject).toHaveProperty('aprop');
smartexpect.expect(testObject).not.toHaveProperty('aprop2');
});
tap.test('should be greater than', async () => {
smartexpect.expect(4).toBeGreaterThan(3);
smartexpect.expect(4).toBeLessThan(5);
});
tap.test('should correctly determine toContain', async () => {
const hello = {
socool: 'yes',
};
const testArray = [hello];
smartexpect.expect(testArray).toContain(hello);
});
tap.start();

View File

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

View File

@ -1,12 +1,41 @@
import { Assertion } from './smartexpect.classes.assertion.js';
import { Assertion, AnyMatcher, AnythingMatcher } from './smartexpect.classes.assertion.js';
import type { TExecutionType } from './types.js';
// import type { TMatcher } from './smartexpect.classes.assertion.js'; // unused
export const expect = (baseArg: any) => {
const assertion = new Assertion(baseArg, 'sync');
return assertion;
};
export const expectAsync = (baseArg: any) => {
const assertion = new Assertion(baseArg, 'async');
return assertion;
};
/**
* Primary entry point for assertions.
* Automatically detects Promises to support async assertions.
*/
/**
* The `expect` function interface. Supports custom matchers via .extend.
*/
/**
* Entry point for assertions.
* Automatically detects Promises to support async assertions.
*/
export function expect<T>(value: Promise<T>): Assertion<T, 'async'>;
export function expect<T>(value: T): Assertion<T, 'sync'>;
export function expect<T>(value: any): Assertion<T, TExecutionType> {
const isThenable = value != null && typeof (value as any).then === 'function';
const mode: 'sync' | 'async' = isThenable ? 'async' : 'sync';
return new Assertion<T, TExecutionType>(value, mode);
}
/**
* Register custom matchers.
*/
export namespace expect {
export const extend = Assertion.extend;
/**
* Matcher for a specific constructor. Passes if value is instance of given constructor.
*/
export function any(constructor: any) {
return new AnyMatcher(constructor);
}
/**
* Matcher for any defined value (not null or undefined).
*/
export function anything() {
return new AnythingMatcher();
}
}

81
ts/namespaces/array.ts Normal file
View File

@ -0,0 +1,81 @@
import { Assertion } from '../smartexpect.classes.assertion.js';
import * as plugins from '../plugins.js';
import type { TExecutionType } from '../types.js';
/**
* Namespace for array-specific matchers
*/
export class ArrayMatchers<T, M extends TExecutionType> {
constructor(private assertion: Assertion<T[], M>) {}
toBeArray() {
return this.assertion.customAssertion(
(value) => Array.isArray(value),
`Expected value to be array`
);
}
toHaveLength(length: number) {
return this.assertion.customAssertion(
(value) => (value as T[]).length === length,
`Expected array to have length ${length}`
);
}
toContain(item: T) {
return this.assertion.customAssertion(
(value) => (value as T[]).includes(item),
`Expected array to contain ${JSON.stringify(item)}`
);
}
toContainEqual(item: T) {
return this.assertion.customAssertion(
(value) => (value as T[]).some((e) => plugins.fastDeepEqual(e, item)),
(value) =>
`Expected array to contain equal to ${JSON.stringify(item)}` +
`\nReceived: ${JSON.stringify(value, null, 2)}`
);
}
toContainAll(items: T[]) {
return this.assertion.customAssertion(
(value) => items.every((i) => (value as T[]).includes(i)),
`Expected array to contain all ${JSON.stringify(items)}`
);
}
toExclude(item: T) {
return this.assertion.customAssertion(
(value) => !(value as T[]).includes(item),
`Expected array to exclude ${JSON.stringify(item)}`
);
}
toBeEmptyArray() {
return this.assertion.customAssertion(
(value) => Array.isArray(value) && (value as T[]).length === 0,
`Expected array to be empty`
);
}
/**
* Alias for empty array check
*/
toBeEmpty() {
return this.toBeEmptyArray();
}
toHaveLengthGreaterThan(length: number) {
return this.assertion.customAssertion(
(value) => (value as T[]).length > length,
`Expected array to have length greater than ${length}`
);
}
toHaveLengthLessThan(length: number) {
return this.assertion.customAssertion(
(value) => (value as T[]).length < length,
`Expected array to have length less than ${length}`
);
}
}

37
ts/namespaces/boolean.ts Normal file
View File

@ -0,0 +1,37 @@
import { Assertion } from '../smartexpect.classes.assertion.js';
import type { TExecutionType } from '../types.js';
/**
* Namespace for boolean-specific matchers
*/
export class BooleanMatchers<M extends TExecutionType> {
constructor(private assertion: Assertion<boolean, M>) {}
toBeTrue() {
return this.assertion.customAssertion(
(v) => v === true,
`Expected value to be true`
);
}
toBeFalse() {
return this.assertion.customAssertion(
(v) => v === false,
`Expected value to be false`
);
}
toBeTruthy() {
return this.assertion.customAssertion(
(v) => Boolean(v),
`Expected value to be truthy`
);
}
toBeFalsy() {
return this.assertion.customAssertion(
(v) => !v,
`Expected value to be falsy`
);
}
}

30
ts/namespaces/date.ts Normal file
View File

@ -0,0 +1,30 @@
import { Assertion } from '../smartexpect.classes.assertion.js';
import type { TExecutionType } from '../types.js';
/**
* Namespace for date-specific matchers
*/
export class DateMatchers<M extends TExecutionType> {
constructor(private assertion: Assertion<Date, M>) {}
toBeDate() {
return this.assertion.customAssertion(
(v) => v instanceof Date,
`Expected value to be a Date instance`
);
}
toBeBeforeDate(date: Date) {
return this.assertion.customAssertion(
(v) => v instanceof Date && (v as Date).getTime() < date.getTime(),
`Expected date to be before ${date.toISOString()}`
);
}
toBeAfterDate(date: Date) {
return this.assertion.customAssertion(
(v) => v instanceof Date && (v as Date).getTime() > date.getTime(),
`Expected date to be after ${date.toISOString()}`
);
}
}

62
ts/namespaces/function.ts Normal file
View File

@ -0,0 +1,62 @@
import { Assertion } from '../smartexpect.classes.assertion.js';
import type { TExecutionType } from '../types.js';
/**
* Namespace for function-specific matchers
*/
export class FunctionMatchers<M extends TExecutionType> {
constructor(private assertion: Assertion<Function, M>) {}
toThrow(expectedError?: any) {
return this.assertion.customAssertion(
(value) => {
let threw = false;
try {
(value as Function)();
} catch (e: any) {
threw = true;
if (expectedError) {
if (typeof expectedError === 'function') {
return e instanceof expectedError;
}
return e === expectedError;
}
}
return threw;
},
`Expected function to throw${expectedError ? ` ${expectedError}` : ''}`
);
}
/**
* Assert thrown error message matches the given regex
*/
toThrowErrorMatching(regex: RegExp) {
return this.assertion.customAssertion(
(value) => {
try {
(value as Function)();
} catch (e: any) {
return regex.test(e && e.message);
}
return false;
},
`Expected function to throw error matching ${regex}`
);
}
/**
* Assert thrown error message equals the given string
*/
toThrowErrorWithMessage(expectedMessage: string) {
return this.assertion.customAssertion(
(value) => {
try {
(value as Function)();
} catch (e: any) {
return e && e.message === expectedMessage;
}
return false;
},
`Expected function to throw error with message "${expectedMessage}"`
);
}
}

8
ts/namespaces/index.ts Normal file
View File

@ -0,0 +1,8 @@
export { StringMatchers } from './string.js';
export { ArrayMatchers } from './array.js';
export { NumberMatchers } from './number.js';
export { BooleanMatchers } from './boolean.js';
export { ObjectMatchers } from './object.js';
export { FunctionMatchers } from './function.js';
export { DateMatchers } from './date.js';
export { TypeMatchers } from './type.js';

84
ts/namespaces/number.ts Normal file
View File

@ -0,0 +1,84 @@
import { Assertion } from '../smartexpect.classes.assertion.js';
import type { TExecutionType } from '../types.js';
/**
* Namespace for number-specific matchers
*/
export class NumberMatchers<M extends TExecutionType> {
constructor(private assertion: Assertion<number, M>) {}
toBeGreaterThan(value: number) {
return this.assertion.customAssertion(
(v) => (v as number) > value,
`Expected number to be greater than ${value}`
);
}
toBeLessThan(value: number) {
return this.assertion.customAssertion(
(v) => (v as number) < value,
`Expected number to be less than ${value}`
);
}
toBeGreaterThanOrEqual(value: number) {
return this.assertion.customAssertion(
(v) => (v as number) >= value,
`Expected number to be greater than or equal to ${value}`
);
}
toBeLessThanOrEqual(value: number) {
return this.assertion.customAssertion(
(v) => (v as number) <= value,
`Expected number to be less than or equal to ${value}`
);
}
toBeCloseTo(value: number, precision?: number) {
return this.assertion.customAssertion(
(v) => {
const num = v as number;
const p = precision !== undefined ? precision : 2;
const diff = Math.abs(num - value);
const tolerance = 0.5 * Math.pow(10, -p);
return diff <= tolerance;
},
`Expected number to be close to ${value} within precision ${precision ?? 2}`
);
}
/** Equality check for numbers */
toEqual(value: number) {
return this.assertion.customAssertion(
(v) => (v as number) === value,
`Expected number to equal ${value}`
);
}
/**
* Checks for NaN
*/
toBeNaN() {
return this.assertion.customAssertion(
(v) => Number.isNaN(v as number),
`Expected number to be NaN`
);
}
/**
* Checks for finite number
*/
toBeFinite() {
return this.assertion.customAssertion(
(v) => Number.isFinite(v as number),
`Expected number to be finite`
);
}
/**
* Checks if number is within inclusive range
*/
toBeWithinRange(min: number, max: number) {
return this.assertion.customAssertion(
(v) => (v as number) >= min && (v as number) <= max,
`Expected number to be within range ${min} - ${max}`
);
}
}

172
ts/namespaces/object.ts Normal file
View File

@ -0,0 +1,172 @@
import { Assertion, AnyMatcher, AnythingMatcher } from '../smartexpect.classes.assertion.js';
import type { TExecutionType } from '../types.js';
import * as plugins from '../plugins.js';
/**
* Namespace for object-specific matchers
*/
export class ObjectMatchers<T extends object, M extends TExecutionType> {
constructor(private assertion: Assertion<T, M>) {}
toEqual(expected: any) {
return this.assertion.customAssertion(
(v) => plugins.fastDeepEqual(v, expected),
(v) =>
`Expected objects to be deeply equal to ${JSON.stringify(expected, null, 2)}` +
`\nReceived: ${JSON.stringify(v, null, 2)}`
);
}
toMatchObject(expected: object) {
return this.assertion.customAssertion(
(v) => {
const obj = v as any;
for (const key of Object.keys(expected)) {
const expectedVal = (expected as any)[key];
const actualVal = obj[key];
if (expectedVal instanceof AnyMatcher) {
const ctor = expectedVal.expectedConstructor;
if (ctor === Number) {
if (typeof actualVal !== 'number') return false;
} else if (ctor === String) {
if (typeof actualVal !== 'string') return false;
} else if (ctor === Boolean) {
if (typeof actualVal !== 'boolean') return false;
} else {
if (!(actualVal instanceof ctor)) return false;
}
} else if (expectedVal instanceof AnythingMatcher) {
if (actualVal === null || actualVal === undefined) {
return false;
}
} else if (!plugins.fastDeepEqual(actualVal, expectedVal)) {
return false;
}
}
return true;
},
(v) =>
`Expected object to match properties ${JSON.stringify(expected, null, 2)}` +
`\nReceived: ${JSON.stringify(v, null, 2)}`
);
}
toBeInstanceOf(constructor: any) {
return this.assertion.customAssertion(
(v) => (v as any) instanceof constructor,
`Expected object to be instance of ${constructor.name || constructor}`
);
}
toHaveProperty(property: string, value?: any) {
return this.assertion.customAssertion(
(v) => {
const obj = v as any;
// first check for a literal property (including inherited)
if (property in obj) {
if (arguments.length === 2) {
return plugins.fastDeepEqual(obj[property], value);
}
return true;
}
// no direct key, try nested path via dot notation
const path = property.split('.');
let current = obj;
for (const key of path) {
if (current == null || !(key in current)) {
return false;
}
current = (current as any)[key];
}
if (arguments.length === 2) {
return plugins.fastDeepEqual(current, value);
}
return true;
},
`Expected object to have property ${property}${value !== undefined ? ` with value ${JSON.stringify(value)}` : ''}`
);
}
toHaveDeepProperty(path: string[]) {
return this.assertion.customAssertion(
(v) => {
let obj: any = v;
for (const key of path) {
if (obj == null || !(key in obj)) {
return false;
}
obj = obj[key];
}
return true;
},
`Expected object to have deep property path ${JSON.stringify(path)}`
);
}
toBeNull() {
return this.assertion.customAssertion(
(v) => v === null,
`Expected value to be null`
);
}
toBeUndefined() {
return this.assertion.customAssertion(
(v) => v === undefined,
`Expected value to be undefined`
);
}
toBeNullOrUndefined() {
return this.assertion.customAssertion(
(v) => v === null || v === undefined,
`Expected value to be null or undefined`
);
}
/**
* Checks own property only (not inherited)
*/
toHaveOwnProperty(property: string, value?: any) {
return this.assertion.customAssertion(
(v) => {
const obj = v as any;
if (!Object.prototype.hasOwnProperty.call(obj, property)) {
return false;
}
if (arguments.length === 2) {
return plugins.fastDeepEqual(obj[property], value);
}
return true;
},
(v) =>
`Expected object to have own property ${property}` +
(value !== undefined ? ` with value ${JSON.stringify(value)}` : ``) +
`\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)}`
);
}
}

60
ts/namespaces/string.ts Normal file
View File

@ -0,0 +1,60 @@
import { Assertion } from '../smartexpect.classes.assertion.js';
import type { TExecutionType } from '../types.js';
/**
* Namespace for string-specific matchers
*/
export class StringMatchers<M extends TExecutionType> {
constructor(private assertion: Assertion<string, M>) {}
toStartWith(prefix: string) {
return this.assertion.customAssertion(
(value) => (value as string).startsWith(prefix),
`Expected string to start with "${prefix}"`
);
}
toEndWith(suffix: string) {
return this.assertion.customAssertion(
(value) => (value as string).endsWith(suffix),
`Expected string to end with "${suffix}"`
);
}
toInclude(substring: string) {
return this.assertion.customAssertion(
(value) => (value as string).includes(substring),
`Expected string to include "${substring}"`
);
}
toMatch(regex: RegExp) {
return this.assertion.customAssertion(
(value) => regex.test(value as string),
`Expected string to match ${regex}`
);
}
toBeOneOf(values: string[]) {
return this.assertion.customAssertion(
(value) => (values as string[]).includes(value as string),
`Expected string to be one of ${JSON.stringify(values)}`
);
}
/** Length check for strings */
toHaveLength(length: number) {
return this.assertion.customAssertion(
(value) => (value as string).length === length,
`Expected string to have length ${length}`
);
}
/**
* Alias for empty string check
*/
toBeEmpty() {
return this.assertion.customAssertion(
(value) => (value as string).length === 0,
`Expected string to be empty`
);
}
}

44
ts/namespaces/type.ts Normal file
View File

@ -0,0 +1,44 @@
import { Assertion } from '../smartexpect.classes.assertion.js';
import type { TExecutionType } from '../types.js';
/**
* Namespace for type-based matchers
*/
export class TypeMatchers<M extends TExecutionType> {
constructor(private assertion: Assertion<any, M>) {}
toBeTypeofString() {
return this.assertion.customAssertion(
(v) => typeof v === 'string',
`Expected type to be 'string'`
);
}
toBeTypeofNumber() {
return this.assertion.customAssertion(
(v) => typeof v === 'number',
`Expected type to be 'number'`
);
}
toBeTypeofBoolean() {
return this.assertion.customAssertion(
(v) => typeof v === 'boolean',
`Expected type to be 'boolean'`
);
}
toBeTypeOf(typeName: string) {
return this.assertion.customAssertion(
(v) => typeof v === typeName,
`Expected type to be '${typeName}'`
);
}
toBeDefined() {
return this.assertion.customAssertion(
(v) => v !== undefined,
`Expected value to be defined`
);
}
}

View File

@ -1,9 +1,7 @@
import * as smartdelay from '@push.rocks/smartdelay';
import * as smartpromise from '@push.rocks/smartpromise';
export { smartdelay, smartpromise };
// third party scope
// third party utilities
import fastDeepEqual from 'fast-deep-equal';
export { fastDeepEqual };
export { fastDeepEqual };

File diff suppressed because it is too large Load Diff

13
ts/types.ts Normal file
View File

@ -0,0 +1,13 @@
/**
* Common types for smartexpect
*/
/** Execution mode: sync or async */
export type TExecutionType = 'sync' | 'async';
/**
* Definition of a custom matcher function.
* Should return an object with `pass` and optional `message`.
*/
export type TMatcher = (
received: any,
...args: any[]
) => { pass: boolean; message?: string | (() => string) };

View File

@ -1,6 +1,7 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"useDefineForClassFields": false,
"target": "ES2022",
"module": "NodeNext",