项目作者: lionralfs

项目描述 :
A babel macro to add assertions for development/testing purposes
高级语言: JavaScript
项目地址: git://github.com/lionralfs/assert.macro.git
创建时间: 2019-05-12T00:01:21Z
项目社区:https://github.com/lionralfs/assert.macro

开源协议:MIT License

下载




# 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:

  1. npm install --save-dev assert.macro babel-plugin-macros

Babel config

In your babel config, add "macros" to your plugins:

  1. {
  2. "plugins": ["macros"]
  3. }

Enabling the assertions

To include the assertions in the babel output, make sure you set the ENABLE_ASSERTIONS environment variable to "true". For example:

  1. 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

  1. import assert from 'assert.macro';
  2. class ShoppingCart {
  3. applyDiscount(discount) {
  4. assert(discount > 0, "Discount can't be 0 or negative.");
  5. assert(discount <= 1, "The discount shouldn't make it more expensive.");
  6. this._total *= discount;
  7. }
  8. }

Why

  • Documents intent
  • More explicit than comments
  • Able to catch bugs

Inspirations

License

MIT © Lion Ralfs