From Library to Tool - power-assert as a General Purpose Assertion Enhancement Tool

9f3a83db74bee75a64b5e6ed106a775c?s=47 Takuto Wada
November 13, 2016

From Library to Tool - power-assert as a General Purpose Assertion Enhancement Tool

@ nodefest 2016

9f3a83db74bee75a64b5e6ed106a775c?s=128

Takuto Wada

November 13, 2016
Tweet

Transcript

  1. From Library to Tool power-assert as a General Purpose Assertion

    Enhancement Tool Takuto Wada Nov 13, 2016 @nodefest 2016
  2. Takuto Wada Twitter: @t_wada github: @twada

  3. Do you write tests?

  4. l+BWB4DSJQU'BUJHVFz

  5. Testing framework should… Evolve slowly. This gives all the people

    confidence that their investment won't go to waste. ,FOU#FDLTBJE IUUQTIPQPSFJMMZDPNQSPEVDUEP
  6. There is a clash between constraints. Easy to write vs.

    Easy to Learn to write ,FOU#FDLBMTPTBJE IUUQTIPQPSFJMMZDPNQSPEVDUEP
  7. So I (re)invented power-assert Evolve Slowly + Easy to Learn

    to Write = power-assert IUUQTHJUIVCDPNQPXFSBTTFSUKTQPXFSBTTFSU
  8. const assert = require('power-assert'); describe('Pokemon', () => { it('name', ()

    => { const pokemon = new Pokemon('Pidgey'); assert(pokemon.name === 'Poppo'); }); }); power-assert E FN P
  9. assert(pokemon.name === 'Poppo') | | | | | false |

    "Pidgey" Pokemon{name:"Pidgey"} --- [string] 'Poppo' +++ [string] pokemon.name @@ -1,5 +1,6 @@ P -oppo +idgey power-assert output
  10. Stop memorizing tons of assertion APIs. Just create expressions that

    return a truthy value or not. And power-assert will show it to your right on the screen as part of your failure message without you having to type in a message at all. No API is the best API IUUQTHJUIVCDPNQPXFSBTTFSUKTQPXFSBTTFSU
  11. http://codelunch.fm/16/

  12. BDIJFWFNFOUTJO https://npm-stat.com/charts.html?package=power-assert&from=2013-08-21&to=2016-11-13

  13. const assert = require('power-assert'); describe('Pokemon', () => { it('name', ()

    => { const pokemon = new Pokemon('Pidgey'); assert(pokemon.name === 'Poppo'); }); }); The last piece
  14. const assert = require('assert'); describe('Pokemon', () => { it('name', ()

    => { const pokemon = new Pokemon('Pidgey'); assert(pokemon.name === 'Poppo'); }); }); The last piece E FN P
  15. const assert = require('assert'); describe('Pokemon', () => { it('name', ()

    => { const pokemon = new Pokemon('Pidgey'); assert(pokemon.name === 'Poppo'); }); }); We eliminated the last dependency. Now your code does not depend on power-assert
  16. 1→0

  17. From Library To Tool Your code does not depend on

    tools explicitly. Means that you can enhance your code outside from your code. Just like code coverage tools.
  18. What does this mean for you?

  19. 1. Now you can empower any existing assertions without modifying

    code.
  20. 2. Now your code live longer than power-assert. Long live

    assertions!
  21. One More Thing…

  22. assertion is not only a technique of testing, but of

    writing reliable code. …even if you cannot write tests for existing code. →assertive programming
  23. 'use strict'; const assert = require('assert'); function add (a, b)

    { assert(!isNaN(a)); assert.equal(typeof b, 'number'); assert.ok(!isNaN(b)); return a + b; } assert in production code
  24. { "env": { "development": { "presets": [ "babel-preset-power-assert" ] },

    "production": { "plugins": [ "babel-plugin-unassert" ] } } } .babelrc
  25. # calc.js:5 assert(!(isNaN(a) || isNaN(b))) | | | | |

    | | | | | true NaN | false 3 true false FOBCMFQPXFSBTTFSUJOEFWFMPQNFOU
  26. 'use strict'; const assert = require('assert'); function add (a, b)

    { assert(!isNaN(a)); assert.equal(typeof b, 'number'); assert.ok(!isNaN(b)); return a + b; } unassert in production https://github.com/unassert-js/unassert
  27. power-assert unassert require('assert') code in production code under development Thank

    you!