tyte
TypeScript icon, indicating that this package has built-in type declarations

0.3.2 • Public • Published

tyte

Write Jest tests for TypeScript types.

Usage

Also see Caveats section below

export type MyNumber = number;
export type MyFunction = (input: string) => boolean;

Positive testing:

test("MyNumber should resolve to a number", tyte((value: MyNumber) => {
	tyte.expectType<number>(value);
}));

describe("MyFunction", () => {
	it("should take a string input", tyte((subject: MyFunction) => {
		tyte.fn.expectParams<[string]>(subject);
	}));

	it("should return a boolean", tyte((subject: MyFunction) => {
		tyte.fn.expectReturns<boolean>(subject);
	}));
});

Negative testing:

test("MyNumber should not resolve to a string", tyte((value: MyNumber) => {
	// @ts-expect-error
	tyte.expectType<string>(value);
}));

Use several subjects in test callback:

test("MyNumber should resolve to a number", tyte((
	value: MyNumber,
	values: MyNumber[],
) => {
	tyte.expectType<number>(value);
	tyte.expectType<number[]>(values);
}));

Iterate over several subjects in .each methods:

type Forward = "forward";
type Vertical = "up" | "down" | Forward;
type Horizontal = "left" | "right" | Forward;

test.each([
	[ "Vertical", tyte.subject as Vertical & Forward ],
	[ "Horizontal", tyte.subject as Horizontal & Forward ],
])("%s should include 'forward'", (identifier, subject) => {
	tyte.expectType<"forward">(subject);
});

Caveats

  • Currently, it is not possible to use tyte() as a function in .each methods, as in:

     test.each(list)("should ...", tyte((element) => {
     	// ...
     }));

    Such code will produce compilation errors.

    Any suggestions and PRs are very welcome!

Package Sidebar

Install

npm i tyte

Weekly Downloads

2

Version

0.3.2

License

MIT

Unpacked Size

9.61 kB

Total Files

7

Last publish

Collaborators

  • parzhitsky