
์๋ฌธ ์ฐธ๊ณ : DaleSeo ๋ธ๋ก๊ทธ - Jest๋ก ํ๋ผ๋ฏธํฐํ ํ ์คํธํ๊ธฐ
ํ
์คํธ ์ฝ๋๋ฅผ ์์ฑํ๋ค ๋ณด๋ฉด ์
๋ ฅ๊ฐ๋ง ๋ค๋ฅธ ๋์ผํ ํ
์คํธ ์ฝ๋๊ฐ ๋ฐ๋ณต๋๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ต๋๋ค.
์ด๋ Jest์ test.each()์ describe.each()๋ฅผ ์ด์ฉํ๋ฉด ๋ฐ๋ณต๋๋ ์ฝ๋๋ฅผ ์ ๊ฑฐํ๊ณ ,
๋ฐ์ดํฐ ๊ธฐ๋ฐ(๋ฐ์ดํฐ ๋๋ฆฌ๋ธ) ํ
์คํธ๋ฅผ ๊ฐ๊ฒฐํ๊ฒ ์์ฑํ ์ ์์ต๋๋ค.
// add.js
export const add = (a, b) => a + b;
// add.test.js
import { add } from './add';
test.each([
[1, 2, 3],
[2, 2, 4],
[10, -5, 5],
])('add(%i, %i) = %i', (a, b, expected) => {
expect(add(a, b)).toBe(expected);
});
โ ํน์ง
%i, %s, %p ๋ฑ์ ํฌ๋งท ๋ฌธ์์ด์ ํตํด ํ
์คํธ ์ด๋ฆ์ ๋ฐ์ดํฐ ์ฝ์
๊ฐ๋ฅit.each() ๋ ๋์ผํ๊ฒ ๋์ (test์ alias)๐ ์ถ๋ ฅ ์์
โ add(1, 2) = 3
โ add(2, 2) = 4
โ add(10, -5) = 5
// greet.js
export function greet(name, { upper = false, prefix = '' } = {}) {
let msg = `${prefix}${name}`;
return upper ? msg.toUpperCase() : msg;
}
// greet.test.js
import { greet } from './greet';
describe.each([
['๊ธฐ๋ณธ', { upper: false, prefix: '' }],
['๋๋ฌธ์ ์ต์
', { upper: true, prefix: '' }],
['์ ๋์ฌ ์ต์
', { upper: false, prefix: 'Mr. ' }],
])('greet ์ต์
: %s', (_label, options) => {
it('์ด๋ฆ์ ํฌํจํด์ผ ํ๋ค', () => {
const out = greet('kim', options);
expect(out).toEqual(expect.stringContaining('kim'));
});
it('๋๋ฌธ์ ์ต์
์ ๋ฐ์ํ๋ค', () => {
const out = greet('kim', options);
const isUpper = out === out.toUpperCase();
expect(isUpper).toBe(Boolean(options.upper));
});
it('์ ๋์ฌ ์ต์
์ ๋ฐ์ํ๋ค', () => {
const out = greet('kim', options);
expect(out).toEqual(expect.stringContaining(options.prefix));
});
});
โ ํน์ง
๐ ์ถ๋ ฅ ์์
PASS ./greet.test.js
greet ์ต์
: ๊ธฐ๋ณธ
โ ์ด๋ฆ์ ํฌํจํด์ผ ํ๋ค
โ ๋๋ฌธ์ ์ต์
์ ๋ฐ์ํ๋ค
โ ์ ๋์ฌ ์ต์
์ ๋ฐ์ํ๋ค
greet ์ต์
: ๋๋ฌธ์ ์ต์
โ ์ด๋ฆ์ ํฌํจํด์ผ ํ๋ค
โ ๋๋ฌธ์ ์ต์
์ ๋ฐ์ํ๋ค
โ ์ ๋์ฌ ์ต์
์ ๋ฐ์ํ๋ค
greet ์ต์
: ์ ๋์ฌ ์ต์
โ ์ด๋ฆ์ ํฌํจํด์ผ ํ๋ค
โ ๋๋ฌธ์ ์ต์
์ ๋ฐ์ํ๋ค
โ ์ ๋์ฌ ์ต์
์ ๋ฐ์ํ๋ค
Jest๋ ๋ฐฐ์ด ์ธ์๋ ํ ํํ์ ๋ฐ์ดํฐ ์ ๋ ฅ์ ์ง์ํฉ๋๋ค.
test.each`
a | b | expected
${1} | ${2} | ${3}
${2} | ${2} | ${4}
${5} | ${-2}| ${3}
`('add($a, $b) = $expected', ({ a, b, expected }) => {
expect(add(a, b)).toBe(expected);
});
๊ฐ๋ ์ฑ์ด ๋๊ณ , ํ ์ด๋ธ ํํ์ ๋ฐ์ดํฐ๊ฐ ํ๋์ ๋ค์ด์ต๋๋ค.
| ๊ตฌ๋ถ | ๋ฐ๋ณต ๋จ์ | ์ฉ๋ | ์ฅ์ | ์์ |
|---|---|---|---|---|
test.each | ๊ฐ๋ณ ํ ์คํธ | ํ ํจ์์ ์ ๋ ฅ๊ฐ ๋ฐ๋ณต | ๋จ์ํ๊ณ ๋น ๋ฆ | add(a,b) |
describe.each | ํ ์คํธ ๊ทธ๋ฃน | ์ฌ๋ฌ ํ ์คํธ๋ฅผ ํ๊ฒฝ๋ณ๋ก ๋ฐ๋ณต | before/after ํ ์ฌ์ฉ ๊ฐ๋ฅ | ์ต์ , DB ํ๊ฒฝ๋ณ ํ ์คํธ |
test.eachdescribe.eachdescribe.each๋ ๋ฐ์ดํฐ ์ธํธ๋ง๋ค ๋
๋ฆฝ๋ describe ๋ธ๋ก์ ์์ฑํ๊ธฐ ๋๋ฌธ์,
๊ฐ ๋ธ๋ก ๋ด์์ beforeAll, afterAll, beforeEach, afterEach ํ
์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
describe.each([
['MySQL', { host: 'localhost', port: 3306 }],
['PostgreSQL', { host: 'localhost', port: 5432 }],
])('DB ์ฐ๊ฒฐ ํ
์คํธ: %s', (dbName, config) => {
beforeAll(() => {
console.log(`${dbName} ์ฐ๊ฒฐ ์์`);
// connectToDB(config)
});
afterAll(() => {
console.log(`${dbName} ์ฐ๊ฒฐ ํด์ `);
// disconnectFromDB()
});
test('์ฐ๊ฒฐ ์ฑ๊ณต ์ฌ๋ถ ํ์ธ', () => {
expect(true).toBe(true); // ์ค์ ๋ก DB ์ฐ๊ฒฐ ํ
์คํธ
});
test('์ฟผ๋ฆฌ ์คํ ํ
์คํธ', () => {
expect(true).toBe(true);
});
});
์ฆ, ๊ฐ ๋ฐ์ดํฐ ์ธํธ๋ง๋ค setup(์ค๋น)๊ณผ cleanup(์ ๋ฆฌ)๊ฐ ๋ ๋ฆฝ์ ์ผ๋ก ์ด๋ฃจ์ด์ง๋๋ค.
test.each: ๋์ผ ํ
์คํธ๋ฅผ ์ฌ๋ฌ ์
๋ ฅ๊ฐ์ผ๋ก ๋ฐ๋ณตdescribe.each: ์ฌ๋ฌ ํ
์คํธ ๊ทธ๋ฃน์ ํ๊ฒฝ๋ณ๋ก ๋ฐ๋ณตdescribe.each๋ ๊ฐ ๊ทธ๋ฃน๋ง๋ค before/after ํ
์ฌ์ฉ ๊ฐ๋ฅtest.each)describe.each)describe.each)