New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

jest-runner-tsd

Package Overview
Dependencies
Maintainers
2
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-runner-tsd - npm Package Compare versions

Comparing version 1.2.0 to 2.0.0

LICENSE.md

26

package.json
{
"name": "jest-runner-tsd",
"version": "1.2.0",
"description": "A Jest runner that tests typescript typings using tsd under the hood",
"version": "2.0.0",
"description": "Run your TypeScript type tests using Jest",
"keywords": [
"check",
"checker",
"easy",
"jest",
"runner",
"tests",
"tsd",
"typings",
"types",
"typescript"
],
"license": "MIT",

@@ -23,5 +35,3 @@ "repository": "https://github.com/jest-community/jest-runner-tsd.git",

"create-jest-runner": "^0.9.0",
"graceful-fs": "^4.2.8",
"jest-docblock": "^27.0.6",
"mlh-tsd": "^0.14.1"
"tsd-lite": "^0.5.0"
},

@@ -32,2 +42,3 @@ "devDependencies": {

"@babel/preset-typescript": "^7.15.0",
"@tsd/typescript": "~4.4.4",
"@typescript-eslint/eslint-plugin": "^4.33.0",

@@ -42,4 +53,7 @@ "@typescript-eslint/parser": "^4.33.0",

"prettier": "^2.4.1",
"typescript": "^4.4.4"
"typescript": "~4.4.4"
},
"peerDependencies": {
"@tsd/typescript": "^3.8.3 || ^4.0.7"
},
"prettier": {

@@ -46,0 +60,0 @@ "arrowParens": "avoid",

# `jest-runner-tsd`
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
[![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)
Install `jest-runner-tsd`
**Note:** the `jest-runner-tsd` is using [`tsd-lite`](https://github.com/mrazauskas/tsd-lite) instead of [`tsd`](https://github.com/SamVerschueren/tsd). Both of them have the same type testing logic, but `tsd-light` makes it easier to test projects written in TypeScript (or types generated by your library).
```bash
yarn add --dev jest-runner-tsd
Most important differences (for the full list see [`tsd-lite` repo](https://github.com/mrazauskas/tsd-lite)):
# or with NPM
- `tsd-lite` has no additional [rules](https://github.com/SamVerschueren/tsd/issues/32) or checks;
- `jest.config` is used to discover test files;
- and `tsconfig.json` provides configuration for TS compiler. For details see [Configuration](#configuration) section.
npm install --save-dev jest-runner-tsd
## Install
```bash
yarn add --dev jest-runner-tsd @tsd/typescript
# or
npm install --save-dev jest-runner-tsd @tsd/typescript
```
### Adding to Jest Config
Remember to install `@tsd/typescript` package. It is a required peer dependency.
Create a `jest.config.types.js` file and have the runner property set to `jest-runner-tsd` as shown below:
Note that `@tsd/typescript` will be used to compile type tests. Generally it is recommended to match versions of `@tsd/typescript` and `typescript` in a project, but you may choose to test on different version too.
## Configuration
### Jest
First of all, you should [configure Jest](https://jestjs.io/docs/configuration) to discover your test files. For example, if the files have `.test.ts` suffix and live inside `__typetests__` directories, set up `jest.config.tsd.js` like this:
```js
module.exports = {
displayName: {
color: 'blue',
name: 'types',
},
runner: 'jest-runner-tsd',
testMatch: ['**/__typetests__/*.test.ts'],
};
```
In the project `package.json` file, modify the scripts block to use the configuration file as show below:
### TS Compiler
```json
...
"scripts": {
...
"type-tests": "yarn jest --config jest.config.types.js"
}
...
```
Your test files will be compiled using the [TypeScript compiler](https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API) (similar to `tsc`), but, instead of emitting JS code, `tsd-lite` will analyze types and diagnostics returned by the compiler.
### Run
To compile each test file, `tsd-lite` will read the nearest [`tsconfig.json`](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) and will pass the configuration to the compiler. Hence, you may have a configuration for the whole project, or a group of test files, or just a particular test file.
To start the test, just execute the following command
For example, if your project already includes a `tsconfig.json` in the root directory, but you prefer to have different configuration for testing, simply add another `tsconfig.json` to a directory with the test files. It may override or extend your root configuration.
```bash
yarn test-types
```
**Hint:** run `yarn tsc -p path/to/__typetests__ --showConfig` to print the configuration which applies to the test files.
## Writing tests
**Note:** if `tsconfig.json` is not found, the compiler will fall back to the default configuration.
> This runner uses TSD. To see the available assertions, checkout it's [documentation](https://github.com/SamVerschueren/tsd)
### Optionally Strict
### For JavaScript Projects
Just like TypeScript, `tsd-lite` is [optionally strict](https://www.typescriptlang.org/docs/handbook/2/basic-types.html#strictness). In contrary, the vanilla `tsd` is strict by default. If you are migrating your test suite, remember to set `"strict": true` in `tsconfig.json`.
There are multiple ways you can pass a type definition file.
```ts
import { expectType } from 'tsd-lite';
#### Default
declare const loggedInUsername: string;
The type definitions should be in a file named `index.d.ts` in the root directory of the project by default.
const users = [
{ name: 'Oby', age: 12 },
{ name: 'Heera', age: 32 },
];
#### `types` property in package.json
const loggedInUser = users.find(u => u.name === loggedInUsername);
You can also set your `types` property in package.json. The runner will automatically pick the type defintion file from there.
expectType<number>(loggedInUser.age);
```
The assertion in this example fails with `"strict": true`, but passes with `"strict": false`.
## Running Tests
If all is set, simply run `yarn jest -c jest.config.tsd.js` command. Or better include a script in `package.json`:
```json
{
...
"types": "path/to/types.d.ts"
"scripts": {
"test:types": "jest -c jest.config.tsd.js"
}
```
#### Docblocks
## Learn More
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.
To learn more about `tsd` and its assertions see the [documentation](https://github.com/SamVerschueren/tsd).
```ts
/**
* @type ../../custom/path/to/types.d.ts
**/
```
## License
### For TypeScript Projects
> **Note:** This is only a workaround. A stable solution may be introduced in future.
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

@@ -1,55 +0,20 @@

const { dirname, join, posix, relative, sep } = require('path');
const { readFileSync } = require('graceful-fs');
const { parse } = require('jest-docblock');
const tsd = require('mlh-tsd');
const formatErrorMessage = require('./formatErrorMessage');
const tsdLite = require('tsd-lite');
const { formatTsdResults } = require('./formatter');
const { fail } = require('./fail');
const { pass } = require('./pass');
const { fail } = require('./fail');
const TEST_TITLE = 'tsd typecheck';
/**
* @param {string} testFile
* @param {string} fileContents
*/
function resolveTypingsFile(testFile, fileContents) {
let { type } = parse(fileContents);
if (Array.isArray(type)) {
type = type[0];
}
if (type !== undefined) {
return join(dirname(testFile), type);
}
return undefined;
}
/**
* @param {string} input
*/
const normalizeSlashes = input => input.split(sep).join(posix.sep);
module.exports = async ({ config: { rootDir }, testPath }) => {
const testFileContents = readFileSync(testPath, 'utf8');
const testFile = relative(rootDir, testPath);
const typingsFile = resolveTypingsFile(testFile, testFileContents);
module.exports = ({ testPath }) => {
const start = Date.now();
const { diagnostics, numTests } = await tsd.default({
cwd: rootDir,
testFiles: [normalizeSlashes(testFile)],
typingsFile,
});
const { assertionsCount, tsdResults } = tsdLite.default(testPath);
const end = Date.now();
const numFailed = diagnostics.length;
const numPassed = numTests - numFailed;
const numFailed = tsdResults.length;
const numPassed = assertionsCount - numFailed;
if (diagnostics.length > 0) {
const errorMessage = formatErrorMessage(diagnostics, testFileContents);
if (tsdResults.length > 0) {
const errorMessage = formatTsdResults(tsdResults);

@@ -59,3 +24,3 @@ return fail({

end,
test: { path: testFile, title: TEST_TITLE },
test: { path: testPath, title: TEST_TITLE },
numFailed,

@@ -71,4 +36,4 @@ numPassed,

numPassed,
test: { path: testFile, title: TEST_TITLE },
test: { path: testPath, title: TEST_TITLE },
});
};
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc