Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/parzh/tyte


https://github.com/parzh/tyte

jest testing typescript

Last synced: 2 days ago
JSON representation

Awesome Lists containing this project

README

        

# `tyte`

Write Jest tests for TypeScript types.

# Usage

> Also see **Caveats** section below

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

## Positive testing:

```ts
test("MyNumber should resolve to a number", tyte((value: MyNumber) => {
tyte.expectType(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(subject);
}));
});
```

## Negative testing:

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

## Use several subjects in test callback:

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

## Iterate over several subjects in [`.each` methods]:

```ts
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!

[`.each` methods]: https://jestjs.io/docs/en/api#methods