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!
Using npm:
npm install --save-dev babel-plugin-stub
or using yarn:
yarn add babel-plugin-stub --dev
// 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),
};
};
// @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;
const obj = {
// @stub 'stub value'
property: 'value',
};
turns into
const obj = {
property: 'stub value',
};
// @stub true
if (variable > 0) {
// @stub false
} else if (variable < 0) {
}
turns into
if (true) {
} else if (false) {
}
function fn() {
// @stub 'stub value'
return 'value';
}
turns into
function fn() {
return 'stub value';
}
fn(
// @stub 'stub value'
variable,
// @stub 'stub value'
'value'
);
turns into
fn(
'stub value',
'stub value,
)