Compare commits

...

4 Commits

19 changed files with 829 additions and 664 deletions

View File

@ -1,5 +1,20 @@
# Changelog
## 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.

View File

@ -1,6 +1,6 @@
{
"name": "@push.rocks/smartexpect",
"version": "1.6.1",
"version": "2.0.1",
"private": false,
"description": "A testing library to manage expectations in code, offering both synchronous and asynchronous assertion methods.",
"main": "dist_ts/index.js",
@ -62,5 +62,6 @@
"onlyBuiltDependencies": [
"mongodb-memory-server"
]
}
},
"packageManager": "pnpm@10.7.0+sha512.6b865ad4b62a1d9842b61d674a393903b871d9244954f652b8842c2b553c72176b278f64c463e52d40fff8aba385c235c8c9ecf5cc7de4fd78b8bb6d49633ab6"
}

View File

@ -53,10 +53,10 @@ 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';
@ -64,8 +64,8 @@ const asyncStringFetcher = async (): Promise<string> => {
const asyncTest = async () => {
// Add a timeout to prevent hanging tests
await expectAsync(asyncStringFetcher()).timeout(5000).toBeTypeofString();
await expectAsync(asyncStringFetcher()).toEqual('async string');
await expect(asyncStringFetcher()).resolves.withTimeout(5000).type.toBeTypeofString();
await expect(asyncStringFetcher()).resolves.toEqual('async string');
};
asyncTest();
@ -230,6 +230,30 @@ expect(user.age)
.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.
## Best Practices
- **Human-readable assertions**: The fluent API is designed to create tests that read like natural language sentences.

37
readme.plan.md Normal file
View File

@ -0,0 +1,37 @@
# Plan for Improving @push.rocks/smartexpect API
This document captures the roadmap for evolving the `expect` / `expectAsync` API.
## Phase 1: Unify Sync + Async
- [x] Consolidate `expect` and `expectAsync` into a single `expect()` entry point.
- [x] Introduce `.resolves` and `.rejects` chainable helpers for Promises.
- [x] Deprecate `expectAsync`, provide migration guidance.
## Phase 2: Timeout Helper
- [x] Rename or wrap the existing `.timeout(ms)` to a more intuitive `.withTimeout(ms)`.
## Phase 3: Custom Matchers
- [x] Implement `expect.extend()` API for user-defined matchers.
## Phase 4: TypeScript Typings
- [ ] Enhance generic matcher types to infer narrow types after `.property()` / `.arrayItem()`.
- [ ] Provide matcher overloads for primitive categories (string, number, array, etc.).
## Phase 5: Namespaced Matchers
- [ ] Group matchers under `.string`, `.array`, `.number`, etc. for discoverability.
## Phase 6: Jest-Style Convenience
- [ ] Add `.toMatchObject()`, `.toMatchSnapshot()`, `expect.any()`, `expect.anything()`, etc.
## Phase 7: Error Messages & Diffs
- [ ] Integrate a diffing library for clear failure output with colorized diffs.
## Phase 8: Nested Access Chaining
- [ ] Provide `.at(path)` or lens-based API for deep property assertions in one go.
## Phase 9: Pluggable Reporters
- [ ] Allow consumers to swap output format: JSON, TAP, HTML, etc.
## Phase 10: API Cleanup
- [ ] Audit and remove legacy aliases and redundant methods.
- [ ] Finalize deprecations and bump to a major version.

View File

@ -1,25 +1,26 @@
import { tap } from '@push.rocks/tapbundle';
import * as smartexpect from '../ts/index.js';
// 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').toBeTypeofString();
smartexpect.expect(1).not.toBeTypeofString();
smartexpect.expect('hello').type.toBeTypeofString();
smartexpect.expect(1).not.type.toBeTypeofString();
// Boolean type checks
smartexpect.expect(true).toBeTypeofBoolean();
smartexpect.expect(false).toBeTypeofBoolean();
smartexpect.expect(1).not.toBeTypeofBoolean();
smartexpect.expect(true).type.toBeTypeofBoolean();
smartexpect.expect(false).type.toBeTypeofBoolean();
smartexpect.expect(1).not.type.toBeTypeofBoolean();
// Number type checks
smartexpect.expect(42).toBeTypeofNumber();
smartexpect.expect(true).not.toBeTypeofNumber();
smartexpect.expect(42).type.toBeTypeofNumber();
smartexpect.expect(true).not.type.toBeTypeofNumber();
// Generic type checks with new method
smartexpect.expect(() => {}).toBeTypeOf('function');
smartexpect.expect(class Test {}).toBeTypeOf('function');
smartexpect.expect({}).toBeTypeOf('object');
smartexpect.expect(Symbol('test')).toBeTypeOf('symbol');
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) => {
@ -27,8 +28,9 @@ tap.test('async tests', async (toolsArg) => {
toolsArg.delayFor(1000).then(() => {
deferred.resolve('hello');
});
await smartexpect.expectAsync(deferred.promise).timeout(2000).toBeTypeofString();
await smartexpect.expectAsync(deferred.promise).not.toBeTypeofBoolean();
// 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();
@ -36,38 +38,39 @@ tap.test('async tests', async (toolsArg) => {
longOperation.resolve('completed');
});
try {
await smartexpect.expectAsync(longOperation.promise).timeout(1000).toBeDefined();
// 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) {
// Expected timeout error
// 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').toEqual('hithere');
smartexpect.expect('hithere').not.toEqual('hithere2');
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).toEqual(obj2);
smartexpect.expect(obj1).not.toEqual(obj3);
smartexpect.expect(obj1).object.toEqual(obj2);
smartexpect.expect(obj1).not.object.toEqual(obj3);
// RegExp matching
smartexpect.expect('hithere').toMatch(/hi/);
smartexpect.expect('hithere').toMatch(/^hithere$/);
smartexpect.expect('hithere').not.toMatch(/ho/);
smartexpect.expect('hithere').string.toMatch(/hi/);
smartexpect.expect('hithere').string.toMatch(/^hithere$/);
smartexpect.expect('hithere').not.string.toMatch(/ho/);
// String inclusion
smartexpect.expect('hithere').toInclude('hit');
smartexpect.expect('hithere').not.toInclude('missing');
smartexpect.expect('hithere').string.toInclude('hit');
smartexpect.expect('hithere').not.string.toInclude('missing');
// String start/end
smartexpect.expect('hithere').toStartWith('hi');
smartexpect.expect('hithere').toEndWith('ere');
smartexpect.expect('hithere').string.toStartWith('hi');
smartexpect.expect('hithere').string.toEndWith('ere');
});
tap.test('object property assertions', async () => {
@ -82,21 +85,21 @@ tap.test('object property assertions', async () => {
};
// Basic property checks
smartexpect.expect(testObject).toHaveProperty('topLevel');
smartexpect.expect(testObject).toHaveProperty('topLevel', 'hello');
smartexpect.expect(testObject).not.toHaveProperty('missing');
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').toHaveProperty('prop', 42);
smartexpect.expect(testObject).property('nested').object.toHaveProperty('prop', 42);
smartexpect
.expect(testObject)
.property('nested')
.property('deeplyNested')
.property('array')
.toBeArray();
.array.toBeArray();
// Deep property checks
smartexpect.expect(testObject).toHaveDeepProperty(['nested', 'deeplyNested', 'array']);
smartexpect.expect(testObject).object.toHaveDeepProperty(['nested', 'deeplyNested', 'array']);
// Array item navigation
smartexpect
@ -105,18 +108,18 @@ tap.test('object property assertions', async () => {
.property('deeplyNested')
.property('array')
.arrayItem(0)
.toEqual(1);
.number.toEqual(1); // numeric equality via number namespace
});
tap.test('numeric comparison assertions', async () => {
// Greater/less than
smartexpect.expect(4).toBeGreaterThan(3);
smartexpect.expect(4).toBeLessThan(5);
smartexpect.expect(4).toBeGreaterThanOrEqual(4);
smartexpect.expect(4).toBeLessThanOrEqual(4);
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).toBeCloseTo(0.3, 10);
smartexpect.expect(0.1 + 0.2).number.toBeCloseTo(0.3, 10);
});
tap.test('array assertions', async () => {
@ -125,43 +128,43 @@ tap.test('array assertions', async () => {
const testArray = [1, 'two', obj1, true];
// Basic array checks
smartexpect.expect(testArray).toBeArray();
smartexpect.expect(testArray).toHaveLength(4);
smartexpect.expect(testArray).array.toBeArray();
smartexpect.expect(testArray).array.toHaveLength(4);
// Content checks
smartexpect.expect(testArray).toContain('two');
smartexpect.expect(testArray).toContain(obj1);
smartexpect.expect(testArray).not.toContain(obj2);
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 }]).toContainEqual({ a: 1 });
smartexpect.expect([{ a: 1 }, { b: 2 }]).array.toContainEqual({ a: 1 });
// Multiple values
smartexpect.expect(testArray).toContainAll([1, 'two']);
smartexpect.expect(testArray).toExclude('missing');
smartexpect.expect(testArray).array.toContainAll([1, 'two']);
smartexpect.expect(testArray).array.toExclude('missing');
// Empty array
smartexpect.expect([]).toBeEmptyArray();
smartexpect.expect([]).array.toBeEmptyArray();
// Length comparisons
smartexpect.expect(testArray).toHaveLengthGreaterThan(3);
smartexpect.expect(testArray).toHaveLengthLessThan(5);
smartexpect.expect(testArray).array.toHaveLengthGreaterThan(3);
smartexpect.expect(testArray).array.toHaveLengthLessThan(5);
});
tap.test('boolean assertions', async () => {
// True/False
smartexpect.expect(true).toBeTrue();
smartexpect.expect(false).toBeFalse();
smartexpect.expect(true).boolean.toBeTrue();
smartexpect.expect(false).boolean.toBeFalse();
// Truthy/Falsy
smartexpect.expect('something').toBeTruthy();
smartexpect.expect(0).toBeFalsy();
smartexpect.expect('something').boolean.toBeTruthy();
smartexpect.expect(0).boolean.toBeFalsy();
// Null/Undefined
smartexpect.expect(null).toBeNull();
smartexpect.expect(undefined).toBeUndefined();
smartexpect.expect(null).toBeNullOrUndefined();
smartexpect.expect(undefined).toBeNullOrUndefined();
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 () => {
@ -169,12 +172,12 @@ tap.test('function assertions', async () => {
const throwingFn = () => {
throw new Error('test error');
};
smartexpect.expect(throwingFn).toThrow();
smartexpect.expect(throwingFn).toThrow(Error);
smartexpect.expect(throwingFn).function.toThrow();
smartexpect.expect(throwingFn).function.toThrow(Error);
// Function that doesn't throw
const nonThrowingFn = () => 'safe';
smartexpect.expect(nonThrowingFn).not.toThrow();
smartexpect.expect(nonThrowingFn).not.function.toThrow();
});
tap.test('date assertions', async () => {
@ -182,9 +185,9 @@ tap.test('date assertions', async () => {
const past = new Date(Date.now() - 10000);
const future = new Date(Date.now() + 10000);
smartexpect.expect(now).toBeDate();
smartexpect.expect(now).toBeAfterDate(past);
smartexpect.expect(now).toBeBeforeDate(future);
smartexpect.expect(now).date.toBeDate();
smartexpect.expect(now).date.toBeAfterDate(past);
smartexpect.expect(now).date.toBeBeforeDate(future);
});
tap.test('custom assertions', async () => {
@ -192,7 +195,7 @@ tap.test('custom assertions', async () => {
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').toHaveLength(4);
smartexpect.expect('test').setFailMessage('Custom fail message for assertion').string.toHaveLength(4);
});
tap.test('logging and debugging', async () => {
@ -212,7 +215,7 @@ tap.test('logging and debugging', async () => {
.property('level2')
.log()
.property('value')
.toEqual('nested value');
.object.toEqual('nested value');
});
export default tap.start();

View File

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

View File

@ -1,12 +1,43 @@
import { Assertion } from './smartexpect.classes.assertion.js';
// import type { TMatcher } from './smartexpect.classes.assertion.js'; // unused
export const expect = (baseArg: any) => {
const assertion = new Assertion(baseArg, 'sync');
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>;
export function expect<T>(value: T): Assertion<T>;
export function expect<T>(value: any): Assertion<T> {
const isThenable = value != null && typeof (value as any).then === 'function';
const mode: 'sync' | 'async' = isThenable ? 'async' : 'sync';
return new Assertion<T>(value, mode);
}
/**
* Register custom matchers.
*/
export namespace expect {
export const extend = Assertion.extend;
}
/**
* @deprecated Use `expect(...)` with `.resolves` or `.rejects` instead.
*/
/**
* @deprecated Use `expect(...)` with `.resolves` or `.rejects` instead.
*/
/**
* @deprecated Use `expect(...)` with `.resolves` or `.rejects` instead.
*/
export const expectAsync = (baseArg: any) => {
const assertion = new Assertion(baseArg, 'async');
return assertion;
// eslint-disable-next-line no-console
console.warn('[DEPRECATED] expectAsync() is deprecated. Use expect(...).resolves / .rejects');
return new Assertion<any>(baseArg, 'async');
};

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

@ -0,0 +1,72 @@
import { Assertion } from '../smartexpect.classes.assertion.js';
import * as plugins from '../plugins.js';
/**
* Namespace for array-specific matchers
*/
export class ArrayMatchers<T> {
constructor(private assertion: Assertion<T[]>) {}
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)),
`Expected array to contain equal to ${JSON.stringify(item)}`
);
}
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`
);
}
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}`
);
}
}

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

@ -0,0 +1,36 @@
import { Assertion } from '../smartexpect.classes.assertion.js';
/**
* Namespace for boolean-specific matchers
*/
export class BooleanMatchers {
constructor(private assertion: Assertion<boolean>) {}
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`
);
}
}

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

@ -0,0 +1,29 @@
import { Assertion } from '../smartexpect.classes.assertion.js';
/**
* Namespace for date-specific matchers
*/
export class DateMatchers {
constructor(private assertion: Assertion<Date>) {}
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()}`
);
}
}

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

@ -0,0 +1,29 @@
import { Assertion } from '../smartexpect.classes.assertion.js';
/**
* Namespace for function-specific matchers
*/
export class FunctionMatchers {
constructor(private assertion: Assertion<Function>) {}
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}` : ''}`
);
}
}

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';

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

@ -0,0 +1,56 @@
import { Assertion } from '../smartexpect.classes.assertion.js';
/**
* Namespace for number-specific matchers
*/
export class NumberMatchers {
constructor(private assertion: Assertion<number>) {}
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}`
);
}
}

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

@ -0,0 +1,89 @@
import { Assertion } from '../smartexpect.classes.assertion.js';
import * as plugins from '../plugins.js';
/**
* Namespace for object-specific matchers
*/
export class ObjectMatchers<T extends object> {
constructor(private assertion: Assertion<T>) {}
toEqual(expected: any) {
return this.assertion.customAssertion(
(v) => plugins.fastDeepEqual(v, expected),
`Expected objects to be deeply equal to ${JSON.stringify(expected)}`
);
}
toMatchObject(expected: object) {
return this.assertion.customAssertion(
(v) => {
for (const key of Object.keys(expected)) {
if (!plugins.fastDeepEqual((v as any)[key], (expected as any)[key])) {
return false;
}
}
return true;
},
`Expected object to match properties ${JSON.stringify(expected)}`
);
}
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;
if (!(property in obj)) {
return false;
}
if (arguments.length === 2) {
return plugins.fastDeepEqual(obj[property], 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`
);
}
}

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

@ -0,0 +1,50 @@
import { Assertion } from '../smartexpect.classes.assertion.js';
/**
* Namespace for string-specific matchers
*/
export class StringMatchers {
constructor(private assertion: Assertion<string>) {}
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}`
);
}
}

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

@ -0,0 +1,43 @@
import { Assertion } from '../smartexpect.classes.assertion.js';
/**
* Namespace for type-based matchers
*/
export class TypeMatchers {
constructor(private assertion: Assertion<any>) {}
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 };

View File

@ -1,14 +1,36 @@
import * as plugins from './smartexpect.plugins.js';
import * as plugins from './plugins.js';
import {
StringMatchers,
ArrayMatchers,
NumberMatchers,
BooleanMatchers,
ObjectMatchers,
FunctionMatchers,
DateMatchers,
TypeMatchers,
} from './namespaces/index.js';
/**
* Definition of a custom matcher function.
* Should return an object with `pass` and optional `message`.
*/
import type { TMatcher, TExecutionType } from './types.js';
export type TExecutionType = 'sync' | 'async';
export class Assertion {
/**
* Core assertion class. Generic over the current value type T.
*/
export class Assertion<T = unknown> {
executionMode: TExecutionType;
baseReference: any;
propertyDrillDown: Array<string | number> = [];
private notSetting = false;
private timeoutSetting = 0;
/** Registry of user-defined custom matchers */
private static customMatchers: Record<string, TMatcher> = {};
/** Flag for Promise rejection assertions */
private isRejects = false;
/** Flag for Promise resolution assertions (default for async) */
private isResolves = false;
private failMessage: string;
private successMessage: string;
@ -16,6 +38,32 @@ export class Assertion {
this.baseReference = baseReferenceArg;
this.executionMode = executionModeArg;
}
/**
* Register custom matchers to be available on all assertions.
* @param matchers An object whose keys are matcher names and values are matcher functions.
*/
public static extend(matchers: Record<string, TMatcher>): void {
for (const [name, fn] of Object.entries(matchers)) {
if ((Assertion.prototype as any)[name]) {
throw new Error(`Cannot extend. Matcher '${name}' already exists on Assertion.`);
}
// store in registry
Assertion.customMatchers[name] = fn;
// add method to prototype
(Assertion.prototype as any)[name] = function (...args: any[]) {
return this.runCheck(() => {
const received = this.getObjectToTestReference();
const result = fn(received, ...args);
const pass = result.pass;
const msg = result.message;
if (!pass) {
const message = typeof msg === 'function' ? msg() : msg;
throw new Error(message || `Custom matcher '${name}' failed`);
}
});
};
}
}
private getObjectToTestReference() {
let returnObjectToTestReference = this.baseReference;
@ -88,8 +136,39 @@ export class Assertion {
this.notSetting = true;
return this;
}
/**
* Assert that a Promise resolves.
*/
public get resolves(): this {
this.isResolves = true;
this.isRejects = false;
this.executionMode = 'async';
return this;
}
/**
* Assert that a Promise rejects.
*/
public get rejects(): this {
this.isRejects = true;
this.isResolves = false;
this.executionMode = 'async';
return this;
}
/**
* @deprecated use `.withTimeout(ms)` instead for clarity
* Set a timeout (in ms) for async assertions (Promise must settle before timeout).
*/
public timeout(millisArg: number) {
// eslint-disable-next-line no-console
console.warn('[DEPRECATED] .timeout() is deprecated. Use .withTimeout(ms)');
this.timeoutSetting = millisArg;
return this;
}
/**
* Set a timeout (in ms) for async assertions (Promise must settle before timeout).
*/
public withTimeout(millisArg: number) {
this.timeoutSetting = millisArg;
return this;
}
@ -123,579 +202,52 @@ export class Assertion {
if (this.executionMode === 'async') {
const done = plugins.smartpromise.defer();
if (!(this.baseReference instanceof Promise)) {
const isThenable = this.baseReference && typeof (this.baseReference as any).then === 'function';
if (!isThenable) {
done.reject(new Error(`Expected a Promise but received: ${this.formatValue(this.baseReference)}`));
} else {
if (this.timeoutSetting) {
plugins.smartdelay.delayFor(this.timeoutSetting).then(() => {
if (done.status === 'pending') {
done.reject(new Error(`Promise timed out after ${this.timeoutSetting}ms`));
}
});
}
this.baseReference.then((promiseResultArg: any) => {
this.baseReference = promiseResultArg;
done.resolve(runDirectOrNegated(checkFunction));
return done.promise;
}
if (this.timeoutSetting) {
plugins.smartdelay.delayFor(this.timeoutSetting).then(() => {
if (done.status === 'pending') {
done.reject(new Error(`Promise timed out after ${this.timeoutSetting}ms`));
}
});
}
if (this.isRejects) {
(this.baseReference as Promise<any>).then(
(res: any) => {
done.reject(new Error(`Expected Promise to reject but it resolved with ${this.formatValue(res)}`));
},
(err: any) => {
this.baseReference = err;
try {
const ret = runDirectOrNegated(checkFunction);
done.resolve(ret);
} catch (e: any) {
done.reject(e);
}
}
);
} else {
(this.baseReference as Promise<any>).then(
(res: any) => {
this.baseReference = res;
try {
const ret = runDirectOrNegated(checkFunction);
done.resolve(ret);
} catch (e: any) {
done.reject(e);
}
},
(err: any) => {
done.reject(err);
}
);
}
return done.promise;
} else {
return runDirectOrNegated(checkFunction);
}
}
public toBeDefined() {
return this.runCheck(() => {
if (this.getObjectToTestReference() === undefined) {
throw new Error(
this.createErrorMessage('Expected value{path} to be defined, but got undefined')
);
}
});
}
public toBeTypeofString() {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
if (typeof value !== 'string') {
throw new Error(
this.createErrorMessage(`Expected value{path} to be of type string, but got ${typeof value}`)
);
}
});
}
public toBeTypeofNumber() {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
if (typeof value !== 'number') {
throw new Error(
this.createErrorMessage(`Expected value{path} to be of type number, but got ${typeof value}`)
);
}
});
}
public toBeTypeofBoolean() {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
if (typeof value !== 'boolean') {
throw new Error(
this.createErrorMessage(`Expected value{path} to be of type boolean, but got ${typeof value}`)
);
}
});
}
public toBeTypeOf(expectedType: string) {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
const actualType = typeof value;
if (actualType !== expectedType) {
throw new Error(
this.createErrorMessage(`Expected value{path} to be of type ${expectedType}, but got ${actualType}`)
);
}
});
}
public toEqual(comparisonObject: any) {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
const result = plugins.fastDeepEqual(value, comparisonObject);
if (!result) {
throw new Error(
this.createErrorMessage(`Expected value{path} to equal ${this.formatValue(comparisonObject)}`)
);
}
});
}
public toMatch(comparisonObject: RegExp) {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
const result = comparisonObject.test(value);
if (!result) {
throw new Error(
this.createErrorMessage(`Expected value{path} to match regex ${comparisonObject}`)
);
}
});
}
public toBeTrue() {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
const result = typeof value === 'boolean' && value === true;
if (!result) {
throw new Error(
this.createErrorMessage(`Expected value{path} to be true, but got ${this.formatValue(value)}`)
);
}
});
}
public toBeFalse() {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
const result = typeof value === 'boolean' && value === false;
if (!result) {
throw new Error(
this.createErrorMessage(`Expected value{path} to be false, but got ${this.formatValue(value)}`)
);
}
});
}
public toBeInstanceOf(classArg: any) {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
const result = value instanceof classArg;
if (!result) {
throw new Error(
this.createErrorMessage(`Expected value{path} to be an instance of ${classArg.name || 'provided class'}`)
);
}
});
}
public toHaveProperty(propertyArg: string, equalsArg?: any) {
return this.runCheck(() => {
const obj = this.getObjectToTestReference();
if (!obj || !(propertyArg in obj)) {
throw new Error(
this.createErrorMessage(`Expected value{path} to have property '${propertyArg}'`)
);
}
if (equalsArg !== undefined) {
if (obj[propertyArg] !== equalsArg) {
throw new Error(
this.createErrorMessage(
`Expected property '${propertyArg}' of value{path} to equal ${this.formatValue(equalsArg)}, but got ${this.formatValue(obj[propertyArg])}`
)
);
}
}
});
}
public toHaveDeepProperty(properties: string[]) {
return this.runCheck(() => {
let obj = this.getObjectToTestReference();
let currentPath = '';
for (const property of properties) {
if (currentPath) {
currentPath += `.${property}`;
} else {
currentPath = property;
}
if (!obj || !(property in obj)) {
throw new Error(
this.createErrorMessage(`Expected value{path} to have property at path '${currentPath}'`)
);
}
obj = obj[property];
}
});
}
public toBeGreaterThan(numberArg: number) {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
const result = value > numberArg;
if (!result) {
throw new Error(
this.createErrorMessage(`Expected value{path} to be greater than ${numberArg}, but got ${this.formatValue(value)}`)
);
}
});
}
public toBeLessThan(numberArg: number) {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
const result = value < numberArg;
if (!result) {
throw new Error(
this.createErrorMessage(`Expected value{path} to be less than ${numberArg}, but got ${this.formatValue(value)}`)
);
}
});
}
public toBeNull() {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
const result = value === null;
if (!result) {
throw new Error(
this.createErrorMessage(`Expected value{path} to be null, but got ${this.formatValue(value)}`)
);
}
});
}
public toBeUndefined() {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
const result = value === undefined;
if (!result) {
throw new Error(
this.createErrorMessage(`Expected value{path} to be undefined, but got ${this.formatValue(value)}`)
);
}
});
}
public toBeNullOrUndefined() {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
const result = value === null || value === undefined;
if (!result) {
throw new Error(
this.createErrorMessage(`Expected value{path} to be null or undefined, but got ${this.formatValue(value)}`)
);
}
});
}
// Array checks
public toContain(itemArg: any) {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
const result = Array.isArray(value) && value.includes(itemArg);
if (!result) {
throw new Error(
this.createErrorMessage(`Expected array{path} to contain ${this.formatValue(itemArg)}`)
);
}
});
}
public toBeEmptyArray() {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
if (!Array.isArray(value)) {
throw new Error(
this.createErrorMessage(`Expected value{path} to be an array, but got ${typeof value}`)
);
}
if (value.length !== 0) {
throw new Error(
this.createErrorMessage(`Expected array{path} to be empty, but it has ${value.length} elements`)
);
}
});
}
public toContainAll(values: any[]) {
return this.runCheck(() => {
const arr = this.getObjectToTestReference();
if (!Array.isArray(arr)) {
throw new Error(
this.createErrorMessage(`Expected value{path} to be an array, but got ${typeof arr}`)
);
}
const missing = values.filter(v => !arr.includes(v));
if (missing.length > 0) {
throw new Error(
this.createErrorMessage(`Expected array{path} to contain all values ${this.formatValue(values)}, but missing: ${this.formatValue(missing)}`)
);
}
});
}
public toExclude(value: any) {
return this.runCheck(() => {
const arr = this.getObjectToTestReference();
if (!Array.isArray(arr)) {
throw new Error(
this.createErrorMessage(`Expected value{path} to be an array, but got ${typeof arr}`)
);
}
if (arr.includes(value)) {
throw new Error(
this.createErrorMessage(`Expected array{path} to exclude ${this.formatValue(value)}, but it was found`)
);
}
});
}
public toStartWith(itemArg: any) {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
const result = typeof value === 'string' && value.startsWith(itemArg);
if (!result) {
throw new Error(
this.createErrorMessage(`Expected string{path} to start with "${itemArg}", but got "${value}"`)
);
}
});
}
public toEndWith(itemArg: any) {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
const result = typeof value === 'string' && value.endsWith(itemArg);
if (!result) {
throw new Error(
this.createErrorMessage(`Expected string{path} to end with "${itemArg}", but got "${value}"`)
);
}
});
}
public toBeOneOf(values: any[]) {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
const result = values.includes(value);
if (!result) {
throw new Error(
this.createErrorMessage(`Expected value{path} to be one of ${this.formatValue(values)}, but got ${this.formatValue(value)}`)
);
}
});
}
public toHaveLength(length: number) {
return this.runCheck(() => {
const obj = this.getObjectToTestReference();
if (typeof obj.length !== 'number') {
throw new Error(
this.createErrorMessage(`Expected value{path} to have a length property, but it doesn't`)
);
}
if (obj.length !== length) {
throw new Error(
this.createErrorMessage(`Expected value{path} to have length ${length}, but got length ${obj.length}`)
);
}
});
}
public toBeCloseTo(value: number, precision = 2) {
return this.runCheck(() => {
const actual = this.getObjectToTestReference();
const difference = Math.abs(actual - value);
const epsilon = Math.pow(10, -precision) / 2;
if (difference > epsilon) {
throw new Error(
this.createErrorMessage(`Expected value{path} to be close to ${value} (within ${epsilon}), but the difference was ${difference}`)
);
}
});
}
public toThrow(expectedError?: any) {
return this.runCheck(() => {
const fn = this.getObjectToTestReference();
if (typeof fn !== 'function') {
throw new Error(
this.createErrorMessage(`Expected value{path} to be a function, but got ${typeof fn}`)
);
}
let thrown = false;
let error: any;
try {
fn();
} catch (e) {
thrown = true;
error = e;
if (expectedError && !(e instanceof expectedError)) {
throw new Error(
this.createErrorMessage(`Expected function{path} to throw ${expectedError.name}, but it threw ${e.constructor.name}`)
);
}
}
if (!thrown) {
throw new Error(
this.createErrorMessage(`Expected function{path} to throw, but it didn't throw any error`)
);
}
});
}
public toBeTruthy() {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
if (!value) {
throw new Error(
this.createErrorMessage(`Expected value{path} to be truthy, but got ${this.formatValue(value)}`)
);
}
});
}
public toBeFalsy() {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
if (value) {
throw new Error(
this.createErrorMessage(`Expected value{path} to be falsy, but got ${this.formatValue(value)}`)
);
}
});
}
public toBeGreaterThanOrEqual(numberArg: number) {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
if (value < numberArg) {
throw new Error(
this.createErrorMessage(`Expected value{path} to be greater than or equal to ${numberArg}, but got ${value}`)
);
}
});
}
public toBeLessThanOrEqual(numberArg: number) {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
if (value > numberArg) {
throw new Error(
this.createErrorMessage(`Expected value{path} to be less than or equal to ${numberArg}, but got ${value}`)
);
}
});
}
public toMatchObject(objectArg: object) {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
const matchResult = plugins.fastDeepEqual(value, objectArg);
if (!matchResult) {
throw new Error(
this.createErrorMessage(`Expected value{path} to match ${this.formatValue(objectArg)}`)
);
}
});
}
public toContainEqual(value: any) {
return this.runCheck(() => {
const arr = this.getObjectToTestReference();
if (!Array.isArray(arr)) {
throw new Error(
this.createErrorMessage(`Expected value{path} to be an array, but got ${typeof arr}`)
);
}
const found = arr.some((item: any) => plugins.fastDeepEqual(item, value));
if (!found) {
throw new Error(
this.createErrorMessage(`Expected array{path} to contain an item equal to ${this.formatValue(value)}`)
);
}
});
}
public toBeArray() {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
if (!Array.isArray(value)) {
throw new Error(
this.createErrorMessage(`Expected value{path} to be an array, but got ${typeof value}`)
);
}
});
}
public toInclude(substring: string) {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
if (typeof value !== 'string') {
throw new Error(
this.createErrorMessage(`Expected value{path} to be a string, but got ${typeof value}`)
);
}
if (!value.includes(substring)) {
throw new Error(
this.createErrorMessage(`Expected string{path} to include "${substring}", but it doesn't`)
);
}
});
}
public toHaveLengthGreaterThan(length: number) {
return this.runCheck(() => {
const obj = this.getObjectToTestReference();
if (typeof obj.length !== 'number') {
throw new Error(
this.createErrorMessage(`Expected value{path} to have a length property, but it doesn't`)
);
}
if (obj.length <= length) {
throw new Error(
this.createErrorMessage(`Expected value{path} to have length greater than ${length}, but got length ${obj.length}`)
);
}
});
}
public toHaveLengthLessThan(length: number) {
return this.runCheck(() => {
const obj = this.getObjectToTestReference();
if (typeof obj.length !== 'number') {
throw new Error(
this.createErrorMessage(`Expected value{path} to have a length property, but it doesn't`)
);
}
if (obj.length >= length) {
throw new Error(
this.createErrorMessage(`Expected value{path} to have length less than ${length}, but got length ${obj.length}`)
);
}
});
}
public toBeDate() {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
if (!(value instanceof Date)) {
throw new Error(
this.createErrorMessage(`Expected value{path} to be a Date, but got ${value.constructor ? value.constructor.name : typeof value}`)
);
}
});
}
public toBeBeforeDate(date: Date) {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
if (!(value instanceof Date)) {
throw new Error(
this.createErrorMessage(`Expected value{path} to be a Date, but got ${value.constructor ? value.constructor.name : typeof value}`)
);
}
if (value >= date) {
throw new Error(
this.createErrorMessage(`Expected date{path} to be before ${date.toISOString()}, but got ${value.toISOString()}`)
);
}
});
}
public toBeAfterDate(date: Date) {
return this.runCheck(() => {
const value = this.getObjectToTestReference();
if (!(value instanceof Date)) {
throw new Error(
this.createErrorMessage(`Expected value{path} to be a Date, but got ${value.constructor ? value.constructor.name : typeof value}`)
);
}
if (value <= date) {
throw new Error(
this.createErrorMessage(`Expected date{path} to be after ${date.toISOString()}, but got ${value.toISOString()}`)
);
}
});
return runDirectOrNegated(checkFunction);
}
public customAssertion(
@ -711,20 +263,23 @@ export class Assertion {
}
/**
* Drill into a property
* Drill into a property of an object.
* @param propertyName Name of the property to navigate into.
* @returns Assertion of the property type.
*/
public property(propertyNameArg: string) {
this.propertyDrillDown.push(propertyNameArg);
return this;
public property<K extends keyof NonNullable<T>>(propertyName: K): Assertion<NonNullable<T>[K]> {
this.propertyDrillDown.push(propertyName as string);
return this as unknown as Assertion<NonNullable<T>[K]>;
}
/**
* Drill into an array index
* Drill into an array element by index.
* @param index Index of the array item.
* @returns Assertion of the element type.
*/
public arrayItem(indexArg: number) {
// Save the number (instead of "[index]")
this.propertyDrillDown.push(indexArg);
return this;
public arrayItem(index: number): Assertion<T extends Array<infer U> ? U : unknown> {
this.propertyDrillDown.push(index);
return this as unknown as Assertion<T extends Array<infer U> ? U : unknown>;
}
public log() {
@ -733,4 +288,80 @@ export class Assertion {
console.log(`Path: ${this.formatDrillDown() || '(root)'}`);
return this;
}
// Direct (flat) matcher aliases
public toEqual(expected: any) {
return this.customAssertion(
(v) => plugins.fastDeepEqual(v, expected),
`Expected value to equal ${JSON.stringify(expected)}`
);
}
public toBeTrue() { return this.boolean.toBeTrue(); }
public toBeFalse() { return this.boolean.toBeFalse(); }
public toBeTruthy() { return this.boolean.toBeTruthy(); }
public toBeFalsy() { return this.boolean.toBeFalsy(); }
public toThrow(expectedError?: any) { return this.function.toThrow(expectedError); }
public toBeGreaterThan(value: number) { return this.number.toBeGreaterThan(value); }
public toBeLessThan(value: number) { return this.number.toBeLessThan(value); }
public toBeGreaterThanOrEqual(value: number) { return this.number.toBeGreaterThanOrEqual(value); }
public toBeLessThanOrEqual(value: number) { return this.number.toBeLessThanOrEqual(value); }
public toBeCloseTo(value: number, precision?: number) { return this.number.toBeCloseTo(value, precision); }
public toBeArray() { return this.array.toBeArray(); }
public toContain(item: any) { return this.array.toContain(item); }
public toContainEqual(item: any) { return this.array.toContainEqual(item); }
public toContainAll(items: any[]) { return this.array.toContainAll(items); }
public toExclude(item: any) { return this.array.toExclude(item); }
public toBeEmptyArray() { return this.array.toBeEmptyArray(); }
public toStartWith(prefix: string) { return this.string.toStartWith(prefix); }
public toEndWith(suffix: string) { return this.string.toEndWith(suffix); }
public toInclude(substring: string) { return this.string.toInclude(substring); }
public toMatch(regex: RegExp) { return this.string.toMatch(regex); }
public toBeOneOf(values: any[]) { return this.string.toBeOneOf(values as string[]); }
public toHaveProperty(property: string, value?: any) { return this.object.toHaveProperty(property, value); }
public toMatchObject(expected: object) { return this.object.toMatchObject(expected); }
public toBeInstanceOf(constructor: any) { return this.object.toBeInstanceOf(constructor); }
public toHaveDeepProperty(path: string[]) { return this.object.toHaveDeepProperty(path); }
public toBeNull() { return this.object.toBeNull(); }
public toBeUndefined() { return this.object.toBeUndefined(); }
public toBeNullOrUndefined() { return this.object.toBeNullOrUndefined(); }
public toBeDate() { return this.date.toBeDate(); }
public toBeBeforeDate(date: Date) { return this.date.toBeBeforeDate(date); }
public toBeAfterDate(date: Date) { return this.date.toBeAfterDate(date); }
public toBeTypeofString() { return this.type.toBeTypeofString(); }
public toBeTypeofNumber() { return this.type.toBeTypeofNumber(); }
public toBeTypeofBoolean() { return this.type.toBeTypeofBoolean(); }
public toBeTypeOf(typeName: string) { return this.type.toBeTypeOf(typeName); }
public toBeDefined() { return this.type.toBeDefined(); }
// Namespaced matcher accessors
/** String-specific matchers */
public get string() {
return new StringMatchers(this as Assertion<string>);
}
/** Array-specific matchers */
public get array() {
return new ArrayMatchers<any>(this as Assertion<any[]>);
}
/** Number-specific matchers */
public get number() {
return new NumberMatchers(this as Assertion<number>);
}
/** Boolean-specific matchers */
public get boolean() {
return new BooleanMatchers(this as Assertion<boolean>);
}
/** Object-specific matchers */
public get object() {
return new ObjectMatchers<any>(this as Assertion<object>);
}
/** Function-specific matchers */
public get function() {
return new FunctionMatchers(this as Assertion<Function>);
}
/** Date-specific matchers */
public get date() {
return new DateMatchers(this as Assertion<Date>);
}
/** Type-based matchers */
public get type() {
return new TypeMatchers(this as Assertion<any>);
}
}

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) };