Compare commits
34 Commits
Author | SHA1 | Date | |
---|---|---|---|
5f5628f647 | |||
e688207d23 | |||
7ae5b4378a | |||
1979d8dd9c | |||
847b106302 | |||
38f0996cfa | |||
6cab20c36d | |||
689afcbd92 | |||
4e9a7b5424 | |||
f83c64e1d4 | |||
04e3c1a9ac | |||
9ef1e5120e | |||
537545766c | |||
56d28d04d6 | |||
b7e098f0a7 | |||
845daccd58 | |||
5917f25a99 | |||
8a8b6cb517 | |||
c3d9b5621c | |||
e5c47a68dc | |||
3cf4a6566b | |||
113e5f48cb | |||
8a2d34e88a | |||
035138a437 | |||
e0a774852e | |||
465e579298 | |||
f5f6e40fff | |||
9d065a54e6 | |||
ee0a797424 | |||
a234069cc8 | |||
534c1e4215 | |||
931a713ae7 | |||
d1be0d8ce2 | |||
3e5121171a |
66
.gitea/workflows/default_nottags.yaml
Normal file
66
.gitea/workflows/default_nottags.yaml
Normal file
@ -0,0 +1,66 @@
|
||||
name: Default (not tags)
|
||||
|
||||
on:
|
||||
push:
|
||||
tags-ignore:
|
||||
- '**'
|
||||
|
||||
env:
|
||||
IMAGE: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@gitea.lossless.digital/${{gitea.repository}}.git
|
||||
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
|
||||
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
|
||||
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
|
||||
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
|
||||
|
||||
jobs:
|
||||
security:
|
||||
runs-on: ubuntu-latest
|
||||
continue-on-error: true
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Install pnpm and npmci
|
||||
run: |
|
||||
pnpm install -g pnpm
|
||||
pnpm install -g @shipzone/npmci
|
||||
|
||||
- name: Run npm prepare
|
||||
run: npmci npm prepare
|
||||
|
||||
- name: Audit production dependencies
|
||||
run: |
|
||||
npmci command npm config set registry https://registry.npmjs.org
|
||||
npmci command pnpm audit --audit-level=high --prod
|
||||
continue-on-error: true
|
||||
|
||||
- name: Audit development dependencies
|
||||
run: |
|
||||
npmci command npm config set registry https://registry.npmjs.org
|
||||
npmci command pnpm audit --audit-level=high --dev
|
||||
continue-on-error: true
|
||||
|
||||
test:
|
||||
if: ${{ always() }}
|
||||
needs: security
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Test stable
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
npmci npm test
|
||||
|
||||
- name: Test build
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
npmci npm build
|
124
.gitea/workflows/default_tags.yaml
Normal file
124
.gitea/workflows/default_tags.yaml
Normal file
@ -0,0 +1,124 @@
|
||||
name: Default (tags)
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- '*'
|
||||
|
||||
env:
|
||||
IMAGE: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@gitea.lossless.digital/${{gitea.repository}}.git
|
||||
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
|
||||
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
|
||||
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
|
||||
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
|
||||
|
||||
jobs:
|
||||
security:
|
||||
runs-on: ubuntu-latest
|
||||
continue-on-error: true
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Prepare
|
||||
run: |
|
||||
pnpm install -g pnpm
|
||||
pnpm install -g @shipzone/npmci
|
||||
npmci npm prepare
|
||||
|
||||
- name: Audit production dependencies
|
||||
run: |
|
||||
npmci command npm config set registry https://registry.npmjs.org
|
||||
npmci command pnpm audit --audit-level=high --prod
|
||||
continue-on-error: true
|
||||
|
||||
- name: Audit development dependencies
|
||||
run: |
|
||||
npmci command npm config set registry https://registry.npmjs.org
|
||||
npmci command pnpm audit --audit-level=high --dev
|
||||
continue-on-error: true
|
||||
|
||||
test:
|
||||
if: ${{ always() }}
|
||||
needs: security
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Prepare
|
||||
run: |
|
||||
pnpm install -g pnpm
|
||||
pnpm install -g @shipzone/npmci
|
||||
npmci npm prepare
|
||||
|
||||
- name: Test stable
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
npmci npm test
|
||||
|
||||
- name: Test build
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
npmci npm build
|
||||
|
||||
release:
|
||||
needs: test
|
||||
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Prepare
|
||||
run: |
|
||||
pnpm install -g pnpm
|
||||
pnpm install -g @shipzone/npmci
|
||||
npmci npm prepare
|
||||
|
||||
- name: Release
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm publish
|
||||
|
||||
metadata:
|
||||
needs: test
|
||||
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
continue-on-error: true
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Prepare
|
||||
run: |
|
||||
pnpm install -g pnpm
|
||||
pnpm install -g @shipzone/npmci
|
||||
npmci npm prepare
|
||||
|
||||
- name: Code quality
|
||||
run: |
|
||||
npmci command npm install -g typescript
|
||||
npmci npm install
|
||||
|
||||
- name: Trigger
|
||||
run: npmci trigger
|
||||
|
||||
- name: Build docs and upload artifacts
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
pnpm install -g @gitzone/tsdoc
|
||||
npmci command tsdoc
|
||||
continue-on-error: true
|
141
.gitlab-ci.yml
141
.gitlab-ci.yml
@ -1,141 +0,0 @@
|
||||
# gitzone ci_default
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
|
||||
cache:
|
||||
paths:
|
||||
- .npmci_cache/
|
||||
key: '$CI_BUILD_STAGE'
|
||||
|
||||
stages:
|
||||
- security
|
||||
- test
|
||||
- release
|
||||
- metadata
|
||||
|
||||
before_script:
|
||||
- npm install -g @shipzone/npmci
|
||||
|
||||
# ====================
|
||||
# security stage
|
||||
# ====================
|
||||
mirror:
|
||||
stage: security
|
||||
script:
|
||||
- npmci git mirror
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
auditProductionDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install --production --ignore-scripts
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command npm audit --audit-level=high --only=prod --production
|
||||
tags:
|
||||
- docker
|
||||
allow_failure: true
|
||||
|
||||
auditDevDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command npm audit --audit-level=high --only=dev
|
||||
tags:
|
||||
- docker
|
||||
allow_failure: true
|
||||
|
||||
# ====================
|
||||
# test stage
|
||||
# ====================
|
||||
|
||||
testStable:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
|
||||
testBuild:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci command npm run build
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
|
||||
release:
|
||||
stage: release
|
||||
script:
|
||||
- npmci node install stable
|
||||
- npmci npm publish
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
# ====================
|
||||
# metadata stage
|
||||
# ====================
|
||||
codequality:
|
||||
stage: metadata
|
||||
allow_failure: true
|
||||
only:
|
||||
- tags
|
||||
script:
|
||||
- npmci command npm install -g tslint typescript
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- priv
|
||||
|
||||
trigger:
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci trigger
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
pages:
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci node install lts
|
||||
- npmci command npm install -g @gitzone/tsdoc
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command tsdoc
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
only:
|
||||
- tags
|
||||
artifacts:
|
||||
expire_in: 1 week
|
||||
paths:
|
||||
- public
|
||||
allow_failure: true
|
73
changelog.md
Normal file
73
changelog.md
Normal file
@ -0,0 +1,73 @@
|
||||
# Changelog
|
||||
|
||||
## 2025-03-04 - 1.5.0 - feat(Assertion)
|
||||
Add toBeTypeOf assertion method
|
||||
|
||||
- Introduced a new assertion method `toBeTypeOf` allowing checks for expected data types.
|
||||
- Updated devDependencies and dependencies to their latest versions.
|
||||
|
||||
## 2024-12-30 - 1.4.0 - feat(Assertion)
|
||||
Add log method to Assertion class
|
||||
|
||||
- Introduced a log method in the Assertion class to output assertion context.
|
||||
|
||||
## 2024-12-30 - 1.3.0 - feat(Assertion)
|
||||
Refactor Assertion class for better error handling and code clarity
|
||||
|
||||
- Improved method runCheck to better handle async and sync execution
|
||||
- Enhanced getObjectToTestReference to handle undefined or null values gracefully
|
||||
- Refactored error message logic for clarity and added more descriptive fail messages
|
||||
- Added arrayItem method for better handling of array index access
|
||||
- Improved structure by integrating consistent error handling in assertion methods
|
||||
|
||||
## 2024-08-24 - 1.2.1 - fix(Assertion)
|
||||
Refactor methods for setting failure and success messages
|
||||
|
||||
- Renamed 'withFailMessage' to 'setFailMessage' for better readability and consistency.
|
||||
- Renamed 'withSuccessMessage' to 'setSuccessMessage' to align with the naming convention.
|
||||
|
||||
## 2024-08-24 - 1.2.0 - feat(assertions)
|
||||
Add custom fail and success messages for assertions
|
||||
|
||||
- Implemented withFailMessage method in Assertion class to customize fail messages
|
||||
- Implemented withSuccessMessage method in Assertion class to customize success messages
|
||||
- Enhanced error messages to use custom fail messages when provided
|
||||
|
||||
## 2024-08-17 - 1.1.0 - feat(assertion)
|
||||
Add toBeDefined assertion method
|
||||
|
||||
- Added the toBeDefined method to the Assertion class for checking if an object is defined
|
||||
|
||||
## 2024-05-29 - 1.0.21 - General Updates
|
||||
General updates and maintenance.
|
||||
|
||||
- Updated description
|
||||
- Updated tsconfig
|
||||
- Updated npmextra.json: githost
|
||||
|
||||
## 2023-08-12 - 1.0.20 to 1.0.21 - General Fixes
|
||||
General fixes and update.
|
||||
|
||||
- Fixed core updates (multiple instances)
|
||||
- 1.0.21 release
|
||||
|
||||
## 2023-07-10 - 1.0.15 - Organization Update
|
||||
- Switched to new org scheme
|
||||
|
||||
## 2023-06-22 - 1.0.14 to 1.0.19 - General Updates
|
||||
General fixes and updates.
|
||||
|
||||
- Fixed core updates (multiple instances)
|
||||
- 1.0.18 to 1.0.16 releases
|
||||
|
||||
## 2022-02-02 - 1.0.8 to 1.0.13 - General Fixes
|
||||
General fixes and update.
|
||||
|
||||
- Fixed core updates (multiple instances)
|
||||
- 1.0.14 release
|
||||
|
||||
## 2022-01-20 - 1.0.1 to 1.0.7 - Initial Releases
|
||||
Initial core updates and fixes.
|
||||
|
||||
- Fixed core updates (multiple instances)
|
||||
- 1.0.7 release
|
@ -2,17 +2,32 @@
|
||||
"gitzone": {
|
||||
"projectType": "npm",
|
||||
"module": {
|
||||
"githost": "gitlab.com",
|
||||
"gitscope": "pushrocks",
|
||||
"githost": "code.foss.global",
|
||||
"gitscope": "push.rocks",
|
||||
"gitrepo": "smartexpect",
|
||||
"description": "manage expectations in code",
|
||||
"npmPackagename": "@pushrocks/smartexpect",
|
||||
"description": "A testing library to manage expectations in code, offering both synchronous and asynchronous assertion methods.",
|
||||
"npmPackagename": "@push.rocks/smartexpect",
|
||||
"license": "MIT",
|
||||
"projectDomain": "push.rocks"
|
||||
"projectDomain": "push.rocks",
|
||||
"keywords": [
|
||||
"testing",
|
||||
"assertions",
|
||||
"code quality",
|
||||
"unit testing",
|
||||
"test driven development",
|
||||
"synchronous testing",
|
||||
"asynchronous testing",
|
||||
"expectations",
|
||||
"validation",
|
||||
"development tools"
|
||||
]
|
||||
}
|
||||
},
|
||||
"npmci": {
|
||||
"npmGlobalTools": [],
|
||||
"npmAccessLevel": "public"
|
||||
},
|
||||
"tsdoc": {
|
||||
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy 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.\n"
|
||||
}
|
||||
}
|
27557
package-lock.json
generated
27557
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
51
package.json
51
package.json
@ -1,28 +1,29 @@
|
||||
{
|
||||
"name": "@pushrocks/smartexpect",
|
||||
"version": "1.0.13",
|
||||
"name": "@push.rocks/smartexpect",
|
||||
"version": "1.5.0",
|
||||
"private": false,
|
||||
"description": "manage expectations in code",
|
||||
"description": "A testing library to manage expectations in code, offering both synchronous and asynchronous assertion methods.",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"type": "module",
|
||||
"author": "Lossless GmbH",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"test": "(tstest test/ --web)",
|
||||
"build": "(tsbuild --web)"
|
||||
"build": "(tsbuild --web)",
|
||||
"buildDocs": "tsdoc"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.25",
|
||||
"@gitzone/tsbundle": "^1.0.89",
|
||||
"@gitzone/tstest": "^1.0.44",
|
||||
"@pushrocks/tapbundle": "^4.0.0",
|
||||
"@types/node": "^17.0.14",
|
||||
"tslint": "^6.1.3",
|
||||
"tslint-config-prettier": "^1.15.0"
|
||||
"@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"
|
||||
},
|
||||
"dependencies": {
|
||||
"@pushrocks/smartdelay": "^2.0.13",
|
||||
"@pushrocks/smartpromise": "^3.1.6",
|
||||
"@push.rocks/smartdelay": "^3.0.5",
|
||||
"@push.rocks/smartpromise": "^4.2.3",
|
||||
"fast-deep-equal": "^3.1.3"
|
||||
},
|
||||
"browserslist": [
|
||||
@ -39,5 +40,27 @@
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
]
|
||||
],
|
||||
"keywords": [
|
||||
"testing",
|
||||
"assertions",
|
||||
"code quality",
|
||||
"unit testing",
|
||||
"test driven development",
|
||||
"synchronous testing",
|
||||
"asynchronous testing",
|
||||
"expectations",
|
||||
"validation",
|
||||
"development tools"
|
||||
],
|
||||
"homepage": "https://code.foss.global/push.rocks/smartexpect",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://code.foss.global/push.rocks/smartexpect.git"
|
||||
},
|
||||
"pnpm": {
|
||||
"onlyBuiltDependencies": [
|
||||
"mongodb-memory-server"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
8958
pnpm-lock.yaml
generated
Normal file
8958
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
1
readme.hints.md
Normal file
1
readme.hints.md
Normal file
@ -0,0 +1 @@
|
||||
|
159
readme.md
159
readme.md
@ -1,39 +1,144 @@
|
||||
# @pushrocks/smartexpect
|
||||
# @push.rocks/smartexpect
|
||||
manage expectations in code
|
||||
|
||||
## Availabililty and Links
|
||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartexpect)
|
||||
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartexpect)
|
||||
* [github.com (source mirror)](https://github.com/pushrocks/smartexpect)
|
||||
* [docs (typedoc)](https://pushrocks.gitlab.io/smartexpect/)
|
||||
## Install
|
||||
|
||||
## Status for master
|
||||
To install `@push.rocks/smartexpect`, use the following command in your terminal:
|
||||
|
||||
Status Category | Status Badge
|
||||
-- | --
|
||||
GitLab Pipelines | [](https://lossless.cloud)
|
||||
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||
npm | [](https://lossless.cloud)
|
||||
Snyk | [](https://lossless.cloud)
|
||||
TypeScript Support | [](https://lossless.cloud)
|
||||
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
Code Style | [](https://lossless.cloud)
|
||||
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||
Platform support | [](https://lossless.cloud) [](https://lossless.cloud)
|
||||
```bash
|
||||
npm install @push.rocks/smartexpect --save
|
||||
```
|
||||
|
||||
This will add `@push.rocks/smartexpect` to your project's dependencies. Make sure you're inside your project directory before running this command.
|
||||
|
||||
## Usage
|
||||
|
||||
Use TypeScript for best in class intellisense
|
||||
`@push.rocks/smartexpect` is a TypeScript library designed to manage expectations in your code effectively, improving testing readability and maintainability. Below are various scenarios showcasing how to use this library effectively across both synchronous and asynchronous code paths.
|
||||
|
||||
## Contribution
|
||||
### Getting Started
|
||||
|
||||
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
||||
First, import `@push.rocks/smartexpect` into your TypeScript file:
|
||||
|
||||
For further information read the linked docs at the top of this readme.
|
||||
```typescript
|
||||
import { expect, expectAsync } from '@push.rocks/smartexpect';
|
||||
```
|
||||
|
||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||
### Synchronous Expectations
|
||||
|
||||
[](https://maintainedby.lossless.com)
|
||||
You can employ `expect` to create synchronous assertions:
|
||||
|
||||
```typescript
|
||||
import { expect } from '@push.rocks/smartexpect';
|
||||
|
||||
// String type assertion
|
||||
expect('hello').toBeTypeofString();
|
||||
|
||||
// Negated String type assertion
|
||||
expect(1).not.toBeTypeofString();
|
||||
|
||||
// Boolean type assertion
|
||||
expect(true).toBeTypeofBoolean();
|
||||
|
||||
// Equality assertion
|
||||
expect('hithere').toEqual('hithere');
|
||||
|
||||
// Deep equality assertion
|
||||
expect({ key: 'value' }).toEqual({ key: 'value' });
|
||||
|
||||
// Regular expression matching
|
||||
expect('hithere').toMatch(/hi/);
|
||||
```
|
||||
|
||||
### Asynchronous Expectations
|
||||
|
||||
For asynchronous operations, use `expectAsync` to return a promise:
|
||||
|
||||
```typescript
|
||||
import { expectAsync } from '@push.rocks/smartexpect';
|
||||
|
||||
const asyncStringFetcher = async (): Promise<string> => {
|
||||
return 'async string';
|
||||
};
|
||||
|
||||
const asyncTest = async () => {
|
||||
await expectAsync(asyncStringFetcher()).toBeTypeofString();
|
||||
await expectAsync(asyncStringFetcher()).toEqual('async string');
|
||||
};
|
||||
|
||||
asyncTest();
|
||||
```
|
||||
|
||||
### Advanced Usage
|
||||
|
||||
- **Properties and Deep Properties:** Assert the existence of properties and their values.
|
||||
|
||||
```typescript
|
||||
const testObject = { level1: { level2: 'value' } };
|
||||
|
||||
// Property existence
|
||||
expect(testObject).toHaveProperty('level1');
|
||||
|
||||
// Deep Property existence
|
||||
expect(testObject).toHaveDeepProperty(['level1', 'level2']);
|
||||
```
|
||||
|
||||
- **Conditions and Comparisons:** Allow more intricate assertions like greater than, less than, or matching specific conditions.
|
||||
|
||||
```typescript
|
||||
// Greater Than
|
||||
expect(5).toBeGreaterThan(3);
|
||||
|
||||
// Less Than
|
||||
expect(3).toBeLessThan(5);
|
||||
|
||||
// Custom conditions
|
||||
expect(7).customAssertion(value => value > 5, 'Value is not greater than 5');
|
||||
```
|
||||
|
||||
- **Arrays and Objects:** Work seamlessly with arrays and objects, checking for containment, length, or specific values.
|
||||
|
||||
```typescript
|
||||
const testArray = [1, 2, 3];
|
||||
|
||||
// Containment
|
||||
expect(testArray).toContain(2);
|
||||
|
||||
// Array length
|
||||
expect(testArray).toHaveLength(3);
|
||||
|
||||
// Object matching
|
||||
expect({ name: 'Test', value: 123 }).toMatchObject({ name: 'Test' });
|
||||
```
|
||||
|
||||
### Handling Promises and Async Operations
|
||||
|
||||
`@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.
|
||||
|
||||
### Best Practices
|
||||
|
||||
- **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.
|
||||
|
||||
- **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.
|
||||
|
||||
- **Maintainability:** Group related assertions together to improve test maintainability. This makes it easier to update tests as your codebase evolves.
|
||||
|
||||
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
|
||||
|
||||
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
|
||||
|
||||
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
|
||||
|
||||
### Trademarks
|
||||
|
||||
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
|
||||
|
||||
### Company Information
|
||||
|
||||
Task Venture Capital GmbH
|
||||
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.
|
||||
|
19
test/test.ts
19
test/test.ts
@ -1,5 +1,5 @@
|
||||
import { tap } from '@pushrocks/tapbundle';
|
||||
import * as smartexpect from '../ts/index';
|
||||
import { tap } from '@push.rocks/tapbundle';
|
||||
import * as smartexpect from '../ts/index.js';
|
||||
|
||||
tap.test('sync tests', async () => {
|
||||
smartexpect.expect('hello').toBeTypeofString();
|
||||
@ -20,11 +20,16 @@ tap.test('async tests', async (toolsArg) => {
|
||||
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'
|
||||
aprop: 'hello',
|
||||
};
|
||||
|
||||
smartexpect.expect(testObject).toHaveProperty('aprop');
|
||||
@ -34,14 +39,14 @@ tap.test('should correctly state property presence', async () => {
|
||||
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'
|
||||
socool: 'yes',
|
||||
};
|
||||
const testArray = [hello];
|
||||
smartexpect.expect(testArray).toContain(hello);
|
||||
})
|
||||
});
|
||||
|
||||
tap.start();
|
||||
|
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @push.rocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartexpect',
|
||||
version: '1.5.0',
|
||||
description: 'A testing library to manage expectations in code, offering both synchronous and asynchronous assertion methods.'
|
||||
}
|
@ -1,13 +1,12 @@
|
||||
import * as plugins from './smartexpect.plugins';
|
||||
|
||||
import { Assertion } from './smartexpect.classes.assertion';
|
||||
import { Assertion } from './smartexpect.classes.assertion.js';
|
||||
|
||||
export const expect = (baseArg: any) => {
|
||||
const assertion = new Assertion(baseArg, 'sync');
|
||||
return assertion;
|
||||
}
|
||||
};
|
||||
|
||||
export const expectAsync = (baseArg: any) => {
|
||||
const assertion = new Assertion(baseArg, 'async');
|
||||
return assertion;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -1,13 +1,17 @@
|
||||
import * as plugins from './smartexpect.plugins';
|
||||
import * as plugins from './smartexpect.plugins.js';
|
||||
|
||||
export type TExecutionType = 'sync' | 'async';
|
||||
|
||||
export class Assertion {
|
||||
executionMode: TExecutionType;
|
||||
baseReference: any;
|
||||
propertyDrillDown: string[] = [];
|
||||
propertyDrillDown: Array<string | number> = [];
|
||||
|
||||
private notSetting = false;
|
||||
private timeoutSetting = 0;
|
||||
private failMessage: string;
|
||||
private successMessage: string;
|
||||
|
||||
constructor(baseReferenceArg: any, executionModeArg: TExecutionType) {
|
||||
this.baseReference = baseReferenceArg;
|
||||
this.executionMode = executionModeArg;
|
||||
@ -16,6 +20,14 @@ export class Assertion {
|
||||
private getObjectToTestReference() {
|
||||
let returnObjectToTestReference = this.baseReference;
|
||||
for (const property of this.propertyDrillDown) {
|
||||
if (returnObjectToTestReference == null) {
|
||||
// if it's null or undefined, stop
|
||||
break;
|
||||
}
|
||||
|
||||
// We just directly access with bracket notation.
|
||||
// If property is a string, it's like obj["someProp"];
|
||||
// If property is a number, it's like obj[0].
|
||||
returnObjectToTestReference = returnObjectToTestReference[property];
|
||||
}
|
||||
return returnObjectToTestReference;
|
||||
@ -31,6 +43,16 @@ export class Assertion {
|
||||
return this;
|
||||
}
|
||||
|
||||
public setFailMessage(failMessageArg: string) {
|
||||
this.failMessage = failMessageArg;
|
||||
return this;
|
||||
}
|
||||
|
||||
public setSuccessMessage(successMessageArg: string) {
|
||||
this.successMessage = successMessageArg;
|
||||
return this;
|
||||
}
|
||||
|
||||
private runCheck(checkFunction: () => any) {
|
||||
const runDirectOrNegated = (checkFunction: () => any) => {
|
||||
if (!this.notSetting) {
|
||||
@ -43,7 +65,7 @@ export class Assertion {
|
||||
isOk = true;
|
||||
}
|
||||
if (!isOk) {
|
||||
throw new Error('Negated assertion is not ok!');
|
||||
throw new Error(this.failMessage || 'Negated assertion is not ok!');
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -60,7 +82,7 @@ export class Assertion {
|
||||
}
|
||||
});
|
||||
}
|
||||
this.baseReference.then((promiseResultArg) => {
|
||||
this.baseReference.then((promiseResultArg: any) => {
|
||||
this.baseReference = promiseResultArg;
|
||||
done.resolve(runDirectOrNegated(checkFunction));
|
||||
});
|
||||
@ -71,12 +93,25 @@ export class Assertion {
|
||||
}
|
||||
}
|
||||
|
||||
public toBeDefined() {
|
||||
return this.runCheck(() => {
|
||||
if (this.getObjectToTestReference() === undefined) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not defined`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeTypeofString() {
|
||||
return this.runCheck(() => {
|
||||
if (typeof this.getObjectToTestReference() !== 'string') {
|
||||
throw new Error(
|
||||
`Assertion failed: ${this.baseReference} with drill down ${this.propertyDrillDown} is not of type string, but typeof ${typeof this
|
||||
.baseReference}`
|
||||
this.failMessage ||
|
||||
`Assertion failed: ${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} is not of type string, but typeof ${typeof this.baseReference}`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -86,8 +121,10 @@ export class Assertion {
|
||||
return this.runCheck(() => {
|
||||
if (typeof this.getObjectToTestReference() !== 'number') {
|
||||
throw new Error(
|
||||
`Assertion failed: ${this.baseReference} with drill down ${this.propertyDrillDown} is not of type string, but typeof ${typeof this
|
||||
.baseReference}`
|
||||
this.failMessage ||
|
||||
`Assertion failed: ${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} is not of type string, but typeof ${typeof this.baseReference}`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -97,8 +134,10 @@ export class Assertion {
|
||||
return this.runCheck(() => {
|
||||
if (typeof this.getObjectToTestReference() !== 'boolean') {
|
||||
throw new Error(
|
||||
`Assertion failed: ${this.baseReference} with drill down ${this.propertyDrillDown} is not of type string, but typeof ${typeof this
|
||||
.baseReference}`
|
||||
this.failMessage ||
|
||||
`Assertion failed: ${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} is not of type string, but typeof ${typeof this.baseReference}`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -108,25 +147,52 @@ export class Assertion {
|
||||
return this.runCheck(() => {
|
||||
const result = plugins.fastDeepEqual(this.getObjectToTestReference(), comparisonObject);
|
||||
if (!result) {
|
||||
throw new Error(`${this.baseReference} with drill down ${this.propertyDrillDown} does not equal ${comparisonObject}`);
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} does not equal ${comparisonObject}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toMatch(comparisonObject: RegExp) {
|
||||
return this.runCheck(() => {
|
||||
const result = comparisonObject.test(this.getObjectToTestReference());
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not match regex ${comparisonObject}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeTrue() {
|
||||
return this.runCheck(() => {
|
||||
const result = typeof this.getObjectToTestReference() === 'boolean' && this.getObjectToTestReference() === true;
|
||||
const result =
|
||||
typeof this.getObjectToTestReference() === 'boolean' &&
|
||||
this.getObjectToTestReference() === true;
|
||||
if (!result) {
|
||||
throw new Error(`${this.baseReference} with drill down ${this.propertyDrillDown} is not true or not of type boolean`);
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not true or not of type boolean`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeFalse() {
|
||||
return this.runCheck(() => {
|
||||
const result = typeof this.getObjectToTestReference() === 'boolean' && this.getObjectToTestReference() === false;
|
||||
const result =
|
||||
typeof this.getObjectToTestReference() === 'boolean' &&
|
||||
this.getObjectToTestReference() === false;
|
||||
if (!result) {
|
||||
throw new Error(`${this.baseReference} with drill down ${this.propertyDrillDown} is not false or not of type boolean`);
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not false or not of type boolean`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -135,16 +201,71 @@ export class Assertion {
|
||||
return this.runCheck(() => {
|
||||
const result = this.getObjectToTestReference() instanceof classArg;
|
||||
if (!result) {
|
||||
throw new Error(`${this.baseReference} with drill down ${this.propertyDrillDown} is not an instance of ${classArg}`);
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not an instance of ${classArg}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toHaveProperty(propertyArg: string) {
|
||||
public toBeTypeOf(expectedType: string) {
|
||||
return this.runCheck(() => {
|
||||
const result = !!this.getObjectToTestReference()[propertyArg];
|
||||
if (!result) {
|
||||
throw new Error(`${this.baseReference} with drill down ${this.propertyDrillDown} does not have property ${propertyArg}`);
|
||||
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}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toHaveProperty(propertyArg: string, equalsArg?: any) {
|
||||
return this.runCheck(() => {
|
||||
const obj = this.getObjectToTestReference();
|
||||
if (!obj || !(propertyArg in obj)) {
|
||||
throw new Error(
|
||||
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.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does have property ${propertyArg}, but it does not equal ${equalsArg}`
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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.failMessage ||
|
||||
`Missing property at path "${currentPath}" in ${this.baseReference}`
|
||||
);
|
||||
}
|
||||
obj = obj[property];
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -153,7 +274,10 @@ export class Assertion {
|
||||
return this.runCheck(() => {
|
||||
const result = this.getObjectToTestReference() > numberArg;
|
||||
if (!result) {
|
||||
throw new Error(`${this.baseReference} with drill down ${this.propertyDrillDown} is not greater than ${numberArg}`);
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not greater than ${numberArg}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -162,7 +286,10 @@ export class Assertion {
|
||||
return this.runCheck(() => {
|
||||
const result = this.getObjectToTestReference() < numberArg;
|
||||
if (!result) {
|
||||
throw new Error(`${this.baseReference} with drill down ${this.propertyDrillDown} is not less than ${numberArg}`);
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not less than ${numberArg}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -171,7 +298,10 @@ export class Assertion {
|
||||
return this.runCheck(() => {
|
||||
const result = this.getObjectToTestReference() === null;
|
||||
if (!result) {
|
||||
throw new Error(`${this.baseReference} with drill down ${this.propertyDrillDown} is not null`);
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not null`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -180,27 +310,92 @@ export class Assertion {
|
||||
return this.runCheck(() => {
|
||||
const result = this.getObjectToTestReference() === undefined;
|
||||
if (!result) {
|
||||
throw new Error(`${this.baseReference} with drill down ${this.propertyDrillDown} is not undefined`);
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not undefined`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeNullOrUndefined() {
|
||||
return this.runCheck(() => {
|
||||
const result = this.getObjectToTestReference() === null || this.getObjectToTestReference() === undefined;
|
||||
const testRef = this.getObjectToTestReference();
|
||||
const result = testRef === null || testRef === undefined;
|
||||
if (!result) {
|
||||
throw new Error(`${this.baseReference} with drill down ${this.propertyDrillDown} is not null or undefined`);
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not null or undefined`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Array checks
|
||||
|
||||
public toContain(itemArg: any) {
|
||||
return this.runCheck(() => {
|
||||
const result =
|
||||
this.getObjectToTestReference() instanceof Array &&
|
||||
this.getObjectToTestReference().includes(itemArg);
|
||||
const testRef = this.getObjectToTestReference();
|
||||
const result = Array.isArray(testRef) && testRef.includes(itemArg);
|
||||
if (!result) {
|
||||
throw new Error(`${this.baseReference} with drill down ${this.propertyDrillDown} is not contain ${itemArg}`);
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} does not contain ${itemArg}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeEmptyArray() {
|
||||
return this.runCheck(() => {
|
||||
const arrayRef = this.getObjectToTestReference();
|
||||
if (!Array.isArray(arrayRef) || arrayRef.length !== 0) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`Expected ${this.baseReference} to be an empty array, but it was not.`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toContainAll(values: any[]) {
|
||||
return this.runCheck(() => {
|
||||
const arrayRef = this.getObjectToTestReference();
|
||||
if (!Array.isArray(arrayRef)) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`Expected ${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} to be an array.`
|
||||
);
|
||||
}
|
||||
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 arrayRef = this.getObjectToTestReference();
|
||||
if (!Array.isArray(arrayRef)) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`Expected ${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} to be an array.`
|
||||
);
|
||||
}
|
||||
if (arrayRef.includes(value)) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`Expected ${this.baseReference} to exclude value "${value}", but it included it.`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -208,11 +403,14 @@ export class Assertion {
|
||||
public toStartWith(itemArg: any) {
|
||||
return this.runCheck(() => {
|
||||
const testObject = this.getObjectToTestReference();
|
||||
const result =
|
||||
typeof testObject === 'string' &&
|
||||
testObject.startsWith(itemArg);
|
||||
const result = typeof testObject === 'string' && testObject.startsWith(itemArg);
|
||||
if (!result) {
|
||||
throw new Error(`${this.baseReference} with drill down ${this.propertyDrillDown} is not contain ${itemArg}`);
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not start with ${itemArg}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -220,17 +418,281 @@ export class Assertion {
|
||||
public toEndWith(itemArg: any) {
|
||||
return this.runCheck(() => {
|
||||
const testObject = this.getObjectToTestReference();
|
||||
const result =
|
||||
typeof testObject === 'string' &&
|
||||
testObject.endsWith(itemArg);
|
||||
const result = typeof testObject === 'string' && testObject.endsWith(itemArg);
|
||||
if (!result) {
|
||||
throw new Error(`${this.baseReference} with drill down ${this.propertyDrillDown} is not contain ${itemArg}`);
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not end with ${itemArg}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeOneOf(values: any[]) {
|
||||
return this.runCheck(() => {
|
||||
const result = values.includes(this.getObjectToTestReference());
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not one of ${values}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toHaveLength(length: number) {
|
||||
return this.runCheck(() => {
|
||||
const obj = this.getObjectToTestReference();
|
||||
if (typeof obj.length !== 'number' || obj.length !== length) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not have a length of ${length}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeCloseTo(value: number, precision = 2) {
|
||||
return this.runCheck(() => {
|
||||
const difference = Math.abs(this.getObjectToTestReference() - value);
|
||||
if (difference > Math.pow(10, -precision) / 2) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} is not close to ${value} up to ${precision} decimal places`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toThrow(expectedError?: any) {
|
||||
return this.runCheck(() => {
|
||||
let thrown = false;
|
||||
try {
|
||||
this.getObjectToTestReference()();
|
||||
} catch (e) {
|
||||
thrown = true;
|
||||
if (expectedError && !(e instanceof expectedError)) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`Expected function to throw ${expectedError.name}, but it threw ${e.name}`
|
||||
);
|
||||
}
|
||||
}
|
||||
if (!thrown) {
|
||||
throw new Error(`Expected function to throw, but it didn't.`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeTruthy() {
|
||||
return this.runCheck(() => {
|
||||
if (!this.getObjectToTestReference()) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not truthy`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeFalsy() {
|
||||
return this.runCheck(() => {
|
||||
if (this.getObjectToTestReference()) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not falsy`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeGreaterThanOrEqual(numberArg: number) {
|
||||
return this.runCheck(() => {
|
||||
if (this.getObjectToTestReference() < numberArg) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} is not greater than or equal to ${numberArg}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeLessThanOrEqual(numberArg: number) {
|
||||
return this.runCheck(() => {
|
||||
if (this.getObjectToTestReference() > numberArg) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} is not less than or equal to ${numberArg}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toMatchObject(objectArg: object) {
|
||||
return this.runCheck(() => {
|
||||
// Implement a partial object match if needed.
|
||||
const matchResult = plugins.fastDeepEqual(this.getObjectToTestReference(), objectArg);
|
||||
if (!matchResult) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not match the object ${JSON.stringify(objectArg)}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toContainEqual(value: any) {
|
||||
return this.runCheck(() => {
|
||||
const arr = this.getObjectToTestReference();
|
||||
if (!Array.isArray(arr)) {
|
||||
throw new Error(
|
||||
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.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not contain the value ${JSON.stringify(value)}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeArray() {
|
||||
return this.runCheck(() => {
|
||||
if (!Array.isArray(this.getObjectToTestReference())) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not an array`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toInclude(substring: string) {
|
||||
return this.runCheck(() => {
|
||||
const testRef = this.getObjectToTestReference();
|
||||
if (typeof testRef !== 'string' || !testRef.includes(substring)) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not include the substring ${substring}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toHaveLengthGreaterThan(length: number) {
|
||||
return this.runCheck(() => {
|
||||
const obj = this.getObjectToTestReference();
|
||||
if (typeof obj.length !== 'number' || obj.length <= length) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not have a length greater than ${length}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toHaveLengthLessThan(length: number) {
|
||||
return this.runCheck(() => {
|
||||
const obj = this.getObjectToTestReference();
|
||||
if (typeof obj.length !== 'number' || obj.length >= length) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not have a length less than ${length}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeDate() {
|
||||
return this.runCheck(() => {
|
||||
const testRef = this.getObjectToTestReference();
|
||||
if (!(testRef instanceof Date)) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not a date`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeBeforeDate(date: Date) {
|
||||
return this.runCheck(() => {
|
||||
const testRef = this.getObjectToTestReference();
|
||||
if (!(testRef instanceof Date) || testRef >= date) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not before ${date}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeAfterDate(date: Date) {
|
||||
return this.runCheck(() => {
|
||||
const testRef = this.getObjectToTestReference();
|
||||
if (!(testRef instanceof Date) || testRef <= date) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not after ${date}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public customAssertion(
|
||||
assertionFunction: (value: any) => boolean,
|
||||
errorMessage: string
|
||||
) {
|
||||
return this.runCheck(() => {
|
||||
if (!assertionFunction(this.getObjectToTestReference())) {
|
||||
throw new Error(this.failMessage || errorMessage);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Drill into a property
|
||||
*/
|
||||
public property(propertyNameArg: string) {
|
||||
this.propertyDrillDown.push(propertyNameArg);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Drill into an array index
|
||||
*/
|
||||
public arrayItem(indexArg: number) {
|
||||
// Save the number (instead of "[index]")
|
||||
this.propertyDrillDown.push(indexArg);
|
||||
return this;
|
||||
}
|
||||
|
||||
public log() {
|
||||
console.log(`this is the object to test:`);
|
||||
console.log(JSON.stringify(this.getObjectToTestReference(), null, 2));
|
||||
return this;
|
||||
}
|
||||
}
|
@ -1,14 +1,9 @@
|
||||
import * as smartdelay from '@pushrocks/smartdelay';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as smartdelay from '@push.rocks/smartdelay';
|
||||
import * as smartpromise from '@push.rocks/smartpromise';
|
||||
|
||||
export {
|
||||
smartdelay,
|
||||
smartpromise
|
||||
}
|
||||
export { smartdelay, smartpromise };
|
||||
|
||||
// third party scope
|
||||
import fastDeepEqual from 'fast-deep-equal';
|
||||
|
||||
export {
|
||||
fastDeepEqual,
|
||||
}
|
||||
export { fastDeepEqual };
|
||||
|
14
tsconfig.json
Normal file
14
tsconfig.json
Normal file
@ -0,0 +1,14 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"experimentalDecorators": true,
|
||||
"useDefineForClassFields": false,
|
||||
"target": "ES2022",
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "NodeNext",
|
||||
"esModuleInterop": true,
|
||||
"verbatimModuleSyntax": true
|
||||
},
|
||||
"exclude": [
|
||||
"dist_*/**/*.d.ts"
|
||||
]
|
||||
}
|
17
tslint.json
17
tslint.json
@ -1,17 +0,0 @@
|
||||
{
|
||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
||||
"rules": {
|
||||
"semicolon": [true, "always"],
|
||||
"no-console": false,
|
||||
"ordered-imports": false,
|
||||
"object-literal-sort-keys": false,
|
||||
"member-ordering": {
|
||||
"options":{
|
||||
"order": [
|
||||
"static-method"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"defaultSeverity": "warning"
|
||||
}
|
Reference in New Issue
Block a user