typescript-transform-unspec
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

typescript-transform-unspec

Typescript transform plugin removes spec definition from source file.
Inspired by unassert.

Motivation

Imagine we have function.ts with single function. Usually we create function.spec.ts with tests. But what if we can keep tests in same file, and remove spec defenition for production.

Example

Before

export function hello(greet = 'world') {
    return `hello ${greet}`;
}

it('hello world test', () => {
    expect(hello()).toBe('hello world');
});

After (it removed)

function hello(greet) {
    if (greet === void 0) { greet = 'world'; }
    return "hello " + greet;
}

Pros and Cons

+ All in one file
- Collecting coverage can be tricky

Installation

npm install --save-dev typescript-transform-unspec

Usage

webpack (with ts-loader or awesome-typescript-loader)

// webpack.config.js
const unspecTransformer = require('typescript-transform-unspec');

rules: [
  {
    test: /\.tsx?$/,
    loader: 'ts-loader', // or 'awesome-typescript-loader'
    options: {
      getCustomTransformers: program => ({
          before: [
              unspecTransformer(program),
          ]
      })
    }
  },
]

TTypescript

// tsconfig.json
{
    "compilerOptions": {
        "plugins": [
            { "transform": "typescript-transform-unspec" },
        ]
    },
}

Rollup (with rollup-plugin-typescript2)

// rollup.config.js
import typescript from 'rollup-plugin-typescript2';
import unspecTransformer from 'typescript-transform-unspec';

plugins: [
  typescript({ 
    transformers: [
        service => ({ 
            before: [unspecTransformer(service.getProgram())],
            after: [],
        }),
    ],
  }),
]

Resources

Package Sidebar

Install

npm i typescript-transform-unspec

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

13.3 kB

Total Files

8

Last publish

Collaborators

  • iamthes