Test Reference
✅ Equality test with enforced readability, based on the concept of RITEway and inspired by uvu.
Usage
📦 Node
Install @lou.codes/tests
as a dev dependency:
Add a test
script to package.json
:
Add TypeScript support
To support TypeScript, install tsx as a dev dependency:
And update package.json
:
Add coverage
To add coverage, install c8
as a dev dependency:
And update package.json
:
If you added TypeScript support, then update package.json
like this instead:
And update package.json
:
Run tests:
🦕 Deno
Import @lou.codes/test
using the npm:
prefix, and use it directly:
🌎 Browser
Import @lou.codes/test
using esm.sh, and use it directly:
Writing tests
TypeScript
JavaScript
Other alternatives
Instead of exporting an Array
of Test
as default
, the export can also be a
single Test
:
Or multiple exports with different tests:
It can also be used directly without the test
bin by importing the different
utils directly (like with the Deno and Browser examples above):
Default output
@lou.codes/tests
provides a default output for the tests. It looks like this:
And if the wanted/received type is more complex, like an object, then the output goes into details about the error:
But developers can choose to run test
directly and use their own formatter, as
it was pointed out in the previous section.
Useful links
- 📝 Documentation: TypeDoc generated documentation.
- ⏳ Changelog: List of changes between versions.
- ✅ Tests Coverage: Coveralls page with tests coverage.
File System
ReadOnlyURL
Read-only URL
.
ReadOnlyURLs
Iterable of ReadOnlyURL
s.
See
TestTuple<Value>
Tuple used to describe a test result and its path.
See
Type parameters
Type parameter | Value |
---|---|
Value | unknown |
TestsImport<Value>
Promise import of a file containing Test
or Tests
.
See
Type parameters
Type parameter | Value |
---|---|
Value | unknown |
importTest()
Import a file that exports a Test
or an Iterable of Test
.
Parameters
Parameter | Type |
---|---|
url | Readonly <URL > |
Returns
AsyncGenerator
<Test
<unknown
>,
void
, unknown
>
Example
Yields
Imported tests.
importTests()
Imports all the tests of the given Iterable of urls and yields TestTuple
.
Parameters
Parameter | Type | Description |
---|---|---|
urls | ReadOnlyURLs | Array of urls of tests. |
Returns
AsyncGenerator
<TestTuple
,
void
, unknown
>
Example
Yields
TestTuple
containing url and test for it.
Internal
EXCEPTION
Exception difference kind.
UNKNOWN_ERROR
Unknown error.
Output
FAIL
Fail message with colors.
FAILED_TESTS
Failed test title with colors.
PASS
Pass message with colors.
TEST
Test message to be shown next to the test path.
formatValueDictionary
Dictionary type->formatter to be used by formatValue
.
stringifyDifferenceDictionary
Dictionary Difference
kind->formatter.
Type declaration
Member | Type | Value |
---|---|---|
1 | (difference : Difference & object ) => string | … |
2 | (difference : Difference & object ) => string | … |
4 | (difference : Difference & object ) => string | … |
8 | (difference : Difference & object ) => `[31m${string}[39m` | … |
formatPropertyPath()
Stringifies and colorizes an array representing a property path.
Parameters
Parameter | Type | Description |
---|---|---|
propertyPath | ReadOnlyArray <PropertyKey > | Path to format. |
Returns
string
String with formatted path.
Example
formatValue()
Colorizes and formats a value based on its type.
Parameters
Parameter | Type | Description |
---|---|---|
value | unknown | Value to colorize. |
Returns
string
Colorized value as a string.
Example
stringifyDifference()
Takes a Difference
object and returns a string using
stringifyDifferenceDictionary
.
Parameters
Parameter | Type | Description |
---|---|---|
difference | Difference | Difference object. |
Returns
string
Formatted string.
Example
stringifyTest()
Takes a TestResult
and returns a readable string..
Parameters
Parameter | Type | Description |
---|---|---|
testResult | TestResult | Test result object. |
Returns
string
Readable string.
Example
Test
Difference
Difference object from @lou.codes/diff
, with an added “EXCEPTION” kind.
Example
See
Template
Type of value being compared.
Differences
Array of Difference
.
Example
See
Template
Type of values being compared.
Test<Value>
Object that describes a test.
Example
Type parameters
Type parameter | Value | Description |
---|---|---|
Value | unknown | Type of value being tested. |
Type declaration
Member | Type | Description |
---|---|---|
given | string | Description of the given value. |
must | string | Description of the wanted value. |
received | () => Awaitable <Value > | Function that returns a value being tested. |
wanted | () => Awaitable <Value > | Functions that returns the expected value. |
TestResult
Object that describes a test result (given, must and differences).
Example
See
Template
Type of value being tested.
Type declaration
Member | Type | Description |
---|---|---|
differences | Differences | Differences between given and must (undefined when equal). |
TestTuple<Value>
Tuple used to describe a test result and its path.
See
Type parameters
Type parameter | Value |
---|---|
Value | unknown |
Tests<Value>
Iterable of Test
.
Example
See
Type parameters
Type parameter | Value | Description |
---|---|---|
Value | unknown | Type of value being tested. |
TestsImport<Value>
Promise import of a file containing Test
or Tests
.
See
Type parameters
Type parameter | Value |
---|---|
Value | unknown |
evaluate()
Takes a Test
object and returns a promise with a TestResult
.
Type parameters
Type parameter |
---|
Value |
Parameters
Parameter | Type | Description |
---|---|---|
testDescription | Test <Value > | A Test object. |
Returns
Promise
<object
>
A promise with a TestResult
object.
Member | Type | Value |
---|---|---|
differences | undefined | Differences | - |
given | string | testDescription.given |
must | string | testDescription.must |
Example
isTest()
Check if given value is a Test
.
Type parameters
Type parameter | Value |
---|---|
Actual | unknown |
Parameters
Parameter | Type | Description |
---|---|---|
value | Test | Actual | Value to check. |
Returns
value is Test
true
if is a Test
, false
otherwise.