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. JNQPSUΛϞοΫ͢Δ࿩
    .FHVSPFT!8BOUFEMZ
    !TPUB

    View full-size slide

  2. XIPBNJ
    w 4PUB4VHJVSB !TPUB

    w ͪΌΜͱ+4೥໨͘Β͍
    w ෆಈલࡏॅ
    w ͍͍ͩͨ໨ࠇ
    w גࣜձࣾΞΠελΠϧ

    View full-size slide

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

    View full-size slide

  4. ςετํ๏Λߟ͑ͯΈΔ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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഑ྻʹ
    ໊લ͕͋Δ͔ݟͯΔ
    ͜͜ͰQFPQMFΛ
    JNQPSUͯ͠Δ

    View full-size slide

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

    View full-size slide

  10. QFPQMFKT
    // people.js
    /** @type {Array} ໨ࠇ۠ຽͷօ༷ */
    export default [
    'sota1235',
    'sota1236',
    'sota1237',
    ]
    bTPUB`͕USVF
    bTPUB`͕GBMTFͳΒΑͦ͞͏

    View full-size slide


  11. // 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();
    });
    });

    View full-size slide

  12. ςετॻ͚ͨʂ
    w ͜ΕͰ໨ࠇ+4FSͷબผ͸׬ᘳ
    w อकͰ͖Δ඼࣭ͷߴ͍ςετ͕ॻ͚ͨ
    w JNQPSUΛϞοΫ͢ΔඞཁͳΜͯͳ͔ͬͨΜ΍

    View full-size slide

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

    View full-size slide

  14. 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͕
    ໨ࠇ۠ʹӬॅ͢Δલఏ

    View full-size slide

  15. 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͕
    ໨ࠇ۠ʹདྷͳ͍લఏ

    View full-size slide

  16. ͭΒ͍
    w ςετ͍ͨ͠Ϟδϡʔϧͷॲཧ͕·ͨผͷϞδϡʔ
    ϧʹґଘ͍ͯ͠Δ
    w ϢχοτςετͷͨΊʹ໨ࠇ۠Ӭॅͱ͍͏ۤߦ

    View full-size slide

  17. ໨ࠇ۠Ҏ֎Ͱ΋ʜ
    w ҎԼͷ༷ͳॲཧ΋ςετ͕ॻ͖ͮΒ͍
    w "1*ॲཧ
    w %#ૢ࡞
    w %0.ૢ࡞
    w ؤுͬͯςετ༻࡞ͬͯ΋͍͍͚Ͳ͊͞ʜ

    View full-size slide

  18. Ͳ͏͢Δ͔
    w ґଘϞδϡʔϧΛελϒʹࠩ͠ସ͑ͨϞοΫΛ
    ࡞Ζ͏

    View full-size slide

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

    View full-size slide

  20. Ͳ͏΍ͬͯ Ňŏ﹏ŏ
    ʁ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  23. ී௨ͷJNQPSU
    JNQPSU
    JNQPSU

    View full-size slide

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

    View full-size slide

  25. ࢖ͬͯΈΑ͏ʂ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  30. DIFDLFSKTΛϞοΫ
    import proxyquire from 'proxyquire';
    const stubs = {
    people: [
    'sota4567',
    'sota4568',
    ],
    };
    const mockChecker = proxyquire('./checker', stubs);
    ୈೋҾ਺ʹελϒ
    ελϒ͍ͨ͠ϞδϡʔϧΛ
    LFZWBMVFܗࣜͰ౉͢

    View full-size slide

  31. मਖ਼൛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();
    });
    });

    View full-size slide

  32. 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`ؚ͕·ΕΔ

    View full-size slide

  33. 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ϞδϡʔϧΛελϒ
    ۠ຽ͕୭΋͍ͳ͍͜ͱʹ

    View full-size slide

  34. .JTTJPO$PNQMFUF
    w JNQPSUΛελϒʹͰ͖ͨʂ

    View full-size slide

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

    View full-size slide

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

    View full-size slide