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
Ζ¬ ReadOnlyURL: ReadOnly
<URL
>
Read-only URL
.
ReadOnlyURLs
Ζ¬ ReadOnlyURLs:
IsomorphicIterable
<ReadOnlyURL
>
Iterable of ReadOnlyURL
s.
See
TestTuple
Ζ¬ TestTuple<Value
>: readonly [path: ReadOnlyURL, test: Test<Value>]
Tuple used to describe a test result and its path.
See
Type parameters
Name | Type |
---|---|
Value | unknown |
TestsImport
Promise import of a file containing Test
or Tests
.
See
Type parameters
Name | Type |
---|---|
Value | unknown |
testImport
βΈ testImport(url
):
AsyncGenerator
<Test
<unknown
>, void
,
unknown
>
Import a file that exports a Test
or an Iterable of Test
.
Parameters
Name | Type | Description |
---|---|---|
url | Object | - |
url.hash | string | MDN Reference |
url.host | string | MDN Reference |
url.hostname | string | MDN Reference |
url.href | string | MDN Reference |
url.origin | string | MDN Reference |
url.password | string | MDN Reference |
url.pathname | string | MDN Reference |
url.port | string | MDN Reference |
url.protocol | string | MDN Reference |
url.search | string | MDN Reference |
url.searchParams | Readonly <ReadonlyMap <string , null | string >> | MDN Reference |
url.username | string | MDN Reference |
url.toJSON | () => string | - |
url.toString | () => string | - |
Returns
AsyncGenerator
<Test
<unknown
>, void
,
unknown
>
Example
Yields
Imported tests.
testsImport
βΈ testsImport(urls
):
AsyncGenerator
<TestTuple
, void
,
unknown
>
Imports all the tests of the given Iterable of urls and yields TestTuple
.
Parameters
Name | 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
β’ Const
EXCEPTION: "EXCEPTION"
Exception difference kind.
UNKNOWN_ERROR
β’ Const
UNKNOWN_ERROR: "Unknown Error"
Unknown error.
Output
FAIL
β’ Const
FAIL: `[31m${string}[39m`
Fail message with colors.
FAILED_TESTS
β’ Const
FAILED_TESTS: string
Failed test title with colors.
PASS
β’ Const
PASS: `[32m${string}[39m`
Pass message with colors.
TEST
β’ Const
TEST: `[33m${string}[39m`
Test message to be shown next to the test path.
formatValueDictionary
β’ Const
formatValueDictionary: ReadOnlyRecord
<TypeOfValue
, (value
:
unknown
) => string
>
Dictionary type->formatter to be used by formatValue
.
stringifyDifferenceDictionary
β’ Const
stringifyDifferenceDictionary: { readonly [Kind in
Difference[βkindβ]]: Function }
Dictionary Difference
kind->formatter.
formatPropertyPath
βΈ formatPropertyPath(propertyPath
): string
Stringifies and colorizes an array representing a property path.
Parameters
Name | Type | Description |
---|---|---|
propertyPath | ReadOnlyArray <PropertyKey > | Path to format. |
Returns
string
String with formatted path.
Example
formatValue
βΈ formatValue(value
): string
Colorizes and formats a value based on its type.
Parameters
Name | Type | Description |
---|---|---|
value | unknown | Value to colorize. |
Returns
string
Colorized value as a string.
Example
stringifyDifference
βΈ stringifyDifference(difference
): string
Takes a Difference
object and returns a string using
stringifyDifferenceDictionary
.
Parameters
Name | Type | Description |
---|---|---|
difference | Difference | Difference object. |
Returns
string
Formatted string.
Example
stringifyTest
βΈ stringifyTest(testResult
): string
Takes a TestResult
and returns a readable string..
Parameters
Name | Type | Description |
---|---|---|
testResult | TestResult | Test result object. |
Returns
string
Readable string.
Example
Test
Difference
Ζ¬ Difference: CreateDifference
| DeleteDifference
| UpdateDifference
| { error
: unknown
; kind
: typeof
EXCEPTION
}
Difference object from @lou.codes/diff
, with an added βEXCEPTIONβ kind.
Example
See
Template
Type of value being compared.
Differences
Ζ¬ Differences:
ReadOnlyArray
<Difference
>
Array of Difference
.
Example
See
Template
Type of values being compared.
Test
Ζ¬ Test<Value
>: Object
Object that describes a test.
Example
Type parameters
Name | Type | Description |
---|---|---|
Value | unknown | Type of value being tested. |
Type declaration
Name | Type | Description |
---|---|---|
given | string | Description of the given value. |
must | string | Description of the wanted value. |
received | () => Awaitable <Value > | - |
wanted | () => Awaitable <Value > | - |
TestResult
Ζ¬ TestResult: Pick
<Test
, "given"
|
"must"
> & { differences?
: Differences
}
Object that describes a test result (given, must and differences).
Example
See
Template
Type of value being tested.
TestTuple
Ζ¬ TestTuple<Value
>: readonly [path: ReadOnlyURL, test: Test<Value>]
Tuple used to describe a test result and its path.
See
Type parameters
Name | Type |
---|---|
Value | unknown |
Tests
Ζ¬ Tests<Value
>:
IsomorphicIterable
<Test
<Value
>>
Iterable of Test
.
Example
See
Type parameters
Name | Type | Description |
---|---|---|
Value | unknown | Type of value being tested. |
TestsImport
Promise import of a file containing Test
or Tests
.
See
Type parameters
Name | Type |
---|---|
Value | unknown |
isTest
βΈ isTest<Actual
>(value
): value is Test
Check if given value is a Test
.
Type parameters
Name | Type |
---|---|
Actual | unknown |
Parameters
Name | Type | Description |
---|---|---|
value | Actual | Test | Value to check. |
Returns
value is Test
true
if is a Test
, false
otherwise.
Example
test
βΈ test<Value
>(testDescription
): Promise
<{ differences
:
undefined
| Differences
; given
:
string
= testDescription.given; must
: string
= testDescription.must }>
Takes a Test
object and returns a promise with a TestResult
.
Type parameters
Name |
---|
Value |
Parameters
Name | Type | Description |
---|---|---|
testDescription | Test <Value > | A Test object. |
Returns
Promise
<{ differences
: undefined
|
Differences
; given
: string
=
testDescription.given; must
: string
= testDescription.must }>
A promise with a TestResult
object.
Example