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

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

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

@ nodefest 2016

Takuto Wada
PRO

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

    View Slide

  2. Takuto Wada
    Twitter: @t_wada
    github: @twada

    View Slide

  3. Do you write tests?

    View Slide

  4. l+BWB4DSJQU'BUJHVFz

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  11. http://codelunch.fm/16/

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  16. 1→0

    View Slide

  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.

    View Slide

  18. What does this
    mean for you?

    View Slide

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

    View Slide

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

    View Slide

  21. One More Thing…

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide