Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lionralfs/assert.macro
A babel macro to add assertions for development/testing purposes
https://github.com/lionralfs/assert.macro
assert assertions babel-macro babel-plugin-macros javascript macro
Last synced: 3 days ago
JSON representation
A babel macro to add assertions for development/testing purposes
- Host: GitHub
- URL: https://github.com/lionralfs/assert.macro
- Owner: lionralfs
- License: mit
- Created: 2019-05-12T00:01:21.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-02-03T11:46:25.000Z (almost 2 years ago)
- Last Synced: 2025-01-15T08:29:06.568Z (15 days ago)
- Topics: assert, assertions, babel-macro, babel-plugin-macros, javascript, macro
- Language: JavaScript
- Homepage:
- Size: 1.97 MB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# assert.macro [![Babel Macro](https://img.shields.io/badge/babel--macro-%F0%9F%8E%A3-f5da55.svg)](https://github.com/kentcdodds/babel-plugin-macros)
A babel macro to add assertions for development/testing purposes.
[![Build Status](https://travis-ci.com/lionralfs/assert.macro.svg?branch=master)](https://travis-ci.com/lionralfs/assert.macro)
## Installation
This project relies on [babel-plugin-macros](https://github.com/kentcdodds/babel-plugin-macros), you can install both from npm:
```sh
npm install --save-dev assert.macro babel-plugin-macros
```#### Babel config
In your babel config, add `"macros"` to your plugins:
```json
{
"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:
```sh
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
```js
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](https://martinfowler.com/books/refactoring.html) by Martin Fowler
- [Programming With Assertions](https://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html) (Java)
- [Invariant](https://github.com/zertosh/invariant) by Facebook## License
[MIT](LICENSE) © Lion Ralfs