Interface CustomTestAPI
Source: test.ts
Utility functions built on top of Vitest's API to make writing tests easier.
See
Properties
simpleCases
simpleCases: <
A,R>(func: (arg:A) =>R,cases: [A,Matcher<R>][]) =>void
A shorthand for CustomTestAPI.cases that tests a function with a single parameter.
Type Parameters
A
A = unknown
The type of parameter the function accepts.
R
R = undefined
The type of value the function returns.
Parameters
func
(arg: A) => R
The function to test.
cases
[A, Matcher<R>][]
An array of tuples. Each tuple consisting of a parameter and the corresponding matching criteria.
Returns
void
Example
test.simpleCases(isEven, [
[0, true],
[1, false],
[2, true],
]);simpleCase
simpleCase: <
A,R>(func: (arg:A) =>R,arg:A,matcher:Matcher<R>) =>void
A shorthand for CustomTestAPI.case that tests a function with a single parameter.
Type Parameters
A
A = unknown
The type of parameter the function accepts.
R
R = undefined
The type of value the function returns.
Parameters
func
(arg: A) => R
The function to test.
arg
A
The parameter to pass to the function.
matcher
Matcher<R>
The matching criteria.
Returns
void
cases
cases: <
A,R>(func: (...args:A) =>R,cases: [A,Matcher<R>][]) =>void
Tests each case in the array by calling the function with the first element of the tuple and checking its behaviour or return value against the matching criteria in the second element.
This is a wrapper around TestAPI.each that handles test naming, function calling and assertions for you, so you can write more parameterized tests with less code.
Type Parameters
A
A extends unknown[] = []
The types of parameters the function accepts.
R
R = undefined
The type of value the function returns.
Parameters
func
(...args: A) => R
The function to test.
cases
[A, Matcher<R>][]
An array of tuples. Each tuple consisting of an array of parameters and the corresponding matching criteria.
Returns
void
Example
test.cases(isEqual, [
[[0, 1], false],
[[2, 3], false],
[[4, 4], true],
]);Comparison with Vitest
Most functions will have simple unit tests like this:
test.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])("add(%i, %i) -> %i", (a, b, expected) => {
expect(a + b).toBe(expected)
})This is the same test, but using the cases wrapper instead:
test.cases(add, [
[[1, 1], 2],
[[1, 2], 3],
[[2, 1], 3]
]);case
case: <
A,R>(func: (...args:A) =>R,args:A,matcher:Matcher<R>) =>void
Tests a single case by calling the function with the given parameters and checking its behaviour or return value against the matching criteria.
This is the equivalent of CustomTestAPI.cases, but for a single test case.
Type Parameters
A
A extends unknown[] = []
The types of parameters the function accepts.
R
R = undefined
The type of value the function returns.
Parameters
func
(...args: A) => R
The function to test.
args
A
The parameters to pass to the function.
matcher
Matcher<R>
The matching criteria.
Returns
void
throws
throws: (
error: (...args:never[]) =>Error,message?:string|RegExp) =>ErrorMatcher
Creates matching criteria for a test case that expects an error to be thrown.
Parameters
error
(...args: never[]) => Error
The type of error to expect.
message?
string | RegExp
The error message to expect.
Returns
See
assert
assert: <
R>(assert: (actual:Assertion<R>) =>void,name?:string) =>AssertionMatcher<R>
Creates matching criteria for a test case where the actual value is verified by the given assertion, instead of simply being compared with an expected return value.
Type Parameters
R
R
Parameters
assert
(actual: Assertion<R>) => void
The function that verifies the actual return value.
name?
string
The name of the assertion.
Returns
See
nullish
nullish: <
R>() =>AssertionMatcher<R>
Creates matching criteria for a test case that expects the actual value to be nullish (null or undefined).
Type Parameters
R
R
Returns
falsy
falsy: <
R>() =>AssertionMatcher<R>
Creates matching criteria for a test case that expects the actual value to be falsy.
Type Parameters
R
R
Returns
truthy
truthy: <
R>() =>AssertionMatcher<R>
Creates matching criteria for a test case that expects the actual value to be truthy.
Type Parameters
R
R
Returns
not
not:
Pick<CustomTestAPI,"nullish"|"falsy"|"truthy">
Negates the proceding matching criteria for a test case.