The Essential Type Testing Tool.
TSTyche is a type testing tool for TypeScript. It ships with describe()
and test()
helpers, expect
style assertions and a mighty test runner.
Helpers
If you are used to test JavaScript, a simple type test file should look familiar:
import { expect, test } from "tstyche";
function firstItem<T>(target: Array<T>): T | undefined {
return target[0];
}
test("firstItem", () => {
expect(firstItem(["a", "b", "c"])).type.toBe<string | undefined>();
expect(firstItem()).type.toRaiseError("Expected 1 argument");
});
To organize, debug and plan tests TSTyche has:
test()
,it()
anddescribe()
helpers,- with
.only
,.skip
and.todo
run mode flags.
Assertions
The assertions can be used to write type tests (like in the above example) or mixed in your functional tests:
import assert from "node:assert";
import test from "node:test";
import * as tstyche from "tstyche";
function secondItem<T>(target: Array<T>): T | undefined {
return target[1];
}
test("handles numbers", () => {
assert.strictEqual(secondItem([1, 2, 3]), 2);
tstyche.expect(secondItem([1, 2, 3])).type.toBe<number | undefined>();
});
Here is the list of all matchers:
.toBe()
,.toBeAssignableTo()
,.toBeAssignableWith()
compare types or types of expression,.toAcceptProps()
checks JSX component props,.toHaveProperty()
looks up keys on an object type,.toRaiseError()
captures the type error message or code,.toBeString()
,.toBeNumber()
,.toBeVoid()
and 9 more shorthand checks for primitive types.
Runner
The tstyche
command is the heart of TSTyche. For example, it can select test files by path, filter tests by name and pass them through TypeScript 4.8
and latest
:
tstyche JsonObject --only external --target 4.8,latest
This simple! (And it has watch mode too.)