BREAKING CHANGE(docs): Update documentation and examples to unify async and sync assertions, add custom matcher guides, and update package configuration
This commit is contained in:
@ -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();
|
||||
|
Reference in New Issue
Block a user