Skip to content

A babel macro to add assertions for development/testing purposes

License

Notifications You must be signed in to change notification settings

lionralfs/assert.macro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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