alexreardon / tiny-invariant

A tiny invariant function
MIT License
1.7k stars 48 forks source link

Feature: Auto debugger #191

Open tylerschloesser opened 6 months ago

tylerschloesser commented 6 months ago

This is actually a feature request. I would of course be glad to be directed toward some existing solution.

I very often want to debug via browser devtools when an invariant fails. I do this one of two ways

  1. Set a conditional breakpoint on the invariant where the condition is the inverse of the invariant. This is not always possible/easy if the invariant is nontrivial. I also often forget to remove this breakpoint and have to do so later.
  2. Temporarily refactory/copy my invariant like so
    // invariant(val)
    if (!val) { debugger }

I've never been able to rely on exception breakpoints (caught or uncaught) due to e.g. React.

I'd love some sort of ability to have my invariants automatically open a debugger. Either configured globally (invariant.debugOnFailure = true) or per instance (invariant.debug(val))