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

importをモックする話

 importをモックする話

Meguro.es #4 で発表しました。

046baac588d91fd78a85b189847a151d?s=128

Sota Sugiura

June 20, 2016
Tweet

Transcript

  1. JNQPSUΛϞοΫ͢Δ࿩ .FHVSPFT!8BOUFEMZ !TPUB

  2. XIPBNJ w 4PUB4VHJVSB !TPUB  w ͪΌΜͱ+4೥໨͘Β͍ w ෆಈલࡏॅ w

    ͍͍ͩͨ໨ࠇ w גࣜձࣾΞΠελΠϧ
  3. ςετͷ࿩

  4. ͜ΜͳϞδϡʔϧ // checker.js // ໨ࠇ۠ຽҰཡΛimport import people from 'people'; /**

    * @description ໨ࠇ۠ຽ͔Ͳ͏͔νΣοΫ͢Δ * @param {String} name * @return {Boolean} */ export default function isMeguroJser(name) { return (people.indexOf(name) > 0); }
  5. ςετํ๏Λߟ͑ͯΈΔ

  6. ৼΔ෣͍Λߟ͑Δ w ඇࠃຽඇ໨ࠇ۠ຽΛ౉ͨ͠ΒGBMTFΛฦ͢ w ࠃຽ໨ࠇ۠ຽΛ౉ͨ͠ΒUSVFΛฦ͢ w ؆୯͡ΌΜ

  7. վΊͯ // checker.js // ໨ࠇ۠ຽҰཡΛimport import people from 'people'; /**

    * @description ໨ࠇ۠ຽ͔Ͳ͏͔νΣοΫ͢Δ * @param {String} name * @return {Boolean} */ export default function isMeguroJser(name) { return (people.indexOf(name) > 0); }
  8. վΊͯ // checker.js // ໨ࠇ۠ຽҰཡΛimport import people from 'people'; /**

    * @description ໨ࠇ۠ຽ͔Ͳ͏͔νΣοΫ͢Δ * @param {String} name * @return {Boolean} */ export default function isMeguroJser(name) { return (people.indexOf(name) > 0); } QFPQMF഑ྻʹ ໊લ͕͋Δ͔ݟͯΔ
  9. վΊͯ // checker.js // ໨ࠇ۠ຽҰཡΛimport import people from 'people'; /**

    * @description ໨ࠇ۠ຽ͔Ͳ͏͔νΣοΫ͢Δ * @param {String} name * @return {Boolean} */ export default function isMeguroJser(name) { return (people.indexOf(name) > 0); } QFPQMF഑ྻʹ ໊લ͕͋Δ͔ݟͯΔ ͜͜ͰQFPQMFΛ JNQPSUͯ͠Δ
  10. QFPQMFKT // people.js /** @type {Array} ໨ࠇ۠ຽͷօ༷ */ export default

    [ 'sota1235', 'sota1236', 'sota1237', ]
  11. QFPQMFKT // people.js /** @type {Array} ໨ࠇ۠ຽͷօ༷ */ export default

    [ 'sota1235', 'sota1236', 'sota1237', ] bTPUB`͕USVF  bTPUB`͕GBMTFͳΒΑͦ͞͏
  12. // test.js import assert from 'assert'; import checker from './checker';

    describe('Test for checker.js', () => { it(‘sota1235͸ࠃຽ :)', done => { assert(checker('sota1235')); done(); }); it(‘sota1238͸ඇࠃຽ :(', done => { assert(!checker('sota1238')); done(); }); });
  13. ςετॻ͚ͨʂ w ͜ΕͰ໨ࠇ+4FSͷબผ͸׬ᘳ w อकͰ͖Δ඼࣭ͷߴ͍ςετ͕ॻ͚ͨ w JNQPSUΛϞοΫ͢ΔඞཁͳΜͯͳ͔ͬͨΜ΍

  14. ׬

  15. ͱ͸͍͔ͳ͍ w ͜ͷςετʹ͸౰વͳ͕Β͕݀͋Δ w ҎԼͷέʔεʹରԠͰ͖ͳ͍ w TPUB͕໨ࠇ۠֎΁Ҡॅ͢Δ w TPUB͕໨ࠇ۠಺΁Ҡॅ͢Δ

  16. UFTUKT // test.js import assert from 'assert'; import checker from

    './checker'; describe('Test for checker.js', () => { it(‘sota1235͸ࠃຽ :)', done => { assert(checker('sota1235')); done(); }); it(‘sota1238͸ඇࠃຽ :(', done => { assert(!checker('sota1238')); done(); }); }); TPUB͕ ໨ࠇ۠ʹӬॅ͢Δલఏ
  17. UFTUKT // test.js import assert from 'assert'; import checker from

    './checker'; describe('Test for checker.js', () => { it(‘sota1235͸ࠃຽ :)', done => { assert(checker('sota1235')); done(); }); it(‘sota1238͸ඇࠃຽ :(', done => { assert(!checker('sota1238')); done(); }); }); TPUB͕ ໨ࠇ۠ʹӬॅ͢Δલఏ TPUB͕ ໨ࠇ۠ʹདྷͳ͍લఏ
  18. ͭΒ͍ w ςετ͍ͨ͠Ϟδϡʔϧͷॲཧ͕·ͨผͷϞδϡʔ ϧʹґଘ͍ͯ͠Δ w ϢχοτςετͷͨΊʹ໨ࠇ۠Ӭॅͱ͍͏ۤߦ

  19. ໨ࠇ۠Ҏ֎Ͱ΋ʜ w ҎԼͷ༷ͳॲཧ΋ςετ͕ॻ͖ͮΒ͍ w "1*ॲཧ w %#ૢ࡞ w %0.ૢ࡞ w

    ؤுͬͯςετ༻࡞ͬͯ΋͍͍͚Ͳ͊͞ʜ
  20. Ͳ͏͢Δ͔ w ґଘϞδϡʔϧΛελϒʹࠩ͠ସ͑ͨϞοΫΛ ࡞Ζ͏

  21. DIFDLFSKT // checker.js // ໨ࠇ۠ຽҰཡΛimport import people from 'meguro'; /**

    * @description ໨ࠇ۠ຽ͔Ͳ͏͔νΣοΫ͢Δ * @param {String} name * @return {Boolean} */ export default function isMeguroJser(name) { return (people.indexOf(name) > 0); } ςετͷ͚࣌ͩ ͜͜Λελϒʹ͢Δ
  22. Ͳ͏΍ͬͯ Ňŏ﹏ŏ ʁ

  23. ͓·ͨͤ

  24. QSPYZRVJSFKT w ຊ೔ͷຊ୊ w ҰݴͰݴ͏ͱʮJNQPSUΛϞοΫ͢ΔʯͨΊͷ OQNϥΠϒϥϦ w ࢖͍ํ͕ඇৗʹγϯϓϧ

  25. QSPYZRVJSFKT w ಺෦࣮૷͸JNQPSUͷϥούʔ w JNQPSU͢ΔϞδϡʔϧ͕౉ͨ͠ελϒΛJNQPSU ͢ΔΑ͏ʹࡉ޻͢Δ

  26. ී௨ͷJNQPSU JNQPSU JNQPSU

  27. QSPYZRVJSFKT JNQPSU JNQPSU ೚ҙͷϞδϡʔϧΛ ελϒʹͰ͖Δ

  28. ࢖ͬͯΈΑ͏ʂ

  29. QFPQMFKTΛελϒʹ͢Δ UFTUKT DIFDLFSKT ελϒ QFPQMFKT JNQPSUQFPQMFGSPNbQFPQMF` Ͱελϒ͕ฦΔ༻ʹ͢Δ

  30. DIFDLFSKTΛϞοΫ import proxyquire from 'proxyquire'; const stubs = { people:

    [ 'sota4567', 'sota4568', ], }; const mockChecker = proxyquire('./checker', stubs);
  31. DIFDLFSKTΛϞοΫ import proxyquire from 'proxyquire'; const stubs = { people:

    [ 'sota4567', 'sota4568', ], }; const mockChecker = proxyquire('./checker', stubs); ୈҰҾ਺ʹελϒΛ஫ೖ͢ΔϞδϡʔϧ NPDL$IFDLFSʹೖΔͷ͸͜ͷϞδϡʔϧ
  32. DIFDLFSKTΛϞοΫ import proxyquire from 'proxyquire'; const stubs = { people:

    [ 'sota4567', 'sota4568', ], }; const mockChecker = proxyquire('./checker', stubs); ୈೋҾ਺ʹελϒ
  33. DIFDLFSKTΛϞοΫ import proxyquire from 'proxyquire'; const stubs = { people:

    [ 'sota4567', 'sota4568', ], }; const mockChecker = proxyquire('./checker', stubs); ୈೋҾ਺ʹελϒ ελϒ͍ͨ͠ϞδϡʔϧΛ LFZWBMVFܗࣜͰ౉͢
  34. मਖ਼൛UFTUKT // test.js import assert from 'assert'; import proxyquire from

    'proxyquire'; describe('Test for checker.js', () => { it(‘sota1235͸ࠃຽ :)', done => { const checkerMock = proxyquire('./checker', { people: ['sota1235'], }); assert(checkerMock('sota1235')); done(); }); it('sota1238͸ඇࠃຽ :(', done => { const checkerMock = proxyquire('./checker', { people: [], }); assert(!checker('sota1238')); done(); }); });
  35. TPUB͸ࠃຽ // test.js import assert from 'assert'; import proxyquire from

    'proxyquire'; describe('Test for checker.js', () => { it('sota1235͸ࠃຽ :)', done => { const checkerMock = proxyquire('./checker', { people: ['sota1235'], }); assert(checkerMock('sota1235')); done(); }); }); QFPQMFϞδϡʔϧΛελϒ bTPUB`ؚ͕·ΕΔ
  36. TPUB͸ඇࠃຽ // test.js import assert from 'assert'; import proxyquire from

    'proxyquire'; describe('Test for checker.js', () => { it('sota1238͸ඇࠃຽ :(', done => { const checkerMock = proxyquire('./checker', { people: [], }); assert(!checker('sota1238')); done(); }); }); QFPQMFϞδϡʔϧΛελϒ ۠ຽ͕୭΋͍ͳ͍͜ͱʹ
  37. .JTTJPO$PNQMFUF w JNQPSUΛελϒʹͰ͖ͨʂ

  38. ॴײ w ಺෦࣮૷ݟ͚ͨͲ͖ͪΜͱ࡞ͬͯ͋Δ͠ΨϯΨ ϯ࢖͑͹͍͍ͱࢥ͏ w ֶशίετ͸ΊͪΌͪ͘Ό௿͍ w ҰԠ͍Ζ͍ΖΦϓγϣϯ͋Δ͚Ͳݸਓతʹ͸ ඪ४ػೳͰॆ෼ w

    ΈΜͳ࢖͓͏
  39. ͪͳΈʹ w ࣅͨ΋ͷͰSFXJSFKTͱ͍͏ͷ΋͋Δ w QSPYZRVJSF͸ઌʹελϒ஫ೖ͢Δ͚ͲSFXJSF ͸TFUUFS HFUUFS͕͋Δ w Ͳ͕͍͍ͬͪͱ͔͸ݸਓతʹͳ͍ͷͰ͓޷ΈͰ Ͳ͏ͧ

  40. Ҏ্ʂ