Skip to content

πŸš€πŸ’ A tool to validate your library can be consumed by other TypeScript projects.

License

Notifications You must be signed in to change notification settings

jimmywarting/pack-n-play

Β 
Β 

Repository files navigation

Google Cloud Platform logo

Pack-N-Play

Make sure TypeScript projects can use the d.ts you're feeding them.

NPM Version Build Status Known Vulnerabilities Code Style: Google

Make sure TypeScript projects can use the d.ts you're feeding them.

It's not uncommon to ship a library written in TypeScript that exports type definitions that cannot be used by dependent code. Typically if the library does not export all types correctly, a TypeScript codebase using the library will fail to compile, potentially forcing the codebase to use the library without typechecks.

If you're shipping .d.ts type definitions, this library can be used as part of your tests to verify that your library can be used by dependent code.

This works by

  • Packing your module as a npm tarball.
  • Setting up your test snippets in a temporary directory.
  • Installing the module from the tarball.
  • Making sure the test snippets can use the module.

Installation

$ npm install --save-dev pack-n-play

Usage

Here's an example that uses mocha (you can use any test harness):

import {packNPlay} from 'pack-n-play';

describe('typescript consumer tests', () => {

  it('should have correct type signature for makeHttpRequestData', async () => {
    const options = {
      packageDir: process.cwd(),  // path to your module.
      sample: {
        description: 'typescript based used can use my type definitions',
        ts: `
              import {makeHttpRequestData} from 'leaky';
              const result = makeHttpRequestData({}, {}, 5);
              console.log(result);
            `
      }
    };
    await packNPlay(options);  // will throw upon error.
  });

});

Targetting ESM packages

It's also possible to target ESM modules in order to make sure the exported definition is working as intended, e.g:

import {packNTest} from 'pack-n-play';

describe('packs my library and try it in a block of code', () => {
  it('should be able to import and use my library', () =>
    packNTest({
      sample: {
        description: 'an ESM package',
        esm: `
          import { getValue } from 'my-esm-package';
          console.log(getValue());
        `, // this block of code is going to be interpreted as ESM
      },
    }));
});

Targetting CommonJS packages

It's also possible to target CommonJS-only package definitions by using a cjs-named code block, similar to the esm example above:

import {packNTest} from 'pack-n-play';

describe('packs my library and try it in a block of code', () => {
  it('should be able to import and use my library', () =>
    packNTest({
      sample: {
        description: 'an CommonJS package',
        cjs: `
          const { getValue } = require('my-esm-package');
          console.log(getValue());
        `, // this block of code is going to be interpreted as CommonJS
      },
    }));
});

About

πŸš€πŸ’ A tool to validate your library can be consumed by other TypeScript projects.

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 99.2%
  • JavaScript 0.8%