0.1.1 • Published 4 years ago
assert.macro v0.1.1
assert.macro
A babel macro to add assertions for development/testing purposes.
Installation
This project relies on babel-plugin-macros, you can install both from npm:
npm install --save-dev assert.macro babel-plugin-macros
Babel config
In your babel config, add "macros"
to your plugins:
{
"plugins": ["macros"]
}
Enabling the assertions
To include the assertions in the babel output, make sure you set the ENABLE_ASSERTIONS
environment variable to "true"
. For example:
ENABLE_ASSERTIONS=true npm run build
All usages of of the call to assert
, including the import will be removed completely, if ENABLE_ASSERTIONS
is not "true"
.
Usage
import assert from 'assert.macro';
class ShoppingCart {
applyDiscount(discount) {
assert(discount > 0, "Discount can't be 0 or negative.");
assert(discount <= 1, "The discount shouldn't make it more expensive.");
this._total *= discount;
}
}
Why
- Documents intent
- More explicit than comments
- Able to catch bugs
Inspirations
- Refactoring by Martin Fowler
- Programming With Assertions (Java)
- Invariant by Facebook
License
MIT © Lion Ralfs