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

importをモックする話

 importをモックする話

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

Sota Sugiura

June 20, 2016
Tweet

More Decks by Sota Sugiura

Other Decks in Programming

Transcript

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

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

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

    * @description ໨ࠇ۠ຽ͔Ͳ͏͔νΣοΫ͢Δ * @param {String} name * @return {Boolean} */ export default function isMeguroJser(name) { return (people.indexOf(name) > 0); } QFPQMF഑ྻʹ ໊લ͕͋Δ͔ݟͯΔ
  4. վΊͯ // 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ͯ͠Δ
  5. QFPQMFKT // people.js /** @type {Array} ໨ࠇ۠ຽͷօ༷ */ export default

    [ 'sota1235', 'sota1236', 'sota1237', ] bTPUB`͕USVF  bTPUB`͕GBMTFͳΒΑͦ͞͏
  6. // 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(); }); });
  7. ׬

  8. 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͕ ໨ࠇ۠ʹӬॅ͢Δલఏ
  9. 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͕ ໨ࠇ۠ʹདྷͳ͍લఏ
  10. DIFDLFSKT // checker.js // ໨ࠇ۠ຽҰཡΛimport import people from 'meguro'; /**

    * @description ໨ࠇ۠ຽ͔Ͳ͏͔νΣοΫ͢Δ * @param {String} name * @return {Boolean} */ export default function isMeguroJser(name) { return (people.indexOf(name) > 0); } ςετͷ͚࣌ͩ ͜͜Λελϒʹ͢Δ
  11. DIFDLFSKTΛϞοΫ import proxyquire from 'proxyquire'; const stubs = { people:

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

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

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

    [ 'sota4567', 'sota4568', ], }; const mockChecker = proxyquire('./checker', stubs); ୈೋҾ਺ʹελϒ ελϒ͍ͨ͠ϞδϡʔϧΛ LFZWBMVFܗࣜͰ౉͢
  15. मਖ਼൛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(); }); });
  16. 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`ؚ͕·ΕΔ
  17. 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ϞδϡʔϧΛελϒ ۠ຽ͕୭΋͍ͳ͍͜ͱʹ