Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: replace mlh-tsd with tsd-lite #41

Merged
merged 19 commits into from Feb 4, 2022
Merged
21 changes: 21 additions & 0 deletions LICENSE.md
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Jest Community

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
95 changes: 36 additions & 59 deletions README.md
@@ -1,84 +1,61 @@
# `jest-runner-tsd`
# jest-runner-tsd
mrazauskas marked this conversation as resolved.
Show resolved Hide resolved

A Jest runner that tests typescript typings using [tsd](https://github.com/SamVerschueren/tsd) under the hood.
> Run your TypeScript type tests using Jest.

## Install

Install `jest-runner-tsd`
[![version](https://img.shields.io/npm/v/jest-runner-tsd.svg)](https://npmjs.com/package/jest-runner-tsd)
[![license](https://img.shields.io/github/license/jest-community/jest-runner-tsd.svg)](https://github.com/jest-community/jest-runner-tsd/blob/main/LICENSE.md)

```bash
yarn add --dev jest-runner-tsd
Note that since `v2` the `jest-runner-tsd` is using [`tsd-lite`](https://github.com/mrazauskas/tsd-lite) instead of [`tsd`](https://github.com/SamVerschueren/tsd). The type testing logic is the same in both implementations, i.e. both of them are wrapping around the [TypeScript compiler](https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API) to analyze types of your code.

# or with NPM
## Differences from `tsd`

npm install --save-dev jest-runner-tsd
```
The usage of `tsd` with monorepos written in TypeScript may become [cumbersome](https://github.com/SamVerschueren/tsd/issues/32), because of checks unrelated with types. `tsd-light` is an attempt to address these and similar issues.

### Adding to Jest Config
Notable differences:

Create a `jest.config.types.js` file and have the runner property set to `jest-runner-tsd` as shown below:
- The `tsd` configuration in `package.json` is ignored. Please use `jest.config` to configure discovery of your test files and `tsconfig.json` to provide configuration for TS compiler.
mrazauskas marked this conversation as resolved.
Show resolved Hide resolved
- The compiler configuration will be read from the nearest `tsconfig.json` for each test file. Hence, you may have a configuration for the whole project, or a group of test files, or just a particular test file.
- `tsd-lite` will use the default TS compiler configuration without altering it. This means you should set `"strict": true` to use strict assertions, which are the default ones in vanilla `tsd`.
- Only type testing is performed without any additional checks or rules.
- The `printType` helper is discarded.

```js
module.exports = {
runner: 'jest-runner-tsd',
};
```

In the project `package.json` file, modify the scripts block to use the configuration file as show below:

```json
...
"scripts": {
...
"type-tests": "yarn jest --config jest.config.types.js"
}
...
```

### Run

To start the test, just execute the following command
## Install

```bash
yarn test-types
yarn add -D jest-runner-tsd @tsd/typescript
mrazauskas marked this conversation as resolved.
Show resolved Hide resolved
SimenB marked this conversation as resolved.
Show resolved Hide resolved
```

## Writing tests

> This runner uses TSD. To see the available assertions, checkout it's [documentation](https://github.com/SamVerschueren/tsd)
Remember to install `@tsd/typescript` package. It is a required peer dependency.
mrazauskas marked this conversation as resolved.
Show resolved Hide resolved

### For JavaScript Projects
### Usage

There are multiple ways you can pass a type definition file.
1. If your type tests live inside `__typetests__` folders, set up `jest.config.tsd.js` like this:

#### Default

The type definitions should be in a file named `index.d.ts` in the root directory of the project by default.
```js
module.exports = {
displayName: {
color: 'blue',
name: 'types',
},
runner: 'jest-runner-tsd',
testMatch: ['**/__typetests__/**/*.test.ts'],
};
```

#### `types` property in package.json
2. Add [`tsconfig.json`](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) to your project with configuration for TS compiler.
mrazauskas marked this conversation as resolved.
Show resolved Hide resolved

You can also set your `types` property in package.json. The runner will automatically pick the type defintion file from there.
3. Run `yarn jest -c jest.config.tsd.js` command or just include a script in `package.json`:

```json
{
...
"types": "path/to/types.d.ts"
"scripts": {
"test:types": "jest -c jest.config.tsd.js"
}
```

#### Docblocks

If the type definition file is located somewhere else then specify its path in the top of respective test file using the `@type` inside a docblock.

```ts
/**
* @type ../../custom/path/to/types.d.ts
**/
```
## Tests

### For TypeScript Projects
To learn more about `tsd` tests and assertions see the [documentation](https://github.com/SamVerschueren/tsd).

> **Note:** This is only a workaround. A stable solution may be introduced in future.
## License

Due to [limitations in TSD](https://github.com/SamVerschueren/tsd/issues/32), the only solution now for testing types in TypeScript projects
would be to have a empty type definition file and specify it's path using one of the many methods explained above.
[MIT](https://github.com/jest-community/jest-runner-tsd/blob/main/LICENSE.md) © Jest Community
14 changes: 13 additions & 1 deletion package.json
@@ -1,7 +1,19 @@
{
"name": "jest-runner-tsd",
"version": "1.2.0",
"description": "A Jest runner that tests typescript typings using tsd under the hood",
"description": "Run your TypeScript type tests using Jest",
"keywords": [
"check",
"checker",
"easy",
"jest",
"runner",
"tests",
"tsd",
"typings",
"types",
"typescript"
],
"license": "MIT",
"repository": "https://github.com/jest-community/jest-runner-tsd.git",
"main": "src/index.js",
Expand Down