jest-runner-tsd
Advanced tools
Comparing version 1.2.0 to 2.0.0
{ | ||
"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", |
104
README.md
# `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 }, | ||
}); | ||
}; |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
11041
5
9
95
0
14
160
+ Addedtsd-lite@^0.5.0
+ Added@tsd/typescript@4.9.5(transitive)
+ Added@types/node@22.13.1(transitive)
+ Addedtsd-lite@0.5.6(transitive)
- Removedgraceful-fs@^4.2.8
- Removedjest-docblock@^27.0.6
- Removedmlh-tsd@^0.14.1
- Removed@nodelib/fs.scandir@2.1.5(transitive)
- Removed@nodelib/fs.stat@2.0.5(transitive)
- Removed@nodelib/fs.walk@1.2.8(transitive)
- Removed@sindresorhus/is@0.14.0(transitive)
- Removed@szmarczak/http-timer@1.1.2(transitive)
- Removed@types/eslint@7.29.0(transitive)
- Removed@types/estree@1.0.6(transitive)
- Removed@types/json-schema@7.0.15(transitive)
- Removed@types/minimist@1.2.5(transitive)
- Removed@types/node@22.13.4(transitive)
- Removed@types/normalize-package-data@2.4.4(transitive)
- Removedansi-align@3.0.1(transitive)
- Removedansi-escapes@4.3.2(transitive)
- Removedansi-regex@5.0.1(transitive)
- Removedarray-union@2.1.0(transitive)
- Removedarrify@1.0.1(transitive)
- Removedboxen@4.2.0(transitive)
- Removedbraces@3.0.3(transitive)
- Removedcacheable-request@6.1.0(transitive)
- Removedcamelcase@5.3.1(transitive)
- Removedcamelcase-keys@6.2.2(transitive)
- Removedchalk@3.0.0(transitive)
- Removedci-info@2.0.0(transitive)
- Removedcli-boxes@2.2.1(transitive)
- Removedclone-response@1.0.3(transitive)
- Removedconfigstore@5.0.1(transitive)
- Removedcrypto-random-string@2.0.0(transitive)
- Removeddecamelize@1.2.0(transitive)
- Removeddecamelize-keys@1.1.1(transitive)
- Removeddecompress-response@3.3.0(transitive)
- Removeddeep-extend@0.6.0(transitive)
- Removeddefer-to-connect@1.1.3(transitive)
- Removeddetect-newline@3.1.0(transitive)
- Removeddir-glob@3.0.1(transitive)
- Removeddot-prop@5.3.0(transitive)
- Removedduplexer3@0.1.5(transitive)
- Removedemoji-regex@8.0.0(transitive)
- Removedend-of-stream@1.4.4(transitive)
- Removederror-ex@1.3.2(transitive)
- Removedescape-goat@2.1.1(transitive)
- Removedeslint-formatter-pretty@4.1.0(transitive)
- Removedeslint-rule-docs@1.1.235(transitive)
- Removedfast-glob@3.3.3(transitive)
- Removedfastq@1.19.0(transitive)
- Removedfill-range@7.1.1(transitive)
- Removedfind-up@4.1.0(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedget-stream@4.1.05.2.0(transitive)
- Removedglob-parent@5.1.2(transitive)
- Removedglobal-dirs@2.1.0(transitive)
- Removedglobby@11.1.0(transitive)
- Removedgot@9.6.0(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedhard-rejection@2.1.0(transitive)
- Removedhas-yarn@2.1.0(transitive)
- Removedhasown@2.0.2(transitive)
- Removedhosted-git-info@2.8.9(transitive)
- Removedhttp-cache-semantics@4.1.1(transitive)
- Removedignore@5.3.2(transitive)
- Removedimport-lazy@2.1.0(transitive)
- Removedimurmurhash@0.1.4(transitive)
- Removedindent-string@4.0.0(transitive)
- Removedini@1.3.7(transitive)
- Removedirregular-plurals@3.5.0(transitive)
- Removedis-arrayish@0.2.1(transitive)
- Removedis-ci@2.0.0(transitive)
- Removedis-core-module@2.16.1(transitive)
- Removedis-extglob@2.1.1(transitive)
- Removedis-fullwidth-code-point@3.0.0(transitive)
- Removedis-glob@4.0.3(transitive)
- Removedis-installed-globally@0.3.2(transitive)
- Removedis-npm@4.0.0(transitive)
- Removedis-number@7.0.0(transitive)
- Removedis-obj@2.0.0(transitive)
- Removedis-path-inside@3.0.3(transitive)
- Removedis-plain-obj@1.1.0(transitive)
- Removedis-typedarray@1.0.0(transitive)
- Removedis-unicode-supported@0.1.0(transitive)
- Removedis-yarn-global@0.3.0(transitive)
- Removedjest-docblock@27.5.1(transitive)
- Removedjson-buffer@3.0.0(transitive)
- Removedjson-parse-even-better-errors@2.3.1(transitive)
- Removedkeyv@3.1.0(transitive)
- Removedkind-of@6.0.3(transitive)
- Removedlatest-version@5.1.0(transitive)
- Removedlines-and-columns@1.2.4(transitive)
- Removedlocate-path@5.0.0(transitive)
- Removedlog-symbols@4.1.0(transitive)
- Removedlowercase-keys@1.0.12.0.0(transitive)
- Removedmake-dir@3.1.0(transitive)
- Removedmap-obj@1.0.14.3.0(transitive)
- Removedmeow@7.1.1(transitive)
- Removedmerge2@1.4.1(transitive)
- Removedmicromatch@4.0.8(transitive)
- Removedmimic-response@1.0.1(transitive)
- Removedmin-indent@1.0.1(transitive)
- Removedminimist@1.2.8(transitive)
- Removedminimist-options@4.1.0(transitive)
- Removedmlh-tsd@0.14.1(transitive)
- Removednormalize-package-data@2.5.0(transitive)
- Removednormalize-url@4.5.1(transitive)
- Removedonce@1.4.0(transitive)
- Removedp-cancelable@1.1.0(transitive)
- Removedp-limit@2.3.0(transitive)
- Removedp-locate@4.1.0(transitive)
- Removedp-try@2.2.0(transitive)
- Removedpackage-json@6.5.0(transitive)
- Removedparse-json@5.2.0(transitive)
- Removedpath-exists@4.0.0(transitive)
- Removedpath-parse@1.0.7(transitive)
- Removedpath-type@4.0.0(transitive)
- Removedpicomatch@2.3.1(transitive)
- Removedplur@4.0.0(transitive)
- Removedprepend-http@2.0.0(transitive)
- Removedpump@3.0.2(transitive)
- Removedpupa@2.1.1(transitive)
- Removedqueue-microtask@1.2.3(transitive)
- Removedquick-lru@4.0.1(transitive)
- Removedrc@1.2.8(transitive)
- Removedread-pkg@5.2.0(transitive)
- Removedread-pkg-up@7.0.1(transitive)
- Removedredent@3.0.0(transitive)
- Removedregistry-auth-token@4.2.2(transitive)
- Removedregistry-url@5.1.0(transitive)
- Removedresolve@1.22.10(transitive)
- Removedresponselike@1.0.2(transitive)
- Removedreusify@1.0.4(transitive)
- Removedrun-parallel@1.2.0(transitive)
- Removedsemver@5.7.26.3.1(transitive)
- Removedsemver-diff@3.1.1(transitive)
- Removedsignal-exit@3.0.7(transitive)
- Removedslash@3.0.0(transitive)
- Removedspdx-correct@3.2.0(transitive)
- Removedspdx-exceptions@2.5.0(transitive)
- Removedspdx-expression-parse@3.0.1(transitive)
- Removedspdx-license-ids@3.0.21(transitive)
- Removedstring-width@4.2.3(transitive)
- Removedstrip-ansi@6.0.1(transitive)
- Removedstrip-indent@3.0.0(transitive)
- Removedstrip-json-comments@2.0.1(transitive)
- Removedsupports-hyperlinks@2.3.0(transitive)
- Removedsupports-preserve-symlinks-flag@1.0.0(transitive)
- Removedterm-size@2.2.1(transitive)
- Removedto-readable-stream@1.0.0(transitive)
- Removedto-regex-range@5.0.1(transitive)
- Removedtrim-newlines@3.0.1(transitive)
- Removedtype-fest@0.13.10.21.30.6.00.8.1(transitive)
- Removedtypedarray-to-buffer@3.1.5(transitive)
- Removedunique-string@2.0.0(transitive)
- Removedupdate-notifier@4.1.3(transitive)
- Removedurl-parse-lax@3.0.0(transitive)
- Removedvalidate-npm-package-license@3.0.4(transitive)
- Removedwidest-line@3.1.0(transitive)
- Removedwrappy@1.0.2(transitive)
- Removedwrite-file-atomic@3.0.3(transitive)
- Removedxdg-basedir@4.0.0(transitive)
- Removedyargs-parser@18.1.3(transitive)