Skip to content

a babel plugin helps you easily mock your code (usually when you debug it) without modify the source code.

Notifications You must be signed in to change notification settings

Darmody/babel-plugin-stub

Repository files navigation

babel-plugin-stub

a babel plugin help you stubbing code easily (usually when you test/debug it) without modify the source code.

It's a handy tool help you temporary modify code when you dubug for some spcial cases. You can also use it combine with testing framework (e.g. Jest) for method stub or mocking!

Install

Using npm:

npm install --save-dev babel-plugin-stub

or using yarn:

yarn add babel-plugin-stub --dev

Usage

Configuration

// babel.config.js
module.exports = function (api) {
  api.cache(true);

  return {
    // recommend using environment variable to apply plugin as your need.
    plugins: [process.env.STUB && 'stub'].filter(i => i),
  };
};

Variable

// @stub 'string'
let string = 'value';
// @stub 1
let number = 'value';
// @stub undefined
let undef = 'value';
// @stub null
let nil = 'value';
// @stub true
let truly = 'value';
// @stub false
let falsy = 'value';

turns into

let string = 'string';
let number = 1;
let undef = undefined;
let nil = null;
let truly = true;
let falsy = false;

Object Property

const obj = {
  // @stub 'stub value'
  property: 'value',
};

turns into

const obj = {
  property: 'stub value',
};

Conditional Statement

// @stub true
if (variable > 0) {
  // @stub false
} else if (variable < 0) {
}

turns into

if (true) {
} else if (false) {
}

Return Statement

function fn() {
  // @stub 'stub value'
  return 'value';
}

turns into

function fn() {
  return 'stub value';
}

Function Arguments

fn(
  // @stub 'stub value'
  variable,
  // @stub 'stub value'
  'value'
);

turns into

fn(
  'stub value',
  'stub value,
)

About

a babel plugin helps you easily mock your code (usually when you debug it) without modify the source code.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published