项目作者: unlight

项目描述 :
Typescript transform plugin removes spec definition from source file
高级语言: TypeScript
项目地址: git://github.com/unlight/typescript-transform-unspec.git
创建时间: 2019-06-11T06:52:44Z
项目社区:https://github.com/unlight/typescript-transform-unspec

开源协议:MIT License

下载


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

  1. export function hello(greet = 'world') {
  2. return `hello ${greet}`;
  3. }
  4. it('hello world test', () => {
  5. expect(hello()).toBe('hello world');
  6. });

After (it removed)

  1. function hello(greet) {
  2. if (greet === void 0) { greet = 'world'; }
  3. return "hello " + greet;
  4. }
  5. `

Pros and Cons

+ All in one file
- Collecting coverage can be tricky

Installation

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

Usage

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

  1. // webpack.config.js
  2. const unspecTransformer = require('typescript-transform-unspec');
  3. rules: [
  4. {
  5. test: /\.tsx?$/,
  6. loader: 'ts-loader', // or 'awesome-typescript-loader'
  7. options: {
  8. getCustomTransformers: program => ({
  9. before: [
  10. unspecTransformer(program),
  11. ]
  12. })
  13. }
  14. },
  15. ]

TTypescript

  1. // tsconfig.json
  2. {
  3. "compilerOptions": {
  4. "plugins": [
  5. { "transform": "typescript-transform-unspec" },
  6. ]
  7. },
  8. }

Rollup (with rollup-plugin-typescript2)

  1. // rollup.config.js
  2. import typescript from 'rollup-plugin-typescript2';
  3. import unspecTransformer from 'typescript-transform-unspec';
  4. plugins: [
  5. typescript({
  6. transformers: [
  7. service => ({
  8. before: [unspecTransformer(service.getProgram())],
  9. after: [],
  10. }),
  11. ],
  12. }),
  13. ]

Resources