[TIL] Day69 #TDD

Beanxxยท2022๋…„ 8์›” 3์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
69/120
post-thumbnail

2022.08.03(Wed)

[TIL] Day69
[SEB FE] Day70

โ˜‘๏ธย TDD

Test-Driven-Development - ํ…Œ์ŠคํŠธ ์ฃผ๋„ ๊ฐœ๋ฐœ
์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์ „์— ํ…Œ์ŠคํŠธ๋ฅผ ์“ฐ๋Š” SW ๊ฐœ๋ฐœ ๋ฐฉ๋ฒ•๋ก 
๐Ÿ‘‰ย ์ž‘์€ ๋‹จ์œ„์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ์ž‘์„ฑํ•˜๊ณ , ์ด๋ฅผ ํ†ต๊ณผํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜๋Š” ๊ฒƒ

โ™ฝ TDD ๊ฐœ๋ฐœ ์ฃผ๊ธฐ

  1. Write Failing Test: ์‹คํŒจํ•˜๋Š” ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ๋จผ์ € ์ž‘์„ฑ
  2. Make Test Pass: ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์„ฑ๊ณต์‹œํ‚ค๊ธฐ ์œ„ํ•œ ์‹ค์ œ ์ฝ”๋“œ ์ž‘์„ฑ
  3. Refactor: ์ค‘๋ณต ์ฝ”๋“œ ์ œ๊ฑฐ, ์ผ๋ฐ˜ํ™” ๋“ฑ์˜ ๋ฆฌํŒฉํ† ๋ง ์ˆ˜ํ–‰

ย โœ‹ย 1 ๊ณผ์ • ๋งˆ์น˜๊ธฐ ์ „์— 2 ์ž‘์—… ์‹œ์ž‘ํ•˜์ง€ ์•Š๋„๋ก ์ฃผ์˜!
โœ‹ย 2 ์ง„ํ–‰์‹œ, 1์˜ ํ…Œ์ŠคํŠธ ํ†ต๊ณผํ•  ์ •๋„์˜ ์ตœ์†Œ ์ฝ”๋“œ๋งŒ ์ž‘์„ฑํ•ด์•ผ ํ•จ!

๐Ÿคทโ€โ™€๏ธย TDD ์™œ ์‚ฌ์šฉ?? โ†’ ์˜ˆ์ƒํ•˜์ง€ ๋ชปํ–ˆ๋˜ ๋ฒ„๊ทธ๋ฅผ ์ค„์—ฌ ์†Œ์š” ์‹œ๊ฐ„์„ ์ค„์ผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ!


โ˜‘๏ธย React ํ™˜๊ฒฝ์—์„œ์˜ Test

  • Testing Library: React ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ์‹œ ์ž๋™์œผ๋กœ ์ด์šฉ ๊ฐ€๋Šฅ
  • Jest: JavaScript์˜ Testing Framework / Test Runner๋กœ,
    ํ…Œ์ŠคํŠธ ํŒŒ์ผ์„ ์ž๋™์œผ๋กœ ์ฐพ์•„ ํ…Œ์ŠคํŠธ ์‹คํ–‰ โ†’ ๊ฒฐ๊ณผ ๊ธฐ๋Œ€๋งŒํผ ์˜ฌ๋ฐ”๋ฅธ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์ฒดํฌ โ†’ ํ…Œ์ŠคํŠธ ์„ฑ๊ณต/์‹คํŒจ ํŒ๋‹จ

๐Ÿ”นย package.json์˜ dependencies ํ™•์ธ

  1. @testing-library/jest-dom : Jest-dom ์ œ๊ณตํ•˜๋Š” custom matcher ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์คŒ
  2. @testing-library/react : ์ปดํฌ๋„ŒํŠธ ์š”์†Œ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•œ query ํฌํ•จ
  3. @testing-library/user-event: click๊ณผ ๊ฐ™์€ ์‚ฌ์šฉ์ž ์ด๋ฒคํŠธ์— ์ด์šฉ

โœ‹ย ํ…Œ์ŠคํŠธ ์‹คํ–‰์„ ์œ„ํ•ด์„  ํŒŒ์ผ๋ช…์„ <file name>.test.js ๊ฐ™์€ ํ˜•์‹์œผ๋กœ ์ž‘์„ฑ!

test('2 ๋”ํ•˜๊ธฐ 2๋Š” 4', () => {
	expect(2 + 2).toBe(4);
});

// it์„ ์‚ฌ์šฉํ•ด๋„ ๊ฒฐ๊ณผ ๊ฐ™์Œ!
it('2 ๋”ํ•˜๊ธฐ 2๋Š” 4', () => {
	expect(2 + 2).toBe(4);
});

// describe()์„ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ it(), test()๋ฅผ ํฌํ•จ ๊ฐ€๋Šฅ
describe('Simple Tests', () => {
	test('2 ๋”ํ•˜๊ธฐ 2๋Š” 4', () => {
		expect(2 + 2).toBe(4);
	});

	it('2 ๋”ํ•˜๊ธฐ 2๋Š” 4', () => {
		expect(2 + 2).toBe(4);
	});
});

โœ‹ย throw: ํ…Œ์ŠคํŠธ๊ฐ€ ํ†ต๊ณผํ•˜์ง€ ์•Š์•˜์„ ๋•Œ ๋ฉ”์„ธ์ง€ ์ „๋‹ฌ์„ ์œ„ํ•ด ์‚ฌ์šฉ
โœ‹ย expect(), should() ํ•จ์ˆ˜๋Š” ๊ธฐ๋Šฅ์ด ๋งค์šฐ ์œ ์‚ฌํ•˜๋ฏ€๋กœ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ผ์ด ๊ฑฐ์˜ ์—†์Œ

๐Ÿ”ถย chai ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

  • ํ…Œ์ŠคํŠธ์— ํ•„์š”ํ•œ helper ํ•จ์ˆ˜๋“ค์ด ๋‹ด๊ธด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
  • ์ž…๋ ฅ๊ฐ’์ด true๊ฐ€ ์•„๋‹ ๊ฒฝ์šฐ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š” assert()๋ฅผ ์ž์ฒด์ ์œผ๋กœ ์ œ๊ณต
profile
FE developer

0๊ฐœ์˜ ๋Œ“๊ธ€