Jest: ์ •์  ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ ์กฐ๋กฑ

์— ๋งŒ๋“  2016๋…„ 07์›” 05์ผ  ยท  3์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: facebook/jest

Jest์—์„œ ์ •์  ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ๋ฅผ ์กฐ๋กฑํ•˜๋Š” ๋ฐ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋™์ž‘์ด ์ง€์›๋ฉ๋‹ˆ๊นŒ?

http://stackoverflow.com/questions/38206478/mocking-methods-in-jest์— ๊ต์ฐจ ๊ฒŒ์‹œ๋จ

๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋งค์šฐ ๊ฐ„๋‹จํ•œ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

import ApiWrapper from '../../services/api_wrapper';
jest.unmock('../helper')

describe('Helper', () => {
    let Helper;

    beforeEach(() => {
        Helper = require('../helper').default;
    });

    it('calls the Api Wrapper', () => {
        Helper.help()

        expect(ApiWrapper.help).toHaveBeenCalled();
    });

});

๋„์šฐ๋ฏธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

import ApiWrapper from '../services/api_wrapper'
class Helper {
    help() {
        ApiWrapper.help()
    }
}

export default new Helper();

ApiWrapper๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

class ApiWrapper {
  static help () {
     console.log('help!')
  }
}
export default ApiWrapper;

ApiWrapper.help()๋Š” Jest๊ฐ€ ์กฐ๋กฑํ•˜๋ฏ€๋กœ '๋„์™€์ฃผ์„ธ์š”!' ๊ฒฐ์ฝ” ์ธ์‡„๋˜์ง€ ์•Š์ง€๋งŒ ํ…Œ์ŠคํŠธ์˜ ๊ธฐ๋Œ€๋Š” ์‹คํŒจํ•ฉ๋‹ˆ๋‹ค. ApiWrapper๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ผ๋ฐ˜ Javascript ๊ฐ์ฒด๋กœ ๋‹ค์‹œ ์ž‘์„ฑํ•˜๋ฉด ์—ฌ์ „ํžˆ ์‹คํŒจํ•ฉ๋‹ˆ๋‹ค.

export default {
    help: () => { console.log('help!'); }
}

๊ทธ๋Ÿฌ๋‚˜ ์‚ฌ์–‘์—์„œ ๊ฐ€์ ธ์˜ค๊ธฐ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ณ (๋”ฐ๋ผ์„œ ApiWrapper๋Š” beforeEach์—์„œ ๊ฐ€์ ธ์˜ด) ApiWrapper๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด Singleton ํด๋ž˜์Šค๋กœ ๋‹ค์‹œ ์ž‘์„ฑํ•˜๋ฉด ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.

class ApiWrapper {
   help() {
      console.log('help!');
   }
}();

export default new ApiWrapper();

Jest์˜ ์กฐ๋กฑ ํ–‰๋™์— ๋Œ€ํ•ด ์ด๋Ÿฐ ์ผ์ด ๋ฐœ์ƒํ•˜๋Š” ์ด์œ ๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

Jest๋Š” it ํ˜ธ์ถœํ•  ๋•Œ๋งˆ๋‹ค ๋ชจ๋“ˆ ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ๋ฅผ ์žฌ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ƒ๋‹จ์— ApiWrapper ํ•„์š”ํ•˜๊ณ  beforeEach ๋‚ด๋ถ€์— Helper ๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ Helper๋Š” ApiWrapper ์˜ ๋‹ค๋ฅธ ์‚ฌ๋ณธ์„ ๋ฐ›์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์Šต๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด ๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ๋งจ ์œ„์— ์žˆ๋Š” ๋‘ ํด๋ž˜์Šค๋ฅผ ๋ชจ๋‘ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
  • beforeEach ๋‘ ํด๋ž˜์Šค ๋ชจ๋‘ ํ•„์š”

๊ฐ„๋‹จํ•œ ์„ค๋ช…:

var A = require('A');
jest.resetModuleRegistry();
A !== require('A'); // true, A is not the exact same module as before we reset the registry.

๊ทธ๋ž˜๋„ ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋˜์ง€ ์•Š์œผ๋ฉด ์•Œ๋ ค์ฃผ์‹ญ์‹œ์˜ค.

๋ชจ๋“  3 ๋Œ“๊ธ€

์˜ฌ๋ฐ”๋ฅธ ์ฃผ์žฅ์€ toBeCalled ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์‹œ๋„ํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๊นŒ?

์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. Stackoverflow์— ์˜ˆ์ œ๋ฅผ ์ง์ ‘ ์ž…๋ ฅํ–ˆ์Šต๋‹ˆ๋‹ค.

Jest๋Š” it ํ˜ธ์ถœํ•  ๋•Œ๋งˆ๋‹ค ๋ชจ๋“ˆ ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ๋ฅผ ์žฌ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ƒ๋‹จ์— ApiWrapper ํ•„์š”ํ•˜๊ณ  beforeEach ๋‚ด๋ถ€์— Helper ๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ Helper๋Š” ApiWrapper ์˜ ๋‹ค๋ฅธ ์‚ฌ๋ณธ์„ ๋ฐ›์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์Šต๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด ๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ๋งจ ์œ„์— ์žˆ๋Š” ๋‘ ํด๋ž˜์Šค๋ฅผ ๋ชจ๋‘ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
  • beforeEach ๋‘ ํด๋ž˜์Šค ๋ชจ๋‘ ํ•„์š”

๊ฐ„๋‹จํ•œ ์„ค๋ช…:

var A = require('A');
jest.resetModuleRegistry();
A !== require('A'); // true, A is not the exact same module as before we reset the registry.

๊ทธ๋ž˜๋„ ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋˜์ง€ ์•Š์œผ๋ฉด ์•Œ๋ ค์ฃผ์‹ญ์‹œ์˜ค.

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰