Low budget expect-based unit tests. Isolates between invocations and safely contains everything that can go wrong.
- Import and instantiate the
UnitTest
class. - Invoke method
test
, supply a description and callback with anexpect
parameter. - Invoke the supplied
expect
function, supply the test data. - Invoke the BDD testing methods of the returned object.
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.081ms
Test passed!
new UnitTest();
The class UnitTest
is used to execute tests, organize test settings, and store test logs.
test( description: string, testFunction: (expect) => any ): Promise<boolean>
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).
Here is an example of a valid testFunction
:
function(expect) {
expect(anyValue).toBe(anyValue);
}
- Type
string[][]
The history
property contains logs of past tests. Is an Array which contains Arrays of strings.
- Type
boolean
- Default =
true
The emit
property is a boolean which ndicates if test progress and results should be logged to console.
new Expectation(anyValue: any);
The test callback is supplied with an expect
factory function which returns an Expectation
instance.
- Type:
any
The value given to expect
, regarded as the "actual data" and not the expected data.
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. |