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

ts-snippet

v5.0.2

Published

A TypeScript snippet testing library for any test framework

Downloads

831

Readme

ts-snippet

GitHub License NPM version Build status dependency status devDependency Status peerDependency Status Greenkeeper badge

What is it?

ts-snippet is a TypeScript snippet compiler for any test framework.

It does not run the compiled snippets. Instead, it provides assertion methods that can be used to test the TypeScript programs compiled from the snippets.

Why might you need it?

I created the ts-snippet package out of the need to test overloaded TypeScript functions that have many overload signatures.

The order in which overload signatures are specified is critical and the most specific overloads need to be placed first - as TypeScript will match the first compatible overload signature.

Without using ts-snippet, it's simple to write tests that establish whether or not TypeScript code compiles, but it's more difficult to write tests that establish whether type inferences are correct (especially when any is involved) or whether types are intentionally incompatible (and generate compilation errors).

ts-snippet includes assertions that will verify whether inferred types are what's expected and whether compilation succeeds or fails.

If you need to perform similar assertions, you might find ts-snippet useful.

For an example of how ts-snippet can be used to write tests, checkout the research-spec.ts file in my ts-action repo.

Install

Install the package using npm:

npm install ts-snippet --save-dev

Usage

This simplest way to use ts-snippet is to create a snippet expectation function using expecter:

import { expecter } from "ts-snippet";

const expectSnippet = expecter();

describe("observables", () => {
  it("should infer the source's type", () => {
    expectSnippet(`
      import * as Rx from "rxjs";
      const source = Rx.Observable.of(1);
    `).toInfer("source", "Observable<number>");
  });
});

expecter can be passed a factory so that common imports can be specified in just one place. For example:

import { expecter } from "ts-snippet";

const expectSnippet = expecter(code => `
  import * as Rx from "rxjs";
  ${code}
`);

describe("observables", () => {
  it("should infer the source's type", () => {
    expectSnippet(`
      const source = Rx.Observable.of(1);
    `).toInfer("source", "Observable<number>");
  });
});

Alternatively, the package exports a snippet function that returns a Snippet instance, upon which assertions can be made.

The snippet function takes an object containing one or more files - with the keys representing the file names and the values the file content (as strings). The function also takes an optional Compiler instance - if not specified, a Compiler instance is created within the snippet call. With snippets that import large packages (such as RxJS) re-using the compiler can effect significant performance gains.

Using Mocha, the tests look something like this:

import { Compiler, snippet } from "ts-snippet";

describe("observables", () => {

  let compiler: Compiler;

  before(() => {
    compiler = new Compiler();
  });

  it("should infer the source's type", () => {
    const s = snippet({
      "snippet.ts": `
        import * as Rx from "rxjs";
        const source = Rx.Observable.of(1);
      `
    }, compiler);
    s.expect("snippet.ts").toInfer("source", "Observable<number>");
  });

  it("should infer the mapped type", () => {
    const s = snippet({
      "snippet.ts": `
        import * as Rx from "rxjs";
        const source = Rx.Observable.of(1);
        const mapped = source.map(x => x.toString());
      `
    }, compiler);
    s.expect("snippet.ts").toInfer("mapped", "Observable<string>");
  });
});

Compiler can be passed the TypeScript compilerOptions JSON configuration and root directory for relative path module resolution (defaults to process.cwd()).

new Compiler({
  strictNullChecks: true
}, __dirname); // Now module paths will be relative to the directory where the test file is located.

If the BDD-style expectations are not to your liking, there are alternate methods that are more terse.

When using ts-snippet with AVA or tape, the import should specify the specific subdirectory so that the appropriate assertions are configured and the assertions count towards the test runner's plan.

Using the tape-specific import and terse assertions, tests would look something like this:

import * as tape from "tape";
import { snippet } from "ts-snippet/tape";

tape("should infer Observable<number>", (t) => {
  t.plan(1);
  const s = snippet(t, {
    "snippet.ts": `
      import * as Rx from "rxjs";
      const source = Rx.Observable.from([0, 1]);
    `
  });
  s.infer("snippet.ts", "source", "Observable<number>");
});

For an example of how ts-snippet can be used, have a look at these tests in ts-action.

API

function expecter(
  factory: (code: string) => string = code => code,
  compilerOptions?: object,
  rootDirectory?: string
): (code: string) => Expect;

function snippet(
  files: { [fileName: string]: string },
  compiler?: Compiler
): Snippet;
interface Snippet {
  fail(fileName: string, expectedMessage?: RegExp): void;
  expect(fileName: string): Expect;
  infer(fileName: string, variableName: string, expectedType: string): void;
  succeed(fileName: string): void;
}

interface Expect {
  toFail(expectedMessage?: RegExp): void;
  toInfer(variableName: string, expectedType: string): void;
  toSucceed(): void;
}