Upgrade to Pro — share decks privately, control downloads, hide ads and more …

From Library to Tool - power-assert as a Genera...

Takuto Wada
November 13, 2016

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

@ nodefest 2016

Takuto Wada

November 13, 2016
Tweet

More Decks by Takuto Wada

Other Decks in Programming

Transcript

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

    Enhancement Tool Takuto Wada Nov 13, 2016 @nodefest 2016
  2. Testing framework should… Evolve slowly. This gives all the people

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

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

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

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

    "Pidgey" Pokemon{name:"Pidgey"} --- [string] 'Poppo' +++ [string] pokemon.name @@ -1,5 +1,6 @@ P -oppo +idgey power-assert output
  7. 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
  8. const assert = require('power-assert'); describe('Pokemon', () => { it('name', ()

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

    => { const pokemon = new Pokemon('Pidgey'); assert(pokemon.name === 'Poppo'); }); }); The last piece E FN P
  10. 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
  11. 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.
  12. assertion is not only a technique of testing, but of

    writing reliable code. …even if you cannot write tests for existing code. →assertive programming
  13. '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
  14. { "env": { "development": { "presets": [ "babel-preset-power-assert" ] },

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

    | | | | | true NaN | false 3 true false FOBCMFQPXFSBTTFSUJOEFWFMPQNFOU
  16. '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