This project relies on babel-plugin-macros, you can install both from npm:
npm install --save-dev assert.macro babel-plugin-macros
In your babel config, add "macros"
to your plugins:
{
"plugins": ["macros"]
}
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"
.
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;
}
}
- Documents intent
- More explicit than comments
- Able to catch bugs
- Refactoring by Martin Fowler
- Programming With Assertions (Java)
- Invariant by Facebook
MIT © Lion Ralfs