devinrhode2 / better-assert-browser

c-style assert() for nodejs, reporting the expression string as the error message

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

better-assert-browser

Better c-style assertions using for self-documenting failure messages IN THE BUHROWZA.

Installation

npm install devinrhode2/better-assert-browser --save-dev

Then include node_modules/better-assert-browser/better-assert-browser.js in your page. You could also copy and paste the code from this repo, nobody's watching.

Example

By default assertions are enabled, however setting window.NO_ASSERT to true will deactivate them.

try {
  console.log('before calling assert')
  assert('truthy')
  console.log('called assert')
  assert(window.someFoo === 'bar')
  console.log('called with false')
} catch (e) {
  console.error('assert() failed:'+e.stack)
}

This library/function is dependent upon the v8/chrome stacktrace api.

To get stacktraces for failed assertions, you need to check "Async" in the right sidebar of the chrome devtools sources pane

This is because this function makes a request for the javascript file at the file url indicated in the stack trace using window.fetch, which returns a promise, inside this we throw an assertion error with the code asserted.

This library won't work unless your server responds to requests for individual files.

Contributing

hub clone devinrhode2/better-assert-browser # brew install hub if u kno foo
cd better-assert-browser
npm install -g live-server
live-server

Open up http://127.0.0.1:8080/browser-test.html

You should then be able to open better-assert-browser.js and browser-test.js in chrome devtools sources pane, hit esc to show the console, and livedit

MIT License, see LICENSE file

About

c-style assert() for nodejs, reporting the expression string as the error message

License:MIT License


Languages

Language:JavaScript 91.8%Language:HTML 8.2%