lazy-ass-helpful
lazy-ass plugin to automatically show helpful info for failed assertions
Test page, test page BDD (mocha)
Stop writing assertion messages, let the automatic on the fly code rewriting put the entire expression into the text message.
This package provides single function lazyAssHelpful
that transforms any given function
with lazy-ass assertions into another function.
If any lazy assertion fails, it will help its expression in the error message.
Example:
Typical code with assertions requires lots of extra stuff to be helpful, if the condition is false.
{ ; ...}; // Error: a shoud be a number 'foo'
lazy-ass-helpful allows you to skip writing text explanations in assertions
{ ; ...}'foo'; // Error: condition [typeof a === "number"] 'foo'
install and use
node:
npm install lazy-ass-helpful --save
require('lazy-ass-helpful');
browser:
bower install lazy-ass-helpful --save
<script src="bower_components/lazy-ass-helpful-browser.js"></script>
use:
{ ;}var helpfulFoo = ;; // Error: failed; // Error: condition [2 + 2 === 5]
Auto variables
lazyAssHelpful
not only places the expression into the message, it also parses
the expression and puts all variables into the message (if they are defined and not functions)
{ var foo = 'something'; ;}var barHelped = ;;// throws// Error: condition [foo === "nothing"] foo: something
Limitation
Because lazyAssHelpful
rewrites and evals the given function, it no longer can access
the closure variables directly. Only global variables or local variables are allowed
// local variables are ok { var bar = 'bar'; ;};// global variables are okwindowbar = 'bar'; { ;};// closure variables are NOT okbar bar = 'bar'; { ;}; // ReferenceError: bar is undefined
lazy-ass-helpful-bdd
I include lazy-ass-helpful-bdd.js that wraps the common BDD
functions describe
and it
into helpDescribe
and helpIt
. These functions
transform the unit test code using lazyAssHelpful
method.
Typical setup:
The write unit test wrapped in helpDescribe
function using lazyAss
assertions.
If an assertion fails, there will be helpful context.
I also have same project optimized for QUnit assertions rewriting, see qunit-helpful
How?
lazyAssHelpful
rewrites the given function using falafel,
and then returns a new function with every lazyAss(condition, ...)
replaced with
lazyAss(condition, 'condition source', ...)
.
Small print
Author: Gleb Bahmutov © 2014 @bahmutov
License: MIT - do anything with the code, but don't blame me if it does not work.
Support: if you find any problems with this module, email / tweet / open issue on Github