Jest๋ ํ์ด์ค๋ถ์์ ์ฌ์ฉ๋๋ ๋ฆฌ์กํธ ์ ํ๋ฆฌ์ผ์ด์ ์ ํฌํจํ ๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ ํ ์คํธ ๋๊ตฌ์ ๋๋ค. Jest์ ์ฒ ํ ์ค ํ๋๋ "Zero-configuration", ์ค์ ์๋ ํ ์คํธ ํ๊ฒฝ์ ์ ๊ณตํ๋ ๊ฒ์ ๋๋ค. ์ด๋ค์ ๊ฐ๋ฐ์์๊ฒ ์ฌ์ฉ ์ค๋น๊ฐ ๋ ๋๊ตฌ๊ฐ ์ฃผ์ด์ง ๋, ๊ฐ๋ฐ์๊ฐ ๋ณด๋ค ๋ง์ test๋ฅผ ์์ฑํ๋ค๊ณ ๋งํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ด๋ฐ ํ ์คํธ๋ ๋ณด๋ค ์์ ์ ์ด๊ณ ๊ฑด๊ฐํ ์ฝ๋์ ๊ฒฐ๊ณผ๋ก ๋์์ต๋๋ค.
Jest๋ Zero-Configuration์ด๋ผ๋ ๋จ์ด์ ๊ฑธ๋ง๊ฒ yarn
์ด๋ npm
์ผ๋ก ์ค์น๋ง ํ๋ฉด ๋์
๋๋ค.
yarn
์ ์ด์ฉํ ์ค์น yarn add --dev jest
npm
์ ์ด์ฉํ ์ค์น npm install --save-dev jest
Jest๋ ์ฌ๋ฌ๊ฐ์ง ํ์ ์ ํ ์คํธ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค.
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
test('object assignment', () => {
const data = { one: 1};
data['two'] = 2;
expect(data).toEqual({one: 1, two: 2});
});
test('adding positive numbers is not zero', () => {
for (let a = 1; a < 10; a++) {
for (let b = 1; b < 10; b++) {
expect(a + b).not.toBe(0);
}
}
});
test('null', () => {
const n = null;
expect(n).toBeNull(); // only null
expect(n).toBeUndefined(); // only undefined
expect(n).not.toBeUndefined(); // opposite of toBeUndefined
expect(n).not.toBeTruthy(); // Anything that an if statement treats as true
expect(n).toBeFalsy(); // Anything that an if statement treats as false
});
test('zero', () => {
const z = 0;
expect(z).not.toBeNull();
expect(z).toBeDefined();
expect(z).not.toBeUndefined();
expect(z).not.toBeTruthy();
expect(z).toBeFalsy();
});
test('two plus two', () => {
const value = 2 + 2;
expect(value).toBeGreaterThan(3);
expect(value).toBeGreaterThanOrEqual(3.5);
expect(value).toBeLessThan(5);
expect(value).toBeLessThanOrEqual(4.5);
expect(value).toBe(4);
expect(value).toEqual(4);
});
// float๊ฐ ํ
์คํธ ์, toEqual ๋์ ์ toBeCloseTo๋ฅผ ์ฌ์ฉํด์ผํฉ๋๋ค.
// toEqual ์ฌ์ฉ์ ๋ฏธ์ธํ๊ฒ ๊ฐ์ด ๋ค๋ฅด๊ฒ ๋์ฌ ์ ์์ด ํ
์คํธ๊ฐ ํ๋ฆด์๋ ์์ต๋๋ค.
test('adding floating point numbers', () => {
const value = 0.1 + 0.2;
expect(value).toBeCloseTo(0.3);
});
// Strings
test('there is no I in team', () => {
expect('team').not.toMatch(/I/);
});
test('but there is a "stop" in Christoph', () => {
expect('Christoph').toMatch(/stop/);
});
const shoppingList = [
'diapers',
'kleenex',
'trash bags',
'paper towels',
'beer'
];
test('the shopping list has beer on it', () => {
expect(shoppingList).toContain('beer');
});
Jest๋ฅผ ์ฌ์ฉํด๋ณด๊ธฐ ์ํด ๊ฐ๋จํ ๋ก๋๋ฒํธ ์์ฑ๊ธฐ๋ฅผ ๋ง๋ค์ด ๋ณด์์ต๋๋ค.
์๊ตฌ์ฌํญ
Host code
const myLottos = shop.buyTicket('5000');
console.log(myLottos.showTickets());
/*
[
[ 10, 2, 15, 28, 26, 8 ],
[ 16, 45, 17, 20, 30, 29 ],
[ 31, 15, 4, 28, 42, 35 ],
[ 5, 34, 35, 22, 20, 40 ],
[ 5, 13, 25, 38, 20, 8 ]
]
*/
test('buy ticket at shop', () => {
const ticket = shop.buyTicket(5000);
expect(ticket.showTickets().length).toBe(5);
});
test('create random Lotto numbers ', () => {
expect(shop.createRandomNumbers().length).toBe(6);
});
const lotto = (function() {
const numbers = Symbol('numbers');
return ({
[numbers]: [],
inputNumbers(inputNumbers) {
this[numbers] = inputNumbers;
},
create: function (inputNumbers) {
const lotto = Object.assign({}, this);
lotto.inputNumbers(inputNumbers);
return lotto;
},
showNumber() {
return this[numbers];
}
});
})();
const shop = ( function () {
const lottos = Symbol('lottos');
return ({
[lottos]: [],
buyTicket(money) {
if(money < 1000) {
throw Error('1000์ ์ด์์ ๊ธ์ก๋ง ๊ฐ๋ฅํฉ๋๋ค.');
}
const count = money / 1000;
for(let i=0; i<count; i++) {
this[lottos].push(lotto.create(this.createRandomNumbers()));
}
},
createRandomNumbers() {
const numbers = [];
for(let i=1; i<=45; i++) {
numbers.push(i);
}
numbers.sort(()=> Math.random() - 0.5);
return numbers.slice(0,7);
},
showTickets() {
console.log(this[lottos].map( a => a.showNumber() ));
return this[lottos];
}
});
})();
module.exports = { lotto, shop };
์์ง Jest๋ฅผ Unit test๋ก ๋ฐ์ ์ฌ์ฉํ์ง ๋ชปํ์์ง๋ง Jest์๋ Snapshot์ด๋ผ๋ ๊ธฐ๋ฅ์ ์ฌ์ฉํ์ฌ React UI Test๋ฅผ ์ง์ํฉ๋๋ค. ๋ค์ ๊ธ์๋ ์ด UI test์ ๊ด๋ จ๋ ์์ ๋ก ๊ธ์ ์ค๋นํด ๋ณด๊ฒ ์ต๋๋ค.
์ ์ฉํ ๊ธ์ด๋ค์!