0.3.2 • Published 3 years ago

tyte v0.3.2

Weekly downloads
33
License
MIT
Repository
-
Last release
3 years ago

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:

    	```ts
    	test.each(list)("should ...", tyte((element) => {
    		// ...
    	}));
    	```
    
    	Such code will produce compilation errors.
    
    	> Any suggestions and PRs are very welcome!
0.3.2

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.1.2

3 years ago

0.1.0

3 years ago