Slide 1

Slide 1 text

From Library to Tool power-assert as a General Purpose Assertion Enhancement Tool Takuto Wada Nov 13, 2016 @nodefest 2016

Slide 2

Slide 2 text

Takuto Wada Twitter: @t_wada github: @twada

Slide 3

Slide 3 text

Do you write tests?

Slide 4

Slide 4 text

l+BWB4DSJQU'BUJHVFz

Slide 5

Slide 5 text

Testing framework should… Evolve slowly. This gives all the people confidence that their investment won't go to waste. ,FOU#FDLTBJE IUUQTIPQPSFJMMZDPNQSPEVDUEP

Slide 6

Slide 6 text

There is a clash between constraints. Easy to write vs. Easy to Learn to write ,FOU#FDLBMTPTBJE IUUQTIPQPSFJMMZDPNQSPEVDUEP

Slide 7

Slide 7 text

So I (re)invented power-assert Evolve Slowly + Easy to Learn to Write = power-assert IUUQTHJUIVCDPNQPXFSBTTFSUKTQPXFSBTTFSU

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

assert(pokemon.name === 'Poppo') | | | | | false | "Pidgey" Pokemon{name:"Pidgey"} --- [string] 'Poppo' +++ [string] pokemon.name @@ -1,5 +1,6 @@ P -oppo +idgey power-assert output

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

http://codelunch.fm/16/

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

1→0

Slide 17

Slide 17 text

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.

Slide 18

Slide 18 text

What does this mean for you?

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

One More Thing…

Slide 22

Slide 22 text

assertion is not only a technique of testing, but of writing reliable code. …even if you cannot write tests for existing code. →assertive programming

Slide 23

Slide 23 text

'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

Slide 24

Slide 24 text

{ "env": { "development": { "presets": [ "babel-preset-power-assert" ] }, "production": { "plugins": [ "babel-plugin-unassert" ] } } } .babelrc

Slide 25

Slide 25 text

# calc.js:5 assert(!(isNaN(a) || isNaN(b))) | | | | | | | | | | true NaN | false 3 true false FOBCMFQPXFSBTTFSUJOEFWFMPQNFOU

Slide 26

Slide 26 text

'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

Slide 27

Slide 27 text

power-assert unassert require('assert') code in production code under development Thank you!