eater

 eater

node 学園付属小学校の2回目で話した eater の話です。

D76231a2114896dfcc7b79ac69558b79?s=128

Yosuke Furukawa

April 26, 2016
Tweet

Transcript

  1. Eater @yosuke_furukawa

  2. @yosuke_furukawa

  3. Hello, Noders. This is Full-time Papa committer

  4. None
  5. child_process.spawn()

  6. None
  7. Node Quiz

  8. Quiz: child_process API ͷԼهͷ ҧ͍͸ԿͰ͠ΐ͏ 1. exec 2. spawn 3.

    fork
  9. A: spawn ͕Ұ൪ϩʔϨϕϧ exec/fork ͸ spawn ͷ wrapper

  10. A: exec ͸ callback Ͱड͚Δɺ fork ͸ JavaScript ͷίʔυ Λ

    Node.js ͱͯ͠࠶࣮ߦ͢Δ
  11. child_process.spawn const spawn = require('child_process').spawn; const ls = spawn('ls', [‘-al']);

    ls.stdout.on('data', (data) => { console.log(data); }); ls.stderr.on('data', (data) => { console.log(data); }); ls.on('exit', (code) => { if (code !== 0) console.error(`Error occurred`); });
  12. child_process.exec const exec = require('child_process').exec; exec(‘ls -al‘, (err, stdout, stderr)

    => { console.log(`${stdout}`); console.error(`${stderr}`); if (error !== null) { console.error(`Error occurred`); } });
  13. child_process.fork const fork = require('child_process').fork; const ls = fork(‘ls.js’); ls.stdout.on('data',

    (data) => { console.log(data); }); ls.stderr.on('data', (data) => { console.log(data); }); ls.on('exit', (code) => { if (code !== 0) console.error(`Error occurred`); });
  14. spawn > exec ͜ͷॱͰ simple exec > spawn ͜ͷॱͰ easy

    spawn > fork ͜ͷॱͰ simple fork > spawn ͜ͷॱͰ easy
  15. Easy / Simple ͷҧ͍

  16. None
  17. Simple !== Easy

  18. Simple is “୯७” ٬؍తɺҰͭͷࣄ͔͠͠ͳ͍

  19. Easy is “؆୯” ओ؍తɺ”୭͔”ʹͱͬͯ؆୯ “୭ʹͱͬͯ΋؆୯”͸ ଘࡏ͠ͳ͍

  20. ॳ৺ऀ͕ؕΓ͕ͪͳࣄɿ Easy ͷݴ༿ʹὃ͞Ε͕ͪ ୭ʹͱͬͯ΋Easy ͳΜͯ͋Γ ͑ͳ͍

  21. ॳ৺ऀ͕ؕΓ͕ͪͳࣄɿ Simple ͱ Easy Λࠞಉ͍ͯ͠ Δ࿩ʹࠔ࿭͞Ε͕ͪ

  22. Eater

  23. Eater is *Ea*sy *t*est runn*er*

  24. Eater has one simple rule If test file outputs `stderr`

    message, the test failed.
  25. Easy => ୭ʹͱͬͯʁ `node` ͔͠஌Βͳ͍Α͏ͳਓ mocha/jestͷࡉ͔͍ߏจΛ֮ ͑ͯͳͯ͘ɺnode ίϚϯυ ͰεΫϦϓτͱ࣮ͯ͠ߦ͢Δ ͚ͩͰΠφϑͳਓ

  26. Eaterͷtest script͸ `node` command Ͱ΋࣮ߦͰ͖Δ

  27. Easy => ୭ʹͱͬͯʁ mock/spyΛଟ༻͢Δ͚Ͳɺ mock/spyͷ։์࿙ΕΛ๨Ε ͕ͪͳਓʹͱͬͯ

  28. Eaterͷtest͸ҰͭҰ͕ͭผϓ ϩηεͰ࣮ߦ͞ΕΔ

  29. mockΛ։์͠ͳͯ͘OK // you don’t need to fix mock/spy console.log =>

    (message) { assert(message.match(/Foo Bar/)); }; const mockDate = new Date(2016, 3, 9); Date.now = () => { return mockDate.valueOf(); }; process.exit => (exitcode) { assert(exitcode === 1); };
  30. ͔͠΋ϓϩηε਺͸ίΞ਺Ͱ ಄ଧͪʹͳΔ eater ——procs 4 ※procs optionͰՄม

  31. Eater DEMO https://github.com/yosuke- furukawa/eater-demo

  32. Happy Eater https://github.com/yosuke- furukawa/eater