Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

unlight/typescript-transform-unspec

Repository files navigation

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

About

Typescript transform plugin removes spec definition from source file

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  
Morty Proxy This is a proxified and sanitized view of the page, visit original site.