0.1.1 • Published 4 years ago

assert.macro v0.1.1

Weekly downloads
5
License
MIT
Repository
github
Last release
4 years ago

assert.macro Babel Macro

A babel macro to add assertions for development/testing purposes.

Build Status

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

License

MIT © Lion Ralfs