npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jest-runner-tsd

v6.0.0

Published

Run your TypeScript type tests using Jest

Downloads

27,900

Readme

jest-runner-tsd

Run your TypeScript type tests using Jest.

version license

Note: the jest-runner-tsd is using tsd-lite instead of tsd. Both of them have the same type testing logic, but tsd-lite makes it easier to test projects written in TypeScript (or types generated by your library).

Most important differences (for the full list see tsd-lite repo):

  • tsd-lite has no additional rules or checks;
  • jest.config is used to discover test files;
  • tsconfig.json provides configuration for TS compiler. For details see Configuration section;
  • the type assertions must be imported from tsd-lite package.

Install

yarn add --dev jest-runner-tsd @tsd/typescript
# or
npm install --save-dev jest-runner-tsd @tsd/typescript

Remember to install @tsd/typescript package. It is a required peer dependency.

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 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:

module.exports = {
  displayName: {
    color: 'blue',
    name: 'types',
  },
  runner: 'jest-runner-tsd',
  testMatch: ['**/__typetests__/*.test.ts'],
};

TS Compiler

Your test files will be compiled using the TypeScript compiler (similar to tsc), but, instead of emitting JS code, tsd-lite will analyze types and diagnostics returned by the compiler.

To compile each test file, tsd-lite will read the nearest tsconfig.json 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.

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.

Tip: run yarn tsc -p path/to/__typetests__ --showConfig to print the configuration which applies to the test files.

Note: if tsconfig.json is not found, the compiler will fall back to the default configuration.

Optionally Strict

Just like TypeScript, tsd-lite is optionally strict. In contrary, the vanilla tsd is strict by default. If you are migrating your test suite, remember to set "strict": true in tsconfig.json.

import { expectType } from 'tsd-lite';

declare const loggedInUsername: string;

const users = [
  { name: 'Oby', age: 12 },
  { name: 'Heera', age: 32 },
];

const loggedInUser = users.find(u => u.name === loggedInUsername);

expectType<number>(loggedInUser.age);

The assertion in this example fails with "strict": true, but passes with "strict": false.

Writing Tests

Let's say you defined a JsonObject type:

// JsonObject.ts
type JsonValue = string | number | boolean | JsonObject | Array<JsonValue>;

export interface JsonObject {
  [key: string]: JsonValue;
}

It is relatively complex, so it is worth adding a type test to prevent mistakes and regression in the future:

// __typetests__/JsonObject.test.ts
import { expectAssignable, expectNotAssignable } from 'tsd-lite';
import type { JsonObject } from '../JsonObject.js';

expectAssignable<JsonObject>({
  caption: 'test',
  count: 100,
  isTest: true,
  location: { name: 'test', start: [1, 2], valid: false, x: 10, y: 20 },
  values: [0, 10, 20, { x: 1, y: 2 }, true, 'test', ['a', 'b']],
});

expectNotAssignable<JsonObject>({
  filter: () => {},
});

Tip: For the full list of type testing assertions see the documentation of tsd-lite.

Running Tests

If all is set, simply run yarn jest --config jest.config.tsd.js command. Or better include a script in package.json:

"scripts": {
  "test:types": "jest --config jest.config.tsd.js"
}

License

MIT © Jest Community