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-tsd-transform

v1.1.1

Published

Jest transform for running tsd tests.

Downloads

16

Readme

Jest TSD Transform

A Jest transform library used to execute type assertions using tsd.

Overview

Jest TSD Transform can be configured as a transform in Jest to execute tsd assertions inside suites of Jest tests, providing a uniform display of test results that includes both typing and regular tests.

Motivation

TypeScript provides us with many typing features. While that is great for the flexibility, the added complexity can sometimes lead to bugs in the typing behavior - something that can be mitigated by properly testing the types. Jest is a very popular testing framework, but using it to test TypeScript's types is not trivial as the typing information is lost during compilation. tsd provides a way to evaluate types, detecting typing errors and evaluating typing assertions, but using two separate testing frameworks introduces inconsistencies and makes it harder to deal with test results.

Getting Started

The following steps show how to set up Jest TSD Transform on a Jest project.

Install Jest TSD Transform:

Using npm:

npm install jest-tsd-transform --save-dev

Using yarn:

yarn add jest-tsd-transform --dev

Add the following to your Jest configuration:

  moduleFileExtensions: ["js", "ts", "json"],
  transform: {
    "^.*(\\.|\\/)(test\\.ts)$": "jest-tsd-transform",
  },
  testMatch: ["<rootDir>/**/*test.ts"],

If you use the Jest configuration proposed above, any files in the project ending with test.ts will be transformed by Jest TSD Transform to support typing assertions.

Use typing assertions in tests:

You can use any of tsd's type assertions in asynchronous tests:

import { expectType, expectError } from "tsd";

const concat: {
  (value1: string, value2: string): string;
  (value1: number, value2: number): number;
} = (v1: any, v2: any) => v1 + v2;

describe("concatenating strings", () => {
  it("returns string", async () => {
    expectType<string>(concat("foo", "bar"));
  });
  it("works", () => {
    expect(concat("foo", "bar")).toBe("foobar");
  });
});

describe("concatenating numbers", () => {
  it("returns string", async () => {
    expectType<string>(concat(1, 2));
  });
  it("works", () => {
    expect(concat(1, 2)).toBe("12");
  });
});

describe("concatenating booleans", () => {
  it("rejects boolean parameters", async () => {
    expectError(
      // @ts-expect-error
      concat("foo", false)
    );
    // @ts-expect-error
    expectError(concat(true, false));
  });
});

Running the test suite above produces the following results:

 FAIL  ./sample.test.ts (12.672 s)
  concatenating strings
    ✓ returns string (8176 ms)
    ✓ works (13 ms)
  concatenating numbers
    ✕ returns string (7 ms)
    ✕ works (9 ms)
  concatenating booleans
    ✓ rejects boolean parameters (4 ms)

  ● concatenating numbers › returns string

    Argument of type 'number' is not assignable to parameter of type 'string'.

      17 | describe("concatenating numbers", () => {
      18 |   it("returns string", async () => {
    > 19 |     expectType<string>(concat(1, 2));
         |                        ^
      20 |   });
      21 |   it("works", () => {
      22 |     expect(concat(1, 2)).toBe("12");

      at sample.test.ts:19:24

  ● concatenating numbers › works

    expect(received).toBe(expected) // Object.is equality

    Expected: "12"
    Received: 3

      20 |   });
      21 |   it("works", () => {
    > 22 |     expect(concat(1, 2)).toBe("12");
         |                          ^
      23 |   });
      24 | });
      25 |

      at Object.<anonymous> (sample.test.ts:22:26)

Test Suites: 1 failed, 1 total
Tests:       2 failed, 3 passed, 5 total
Snapshots:   0 total
Time:        12.858 s
Ran all test suites.
npm ERR! Test failed.  See above for more details.

Limitations

  • The test functions have to be asynchronous (with the async keyword) to support typing assertions.
  • import type {...} from ... statements are supported by tsd, but Jest's dependency resolver ignores those lines, so it is preferable to use import {...} from ... on typing test files to import types instead.