Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/floofies/js-test
Low budget expect-based unit tests. Isolates between invocations and safely contains everything that can go wrong.
https://github.com/floofies/js-test
bdd expect testing unit-test
Last synced: 27 days ago
JSON representation
Low budget expect-based unit tests. Isolates between invocations and safely contains everything that can go wrong.
- Host: GitHub
- URL: https://github.com/floofies/js-test
- Owner: Floofies
- License: mit
- Created: 2024-03-19T22:13:34.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-03-21T19:04:50.000Z (9 months ago)
- Last Synced: 2024-03-22T03:34:33.469Z (9 months ago)
- Topics: bdd, expect, testing, unit-test
- Language: TypeScript
- Homepage:
- Size: 26.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# js-test
Low budget expect-based unit tests. Isolates between invocations and safely contains everything that can go wrong.
# Usage
1. Import and instantiate the `UnitTest` class.
2. Invoke method `test`, supply a description and callback with an `expect` parameter.
3. Invoke the supplied `expect` function, supply the test data.
4. Invoke the BDD testing methods of the returned object.```JS
import UnitTest from "js-test";const myTest = new UnitTest();
const testPromise = myTest.test("My Test", expect => {
expect(2+2).toBe(4);
expect(2+2).toNotBe(5);
});testPromise.then(testStatus => {
if(testStatus)
console.log("Test passed!");
else
console.log("Test failed!");
});
```Console output:
```
Test "My Test":
ƒ Test started 2024-03-21T02:35:44.098Z
✓ Test PASSED in 0.081msTest passed!
```# `UnitTest` Class
```JS
new UnitTest();
```The class `UnitTest` is used to execute tests, organize test settings, and store test logs.
## Instance Methods
### `UnitTest.prototype.test` Method
```JS
test( description: string, testFunction: (expect) => any ): Promise
```Calls the given `testFunction` function and supplies an `expect` function as the input parameter.
Returns a Promise which resolves to a boolean which indicates if the test passed or failed.
`expect` returns a new `Expectation` class instance. (See [Expectation Class](#expectation-class)).
Here is an example of a valid `testFunction`:
```JS
function(expect) {
expect(anyValue).toBe(anyValue);
}
```## Instance Properties
### `history` Property
- Type `string[][]`The `history` property contains logs of past tests. Is an Array which contains Arrays of strings.
### `emit` Property
- Type `boolean`
- *Default = `true`*The `emit` property is a boolean which ndicates if test progress and results should be logged to console.
# Expectation Class
```JS
new Expectation(anyValue: any);
```The test callback is supplied with an `expect` factory function which returns an `Expectation` instance.
## Instance Properties
### `actualValue` Property
- Type: `any`
The value given to `expect`, regarded as the "actual data" and not the expected data.## Instance Methods
Each of the following methods throws an `ExpectError` when its condition fails:
Method|Expect Input|Method Input|Condition Description
---|---|---|---
`toBe`|any|any|Passes based on the expected value being strictly equivalent to the given value.
`toNotBe`|any|any|Opposite of `toBe`. Passes based on the expected value being strictly equivalent to the given value.
`toReturn`|Function|any|Passes based on the expected function return value being strictly equivalent to the given value.
`toNotReturn`|Function|any|Opposite of `toReturn`. Passes based on the expected function not returning the given value, or none at all.
`toThrow`|Function|any|Passes based on the expected function throwing an instance of the given error, or at all.
`toNotThrow`|Function|any|Opposite of `toThrow`. Passes based on the expected function not throwing an instance of the given error, or none at all.