Skip to content

Files

Latest commit

author
roman.vasilev
Jun 14, 2019
02d5cae · Jun 14, 2019

History

History
112 lines (98 loc) · 2.88 KB

README.md

File metadata and controls

112 lines (98 loc) · 2.88 KB

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