Compare commits
No commits in common. "master" and "v1.5.0" have entirely different histories.
@ -119,6 +119,6 @@ jobs:
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
pnpm install -g @git.zone/tsdoc
|
||||
pnpm install -g @gitzone/tsdoc
|
||||
npmci command tsdoc
|
||||
continue-on-error: true
|
||||
|
14
changelog.md
14
changelog.md
@ -1,19 +1,5 @@
|
||||
# Changelog
|
||||
|
||||
## 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
|
||||
|
||||
|
12
package.json
12
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@push.rocks/smartexpect",
|
||||
"version": "1.6.1",
|
||||
"version": "1.5.0",
|
||||
"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 tsfolders)",
|
||||
"build": "(tsbuild --web)",
|
||||
"buildDocs": "tsdoc"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@git.zone/tsbuild": "^2.2.1",
|
||||
"@git.zone/tsbundle": "^2.2.5",
|
||||
"@git.zone/tsrun": "^1.3.3",
|
||||
"@git.zone/tstest": "^1.0.96",
|
||||
"@gitzone/tsbuild": "^2.1.66",
|
||||
"@gitzone/tsbundle": "^2.0.8",
|
||||
"@gitzone/tsrun": "^1.2.44",
|
||||
"@gitzone/tstest": "^1.0.77",
|
||||
"@push.rocks/tapbundle": "^5.5.6",
|
||||
"@types/node": "^22.13.9"
|
||||
},
|
||||
|
5021
pnpm-lock.yaml
generated
5021
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
208
readme.md
208
readme.md
@ -1,5 +1,5 @@
|
||||
# @push.rocks/smartexpect
|
||||
Manage expectations in code with precise, readable assertions
|
||||
manage expectations in code
|
||||
|
||||
## Install
|
||||
|
||||
@ -30,16 +30,14 @@ You can employ `expect` to create synchronous assertions:
|
||||
```typescript
|
||||
import { expect } from '@push.rocks/smartexpect';
|
||||
|
||||
// Type assertions
|
||||
// String type assertion
|
||||
expect('hello').toBeTypeofString();
|
||||
expect(42).toBeTypeofNumber();
|
||||
expect(true).toBeTypeofBoolean();
|
||||
expect(() => {}).toBeTypeOf('function');
|
||||
expect({}).toBeTypeOf('object');
|
||||
|
||||
// Negated assertions
|
||||
// Negated String type assertion
|
||||
expect(1).not.toBeTypeofString();
|
||||
expect('string').not.toBeTypeofNumber();
|
||||
|
||||
// Boolean type assertion
|
||||
expect(true).toBeTypeofBoolean();
|
||||
|
||||
// Equality assertion
|
||||
expect('hithere').toEqual('hithere');
|
||||
@ -63,184 +61,68 @@ const asyncStringFetcher = async (): Promise<string> => {
|
||||
};
|
||||
|
||||
const asyncTest = async () => {
|
||||
// Add a timeout to prevent hanging tests
|
||||
await expectAsync(asyncStringFetcher()).timeout(5000).toBeTypeofString();
|
||||
await expectAsync(asyncStringFetcher()).toBeTypeofString();
|
||||
await expectAsync(asyncStringFetcher()).toEqual('async string');
|
||||
};
|
||||
|
||||
asyncTest();
|
||||
```
|
||||
|
||||
### Navigating Complex Objects
|
||||
### Advanced Usage
|
||||
|
||||
You can navigate complex objects using the `property()` and `arrayItem()` methods:
|
||||
- **Properties and Deep Properties:** Assert the existence of properties and their values.
|
||||
|
||||
```typescript
|
||||
const testObject = { level1: { level2: 'value' } };
|
||||
|
||||
```typescript
|
||||
const complexObject = {
|
||||
users: [
|
||||
{ id: 1, name: 'Alice', permissions: { admin: true } },
|
||||
{ id: 2, name: 'Bob', permissions: { admin: false } }
|
||||
]
|
||||
};
|
||||
// Property existence
|
||||
expect(testObject).toHaveProperty('level1');
|
||||
|
||||
// Deep Property existence
|
||||
expect(testObject).toHaveDeepProperty(['level1', 'level2']);
|
||||
```
|
||||
|
||||
// Navigate to a nested property
|
||||
expect(complexObject)
|
||||
.property('users')
|
||||
.arrayItem(0)
|
||||
.property('name')
|
||||
.toEqual('Alice');
|
||||
- **Conditions and Comparisons:** Allow more intricate assertions like greater than, less than, or matching specific conditions.
|
||||
|
||||
// Check nested permission
|
||||
expect(complexObject)
|
||||
.property('users')
|
||||
.arrayItem(0)
|
||||
.property('permissions')
|
||||
.property('admin')
|
||||
.toBeTrue();
|
||||
```
|
||||
```typescript
|
||||
// Greater Than
|
||||
expect(5).toBeGreaterThan(3);
|
||||
|
||||
### Advanced Assertions
|
||||
// Less Than
|
||||
expect(3).toBeLessThan(5);
|
||||
|
||||
#### Properties and Deep Properties
|
||||
// Custom conditions
|
||||
expect(7).customAssertion(value => value > 5, 'Value is not greater than 5');
|
||||
```
|
||||
|
||||
Assert the existence of properties and their values:
|
||||
- **Arrays and Objects:** Work seamlessly with arrays and objects, checking for containment, length, or specific values.
|
||||
|
||||
```typescript
|
||||
const testObject = { level1: { level2: 'value' } };
|
||||
```typescript
|
||||
const testArray = [1, 2, 3];
|
||||
|
||||
// Property existence
|
||||
expect(testObject).toHaveProperty('level1');
|
||||
// Containment
|
||||
expect(testArray).toContain(2);
|
||||
|
||||
// Property with specific value
|
||||
expect(testObject).toHaveProperty('level1.level2', 'value');
|
||||
// Array length
|
||||
expect(testArray).toHaveLength(3);
|
||||
|
||||
// Deep Property existence
|
||||
expect(testObject).toHaveDeepProperty(['level1', 'level2']);
|
||||
```
|
||||
// Object matching
|
||||
expect({ name: 'Test', value: 123 }).toMatchObject({ name: 'Test' });
|
||||
```
|
||||
|
||||
#### Conditions and Comparisons
|
||||
### Handling Promises and Async Operations
|
||||
|
||||
Perform more intricate assertions:
|
||||
`@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.
|
||||
|
||||
```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
|
||||
### Best Practices
|
||||
|
||||
// Truthiness checks
|
||||
expect(true).toBeTrue();
|
||||
expect(false).toBeFalse();
|
||||
expect('non-empty').toBeTruthy();
|
||||
expect(0).toBeFalsy();
|
||||
- **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.
|
||||
|
||||
// Null/Undefined checks
|
||||
expect(null).toBeNull();
|
||||
expect(undefined).toBeUndefined();
|
||||
expect(null).toBeNullOrUndefined();
|
||||
- **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.
|
||||
|
||||
// Custom conditions
|
||||
expect(7).customAssertion(value => value % 2 === 1, 'Value is not odd');
|
||||
```
|
||||
- **Maintainability:** Group related assertions together to improve test maintainability. This makes it easier to update tests as your codebase evolves.
|
||||
|
||||
#### 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);
|
||||
```
|
||||
|
||||
## 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.
|
||||
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.
|
||||
|
||||
## License and Legal Information
|
||||
|
||||
@ -259,4 +141,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.
|
||||
|
@ -1,218 +0,0 @@
|
||||
import { tap } from '@push.rocks/tapbundle';
|
||||
import * as smartexpect from '../ts/index.js';
|
||||
|
||||
tap.test('basic type assertions', async () => {
|
||||
// String type checks
|
||||
smartexpect.expect('hello').toBeTypeofString();
|
||||
smartexpect.expect(1).not.toBeTypeofString();
|
||||
|
||||
// Boolean type checks
|
||||
smartexpect.expect(true).toBeTypeofBoolean();
|
||||
smartexpect.expect(false).toBeTypeofBoolean();
|
||||
smartexpect.expect(1).not.toBeTypeofBoolean();
|
||||
|
||||
// Number type checks
|
||||
smartexpect.expect(42).toBeTypeofNumber();
|
||||
smartexpect.expect(true).not.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');
|
||||
});
|
||||
|
||||
tap.test('async tests', async (toolsArg) => {
|
||||
const deferred = toolsArg.defer();
|
||||
toolsArg.delayFor(1000).then(() => {
|
||||
deferred.resolve('hello');
|
||||
});
|
||||
await smartexpect.expectAsync(deferred.promise).timeout(2000).toBeTypeofString();
|
||||
await smartexpect.expectAsync(deferred.promise).not.toBeTypeofBoolean();
|
||||
|
||||
// Test async timeout handling
|
||||
const longOperation = toolsArg.defer();
|
||||
toolsArg.delayFor(3000).then(() => {
|
||||
longOperation.resolve('completed');
|
||||
});
|
||||
try {
|
||||
await smartexpect.expectAsync(longOperation.promise).timeout(1000).toBeDefined();
|
||||
throw new Error('Should have timed out');
|
||||
} catch (err) {
|
||||
// Expected timeout error
|
||||
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');
|
||||
|
||||
// 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);
|
||||
|
||||
// RegExp matching
|
||||
smartexpect.expect('hithere').toMatch(/hi/);
|
||||
smartexpect.expect('hithere').toMatch(/^hithere$/);
|
||||
smartexpect.expect('hithere').not.toMatch(/ho/);
|
||||
|
||||
// String inclusion
|
||||
smartexpect.expect('hithere').toInclude('hit');
|
||||
smartexpect.expect('hithere').not.toInclude('missing');
|
||||
|
||||
// String start/end
|
||||
smartexpect.expect('hithere').toStartWith('hi');
|
||||
smartexpect.expect('hithere').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).toHaveProperty('topLevel');
|
||||
smartexpect.expect(testObject).toHaveProperty('topLevel', 'hello');
|
||||
smartexpect.expect(testObject).not.toHaveProperty('missing');
|
||||
|
||||
// Drill-down property navigation
|
||||
smartexpect.expect(testObject).property('nested').toHaveProperty('prop', 42);
|
||||
smartexpect
|
||||
.expect(testObject)
|
||||
.property('nested')
|
||||
.property('deeplyNested')
|
||||
.property('array')
|
||||
.toBeArray();
|
||||
|
||||
// Deep property checks
|
||||
smartexpect.expect(testObject).toHaveDeepProperty(['nested', 'deeplyNested', 'array']);
|
||||
|
||||
// Array item navigation
|
||||
smartexpect
|
||||
.expect(testObject)
|
||||
.property('nested')
|
||||
.property('deeplyNested')
|
||||
.property('array')
|
||||
.arrayItem(0)
|
||||
.toEqual(1);
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
// Approximate equality
|
||||
smartexpect.expect(0.1 + 0.2).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).toBeArray();
|
||||
smartexpect.expect(testArray).toHaveLength(4);
|
||||
|
||||
// Content checks
|
||||
smartexpect.expect(testArray).toContain('two');
|
||||
smartexpect.expect(testArray).toContain(obj1);
|
||||
smartexpect.expect(testArray).not.toContain(obj2);
|
||||
|
||||
// Array with equal items (not same reference)
|
||||
smartexpect.expect([{ a: 1 }, { b: 2 }]).toContainEqual({ a: 1 });
|
||||
|
||||
// Multiple values
|
||||
smartexpect.expect(testArray).toContainAll([1, 'two']);
|
||||
smartexpect.expect(testArray).toExclude('missing');
|
||||
|
||||
// Empty array
|
||||
smartexpect.expect([]).toBeEmptyArray();
|
||||
|
||||
// Length comparisons
|
||||
smartexpect.expect(testArray).toHaveLengthGreaterThan(3);
|
||||
smartexpect.expect(testArray).toHaveLengthLessThan(5);
|
||||
});
|
||||
|
||||
tap.test('boolean assertions', async () => {
|
||||
// True/False
|
||||
smartexpect.expect(true).toBeTrue();
|
||||
smartexpect.expect(false).toBeFalse();
|
||||
|
||||
// Truthy/Falsy
|
||||
smartexpect.expect('something').toBeTruthy();
|
||||
smartexpect.expect(0).toBeFalsy();
|
||||
|
||||
// Null/Undefined
|
||||
smartexpect.expect(null).toBeNull();
|
||||
smartexpect.expect(undefined).toBeUndefined();
|
||||
smartexpect.expect(null).toBeNullOrUndefined();
|
||||
smartexpect.expect(undefined).toBeNullOrUndefined();
|
||||
});
|
||||
|
||||
tap.test('function assertions', async () => {
|
||||
// Function that throws
|
||||
const throwingFn = () => {
|
||||
throw new Error('test error');
|
||||
};
|
||||
smartexpect.expect(throwingFn).toThrow();
|
||||
smartexpect.expect(throwingFn).toThrow(Error);
|
||||
|
||||
// Function that doesn't throw
|
||||
const nonThrowingFn = () => 'safe';
|
||||
smartexpect.expect(nonThrowingFn).not.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).toBeDate();
|
||||
smartexpect.expect(now).toBeAfterDate(past);
|
||||
smartexpect.expect(now).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').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')
|
||||
.toEqual('nested value');
|
||||
});
|
||||
|
||||
export default tap.start();
|
52
test/test.ts
Normal file
52
test/test.ts
Normal file
@ -0,0 +1,52 @@
|
||||
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();
|
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartexpect',
|
||||
version: '1.6.1',
|
||||
version: '1.5.0',
|
||||
description: 'A testing library to manage expectations in code, offering both synchronous and asynchronous assertion methods.'
|
||||
}
|
||||
|
@ -33,57 +33,6 @@ export class Assertion {
|
||||
return returnObjectToTestReference;
|
||||
}
|
||||
|
||||
private formatDrillDown(): string {
|
||||
if (!this.propertyDrillDown || this.propertyDrillDown.length === 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const path = this.propertyDrillDown.map(prop => {
|
||||
if (typeof prop === 'number') {
|
||||
return `[${prop}]`;
|
||||
} else {
|
||||
return `.${prop}`;
|
||||
}
|
||||
}).join('');
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
private formatValue(value: any): string {
|
||||
if (value === null) {
|
||||
return 'null';
|
||||
} else if (value === undefined) {
|
||||
return 'undefined';
|
||||
} else if (typeof value === 'object') {
|
||||
try {
|
||||
return JSON.stringify(value);
|
||||
} catch (e) {
|
||||
return `[Object ${value.constructor.name}]`;
|
||||
}
|
||||
} else if (typeof value === 'function') {
|
||||
return `[Function${value.name ? ': ' + value.name : ''}]`;
|
||||
} else if (typeof value === 'string') {
|
||||
return `"${value}"`;
|
||||
} else {
|
||||
return String(value);
|
||||
}
|
||||
}
|
||||
|
||||
private createErrorMessage(message: string): string {
|
||||
if (this.failMessage) {
|
||||
return this.failMessage;
|
||||
}
|
||||
|
||||
const testValue = this.getObjectToTestReference();
|
||||
const formattedValue = this.formatValue(testValue);
|
||||
const drillDown = this.formatDrillDown();
|
||||
|
||||
// Replace placeholders in the message
|
||||
return message
|
||||
.replace('{value}', formattedValue)
|
||||
.replace('{path}', drillDown || '');
|
||||
}
|
||||
|
||||
public get not() {
|
||||
this.notSetting = true;
|
||||
return this;
|
||||
@ -116,7 +65,7 @@ export class Assertion {
|
||||
isOk = true;
|
||||
}
|
||||
if (!isOk) {
|
||||
throw new Error(this.failMessage || 'Negated assertion failed');
|
||||
throw new Error(this.failMessage || 'Negated assertion is not ok!');
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -124,12 +73,12 @@ export class Assertion {
|
||||
if (this.executionMode === 'async') {
|
||||
const done = plugins.smartpromise.defer();
|
||||
if (!(this.baseReference instanceof Promise)) {
|
||||
done.reject(new Error(`Expected a Promise but received: ${this.formatValue(this.baseReference)}`));
|
||||
done.reject(new Error(`${this.baseReference} is not of type promise.`));
|
||||
} 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`));
|
||||
done.reject(new Error(`${this.baseReference} timed out at ${this.timeoutSetting}!`));
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -148,7 +97,8 @@ export class Assertion {
|
||||
return this.runCheck(() => {
|
||||
if (this.getObjectToTestReference() === undefined) {
|
||||
throw new Error(
|
||||
this.createErrorMessage('Expected value{path} to be defined, but got undefined')
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not defined`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -156,10 +106,12 @@ export class Assertion {
|
||||
|
||||
public toBeTypeofString() {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (typeof value !== 'string') {
|
||||
if (typeof this.getObjectToTestReference() !== 'string') {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to be of type string, but got ${typeof value}`)
|
||||
this.failMessage ||
|
||||
`Assertion failed: ${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} is not of type string, but typeof ${typeof this.baseReference}`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -167,10 +119,12 @@ export class Assertion {
|
||||
|
||||
public toBeTypeofNumber() {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (typeof value !== 'number') {
|
||||
if (typeof this.getObjectToTestReference() !== 'number') {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to be of type number, but got ${typeof value}`)
|
||||
this.failMessage ||
|
||||
`Assertion failed: ${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} is not of type string, but typeof ${typeof this.baseReference}`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -178,22 +132,12 @@ export class Assertion {
|
||||
|
||||
public toBeTypeofBoolean() {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (typeof value !== 'boolean') {
|
||||
if (typeof this.getObjectToTestReference() !== '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}`)
|
||||
this.failMessage ||
|
||||
`Assertion failed: ${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} is not of type string, but typeof ${typeof this.baseReference}`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -201,11 +145,11 @@ export class Assertion {
|
||||
|
||||
public toEqual(comparisonObject: any) {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = plugins.fastDeepEqual(value, comparisonObject);
|
||||
const result = plugins.fastDeepEqual(this.getObjectToTestReference(), comparisonObject);
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to equal ${this.formatValue(comparisonObject)}`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} does not equal ${comparisonObject}`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -213,11 +157,13 @@ export class Assertion {
|
||||
|
||||
public toMatch(comparisonObject: RegExp) {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = comparisonObject.test(value);
|
||||
const result = comparisonObject.test(this.getObjectToTestReference());
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to match regex ${comparisonObject}`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not match regex ${comparisonObject}`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -225,11 +171,13 @@ export class Assertion {
|
||||
|
||||
public toBeTrue() {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = typeof value === 'boolean' && value === true;
|
||||
const result =
|
||||
typeof this.getObjectToTestReference() === 'boolean' &&
|
||||
this.getObjectToTestReference() === true;
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to be true, but got ${this.formatValue(value)}`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not true or not of type boolean`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -237,11 +185,13 @@ export class Assertion {
|
||||
|
||||
public toBeFalse() {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = typeof value === 'boolean' && value === false;
|
||||
const result =
|
||||
typeof this.getObjectToTestReference() === 'boolean' &&
|
||||
this.getObjectToTestReference() === false;
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to be false, but got ${this.formatValue(value)}`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not false or not of type boolean`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -249,11 +199,25 @@ export class Assertion {
|
||||
|
||||
public toBeInstanceOf(classArg: any) {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = value instanceof classArg;
|
||||
const result = this.getObjectToTestReference() instanceof classArg;
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to be an instance of ${classArg.name || 'provided class'}`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not an instance of ${classArg}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeTypeOf(expectedType: string) {
|
||||
return this.runCheck(() => {
|
||||
const actualType = typeof this.getObjectToTestReference();
|
||||
if (actualType !== expectedType) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`Assertion failed: ${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} is not of type ${expectedType}, but typeof ${actualType}`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -264,15 +228,19 @@ export class Assertion {
|
||||
const obj = this.getObjectToTestReference();
|
||||
if (!obj || !(propertyArg in obj)) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to have property '${propertyArg}'`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not 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])}`
|
||||
)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does have property ${propertyArg}, but it does not equal ${equalsArg}`
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -293,7 +261,8 @@ export class Assertion {
|
||||
|
||||
if (!obj || !(property in obj)) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to have property at path '${currentPath}'`)
|
||||
this.failMessage ||
|
||||
`Missing property at path "${currentPath}" in ${this.baseReference}`
|
||||
);
|
||||
}
|
||||
obj = obj[property];
|
||||
@ -303,11 +272,11 @@ export class Assertion {
|
||||
|
||||
public toBeGreaterThan(numberArg: number) {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = value > numberArg;
|
||||
const result = this.getObjectToTestReference() > numberArg;
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to be greater than ${numberArg}, but got ${this.formatValue(value)}`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not greater than ${numberArg}`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -315,11 +284,11 @@ export class Assertion {
|
||||
|
||||
public toBeLessThan(numberArg: number) {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = value < numberArg;
|
||||
const result = this.getObjectToTestReference() < numberArg;
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to be less than ${numberArg}, but got ${this.formatValue(value)}`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not less than ${numberArg}`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -327,11 +296,11 @@ export class Assertion {
|
||||
|
||||
public toBeNull() {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = value === null;
|
||||
const result = this.getObjectToTestReference() === null;
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to be null, but got ${this.formatValue(value)}`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not null`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -339,11 +308,11 @@ export class Assertion {
|
||||
|
||||
public toBeUndefined() {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = value === undefined;
|
||||
const result = this.getObjectToTestReference() === undefined;
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to be undefined, but got ${this.formatValue(value)}`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not undefined`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -351,11 +320,12 @@ export class Assertion {
|
||||
|
||||
public toBeNullOrUndefined() {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = value === null || value === undefined;
|
||||
const testRef = this.getObjectToTestReference();
|
||||
const result = testRef === null || testRef === undefined;
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to be null or undefined, but got ${this.formatValue(value)}`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not null or undefined`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -365,11 +335,12 @@ export class Assertion {
|
||||
|
||||
public toContain(itemArg: any) {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = Array.isArray(value) && value.includes(itemArg);
|
||||
const testRef = this.getObjectToTestReference();
|
||||
const result = Array.isArray(testRef) && testRef.includes(itemArg);
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected array{path} to contain ${this.formatValue(itemArg)}`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} does not contain ${itemArg}`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -377,15 +348,11 @@ export class Assertion {
|
||||
|
||||
public toBeEmptyArray() {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (!Array.isArray(value)) {
|
||||
const arrayRef = this.getObjectToTestReference();
|
||||
if (!Array.isArray(arrayRef) || arrayRef.length !== 0) {
|
||||
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`)
|
||||
this.failMessage ||
|
||||
`Expected ${this.baseReference} to be an empty array, but it was not.`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -393,33 +360,41 @@ export class Assertion {
|
||||
|
||||
public toContainAll(values: any[]) {
|
||||
return this.runCheck(() => {
|
||||
const arr = this.getObjectToTestReference();
|
||||
if (!Array.isArray(arr)) {
|
||||
const arrayRef = this.getObjectToTestReference();
|
||||
if (!Array.isArray(arrayRef)) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to be an array, but got ${typeof arr}`)
|
||||
this.failMessage ||
|
||||
`Expected ${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} to be an array.`
|
||||
);
|
||||
}
|
||||
|
||||
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)}`)
|
||||
);
|
||||
for (const value of values) {
|
||||
if (!arrayRef.includes(value)) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`Expected ${this.baseReference} to include value "${value}", but it did not.`
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toExclude(value: any) {
|
||||
return this.runCheck(() => {
|
||||
const arr = this.getObjectToTestReference();
|
||||
if (!Array.isArray(arr)) {
|
||||
const arrayRef = this.getObjectToTestReference();
|
||||
if (!Array.isArray(arrayRef)) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to be an array, but got ${typeof arr}`)
|
||||
this.failMessage ||
|
||||
`Expected ${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} to be an array.`
|
||||
);
|
||||
}
|
||||
if (arr.includes(value)) {
|
||||
if (arrayRef.includes(value)) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected array{path} to exclude ${this.formatValue(value)}, but it was found`)
|
||||
this.failMessage ||
|
||||
`Expected ${this.baseReference} to exclude value "${value}", but it included it.`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -427,11 +402,14 @@ export class Assertion {
|
||||
|
||||
public toStartWith(itemArg: any) {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = typeof value === 'string' && value.startsWith(itemArg);
|
||||
const testObject = this.getObjectToTestReference();
|
||||
const result = typeof testObject === 'string' && testObject.startsWith(itemArg);
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected string{path} to start with "${itemArg}", but got "${value}"`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not start with ${itemArg}`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -439,11 +417,14 @@ export class Assertion {
|
||||
|
||||
public toEndWith(itemArg: any) {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = typeof value === 'string' && value.endsWith(itemArg);
|
||||
const testObject = this.getObjectToTestReference();
|
||||
const result = typeof testObject === 'string' && testObject.endsWith(itemArg);
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected string{path} to end with "${itemArg}", but got "${value}"`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not end with ${itemArg}`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -451,11 +432,11 @@ export class Assertion {
|
||||
|
||||
public toBeOneOf(values: any[]) {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = values.includes(value);
|
||||
const result = values.includes(this.getObjectToTestReference());
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to be one of ${this.formatValue(values)}, but got ${this.formatValue(value)}`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not one of ${values}`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -464,14 +445,12 @@ export class Assertion {
|
||||
public toHaveLength(length: number) {
|
||||
return this.runCheck(() => {
|
||||
const obj = this.getObjectToTestReference();
|
||||
if (typeof obj.length !== 'number') {
|
||||
if (typeof obj.length !== 'number' || obj.length !== length) {
|
||||
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}`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not have a length of ${length}`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -479,12 +458,13 @@ export class Assertion {
|
||||
|
||||
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) {
|
||||
const difference = Math.abs(this.getObjectToTestReference() - value);
|
||||
if (difference > Math.pow(10, -precision) / 2) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to be close to ${value} (within ${epsilon}), but the difference was ${difference}`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} is not close to ${value} up to ${precision} decimal places`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -492,42 +472,30 @@ export class Assertion {
|
||||
|
||||
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();
|
||||
this.getObjectToTestReference()();
|
||||
} 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}`)
|
||||
this.failMessage ||
|
||||
`Expected function to throw ${expectedError.name}, but it threw ${e.name}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (!thrown) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected function{path} to throw, but it didn't throw any error`)
|
||||
);
|
||||
throw new Error(`Expected function to throw, but it didn't.`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeTruthy() {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (!value) {
|
||||
if (!this.getObjectToTestReference()) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to be truthy, but got ${this.formatValue(value)}`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not truthy`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -535,10 +503,10 @@ export class Assertion {
|
||||
|
||||
public toBeFalsy() {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (value) {
|
||||
if (this.getObjectToTestReference()) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to be falsy, but got ${this.formatValue(value)}`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not falsy`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -546,10 +514,12 @@ export class Assertion {
|
||||
|
||||
public toBeGreaterThanOrEqual(numberArg: number) {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (value < numberArg) {
|
||||
if (this.getObjectToTestReference() < numberArg) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to be greater than or equal to ${numberArg}, but got ${value}`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} is not greater than or equal to ${numberArg}`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -557,10 +527,12 @@ export class Assertion {
|
||||
|
||||
public toBeLessThanOrEqual(numberArg: number) {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (value > numberArg) {
|
||||
if (this.getObjectToTestReference() > numberArg) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to be less than or equal to ${numberArg}, but got ${value}`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} is not less than or equal to ${numberArg}`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -568,11 +540,14 @@ export class Assertion {
|
||||
|
||||
public toMatchObject(objectArg: object) {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
const matchResult = plugins.fastDeepEqual(value, objectArg);
|
||||
// Implement a partial object match if needed.
|
||||
const matchResult = plugins.fastDeepEqual(this.getObjectToTestReference(), objectArg);
|
||||
if (!matchResult) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to match ${this.formatValue(objectArg)}`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not match the object ${JSON.stringify(objectArg)}`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -583,14 +558,16 @@ export class Assertion {
|
||||
const arr = this.getObjectToTestReference();
|
||||
if (!Array.isArray(arr)) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to be an array, but got ${typeof arr}`)
|
||||
this.failMessage || `Expected ${this.baseReference} to be an array but it is not.`
|
||||
);
|
||||
}
|
||||
|
||||
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)}`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not contain the value ${JSON.stringify(value)}`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -598,10 +575,10 @@ export class Assertion {
|
||||
|
||||
public toBeArray() {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (!Array.isArray(value)) {
|
||||
if (!Array.isArray(this.getObjectToTestReference())) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to be an array, but got ${typeof value}`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not an array`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -609,15 +586,13 @@ export class Assertion {
|
||||
|
||||
public toInclude(substring: string) {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (typeof value !== 'string') {
|
||||
const testRef = this.getObjectToTestReference();
|
||||
if (typeof testRef !== 'string' || !testRef.includes(substring)) {
|
||||
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`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not include the substring ${substring}`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -626,14 +601,12 @@ export class Assertion {
|
||||
public toHaveLengthGreaterThan(length: number) {
|
||||
return this.runCheck(() => {
|
||||
const obj = this.getObjectToTestReference();
|
||||
if (typeof obj.length !== 'number') {
|
||||
if (typeof obj.length !== 'number' || obj.length <= length) {
|
||||
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}`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not have a length greater than ${length}`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -642,14 +615,12 @@ export class Assertion {
|
||||
public toHaveLengthLessThan(length: number) {
|
||||
return this.runCheck(() => {
|
||||
const obj = this.getObjectToTestReference();
|
||||
if (typeof obj.length !== 'number') {
|
||||
if (typeof obj.length !== 'number' || obj.length >= length) {
|
||||
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}`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not have a length less than ${length}`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -657,10 +628,11 @@ export class Assertion {
|
||||
|
||||
public toBeDate() {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (!(value instanceof Date)) {
|
||||
const testRef = this.getObjectToTestReference();
|
||||
if (!(testRef instanceof Date)) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to be a Date, but got ${value.constructor ? value.constructor.name : typeof value}`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not a date`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -668,15 +640,11 @@ export class Assertion {
|
||||
|
||||
public toBeBeforeDate(date: Date) {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (!(value instanceof Date)) {
|
||||
const testRef = this.getObjectToTestReference();
|
||||
if (!(testRef instanceof Date) || testRef >= 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()}`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not before ${date}`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -684,15 +652,11 @@ export class Assertion {
|
||||
|
||||
public toBeAfterDate(date: Date) {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (!(value instanceof Date)) {
|
||||
const testRef = this.getObjectToTestReference();
|
||||
if (!(testRef instanceof Date) || testRef <= 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()}`)
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not after ${date}`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -703,8 +667,7 @@ export class Assertion {
|
||||
errorMessage: string
|
||||
) {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (!assertionFunction(value)) {
|
||||
if (!assertionFunction(this.getObjectToTestReference())) {
|
||||
throw new Error(this.failMessage || errorMessage);
|
||||
}
|
||||
});
|
||||
@ -728,9 +691,8 @@ export class Assertion {
|
||||
}
|
||||
|
||||
public log() {
|
||||
console.log(`Current value:`);
|
||||
console.log(`this is the object to test:`);
|
||||
console.log(JSON.stringify(this.getObjectToTestReference(), null, 2));
|
||||
console.log(`Path: ${this.formatDrillDown() || '(root)'}`);
|
||||
return this;
|
||||
}
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"experimentalDecorators": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"useDefineForClassFields": false,
|
||||
"target": "ES2022",
|
||||
"module": "NodeNext",
|
||||
|
Loading…
x
Reference in New Issue
Block a user