Jest Globals
ํ ์คํธ ํ๋ ์์ํฌ ์ค ํ๋์ธ Jest์๋ ๋ค์๊ณผ ๊ฐ์ ์ ์ญ ๊ฐ์ฒด์ ํจ์๋ค์ ์ ๊ณตํ๊ณ ์์ต๋๋ค.
beforeAll
- ํ์ผ ์์ ๊ฐ ํ ์คํธ๊ฐ ์คํ๋๊ธฐ ์ ์คํ๋ฉ๋๋ค.
- ์ฝ๋ฐฑ ํจ์๊ฐ promise๋ฅผ ๋ฐํํ๋ ๊ฒฝ์ฐ, promise๊ฐ ์ดํ(fulfilled)๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฝ๋๋ค.
- ์ฌ๋ฌ ํ ์คํธ์์ ์ฌ์ฉ๋๋ ์ ์ญ ์ํ๋ฅผ ์ค์ (set up)ํ๋ ค๋ ๊ฒฝ์ฐ ์ฌ์ฉํ ์ ์์ต๋๋ค.
beforeEach
- ํ์ผ ์์ ๊ฐ ํ ์คํธ๊ฐ ์คํ๋๊ธฐ ์ ์คํ๋ฉ๋๋ค.
- ์ฝ๋ฐฑ ํจ์๊ฐ promise๋ฅผ ๋ฐํํ๋ ๊ฒฝ์ฐ, promise๊ฐ ์ดํ(fulfilled)๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฝ๋๋ค.
- ๊ฐ ํ ์คํธ์์ ์ฌ์ฉ๋๋ ์ ์ญ ์ํ๋ฅผ ์ฌ์ค์ (reset)ํ๋ ค๋ ๊ฒฝ์ฐ ์ฌ์ฉํ ์ ์์ต๋๋ค.
afterAll
- ํ์ผ ์์ ๋ชจ๋ ํ ์คํธ๊ฐ ์๋ฃ๋ ํ ์คํ๋ฉ๋๋ค.
- ์ฝ๋ฐฑ ํจ์๊ฐ promise๋ฅผ ๋ฐํํ๋ ๊ฒฝ์ฐ, promise๊ฐ ์ดํ(fulfilled)๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฝ๋๋ค.
- ํ ์คํธ ์ ๋ฐ์ ๊ฑธ์ณ ์ฌ์ฉ๋๋ ์ ์ญ ์ํ๋ฅผ ์ ๋ฆฌ(clean up)ํ๋ ค๋ ๊ฒฝ์ฐ ์ฌ์ฉํ ์ ์์ต๋๋ค.
afterEach
- ํ์ผ ์์ ๊ฐ ํ ์คํธ๊ฐ ์๋ฃ๋ ํ ์คํ๋ฉ๋๋ค.
- ์ฝ๋ฐฑ ํจ์๊ฐ promise๋ฅผ ๋ฐํํ๋ ๊ฒฝ์ฐ, promise๊ฐ ์ดํ(fulfilled)๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฝ๋๋ค.
- ๊ฐ ํ ์คํธ์์ ์์ฑ๋ ์์ ์ํ๋ฅผ ์ ๋ฆฌ(clean up)ํ๋ ค๋ ๊ฒฝ์ฐ ์ฌ์ฉํ ์ ์์ต๋๋ค.
describe
- ์ฌ๋ฌ ๊ฐ์ ํ ์คํธ๋ฅผ ๋ฌถ์ด์ ๊ทธ๋ฃนํํด์ฃผ๋ ํจ์์ ๋๋ค.
- ํ ์คํธ ์ผ์ด์ค๋ฅผ ๊ทธ๋ฃนํํ๋ ๋ชฉ์ ์ ํจ์์ด๊ธฐ ๋๋ฌธ์, beforeAll, beforeEach, afterAll, afterEach์ ๊ฐ์ด ๋น๋๊ธฐ ์์ ์ ๊ธฐ๋ค๋ฆฌ๋ ๋ก์ง์ ๋ฃ์ ์ ์์ต๋๋ค.
it / test
- ํ ์คํธํ ๋ด์ฉ์ ์ ๋ ํจ์์ ๋๋ค.
- ์ฝ๋ฐฑ ํจ์๊ฐ promise๋ฅผ ๋ฐํํ๋ ๊ฒฝ์ฐ, promise๊ฐ ์ดํ(fulfilled)๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฝ๋๋ค.
์คํ ์์
describe๊ฐ ์ค์ฒฉ๋์ง ์์๋ค๋ฉด, < beforeAll - beforeEach - test - afterEach - afterAll > ์ ์์๋ก ์งํ๋๊ฒ ์ฃ .
describe๊ฐ ์ค์ฒฉ๋๋ค๋ฉด ์ด๋ป๊ฒ ๋ ๊น์?
์๋์ ๊ฐ์ด describe ๋ธ๋ก ์ํ์ globals ํจ์๋ค์ ๋ฐฐ์นํ์ ๋ ์ด๋ค ์์๋ก ์คํ๋๋์ง ๋ณด๊ฒ ์ต๋๋ค.
describe('Sample Test', () => {
beforeAll(() => {
console.log('โ
Outer before all');
});
beforeEach(() => {
console.log('โ
Outer before each');
});
describe('sample test', () => {
beforeAll(() => {
console.log('โ
Inner before all');
});
beforeEach(() => {
console.log('โ
Inner before Each');
});
test('test1', () => {
console.log('โ
Sample test1');
expect(true).toBe(true);
});
test('test2', () => {
console.log('โ
Sample test2');
expect(true).toBe(true);
});
afterEach(() => {
console.log('โ
Inner after each');
});
afterAll(() => {
console.log('โ
Inner after all');
});
});
afterEach(() => {
console.log('โ
Outer after each');
});
afterAll(() => {
console.log('โ
Outer after all');
});
});
๊ฒฐ๊ณผ
โ
Outer before all
โ
Inner before all
โ
Outer before each
โ
Inner before Each
โ
Sample test1
โ
Inner after each
โ
Outer after each
โ
Outer before each
โ
Inner before Each
โ
Sample test2
โ
Inner after each
โ
Outer after each
โ
Inner after all
โ
Outer after all
์ ๋ฆฌ
์ค์ฒฉ๋ describe ๋ธ๋ก์ด ์๋ ๊ฒฝ์ฐ
- ๋ด๋ถ์ ์๋ beforeAll์ ์ธ๋ถ์ ์๋ beforeEach๋ณด๋ค ๋จผ์ ์คํ๋ฉ๋๋ค.
- beforeEach๋ ์ธ๋ถ์ ์๋ ๋ด๋ถ์ ์๋ , test ๋ธ๋ก๋ง๋ค ์คํ๋ฉ๋๋ค.
- ์ธ๋ถ์ ์๋ beforeEach๊ฐ ๋จผ์ ์คํ๋๊ณ , ์ดํ ๋ด๋ถ์ ์๋ beforeEach๊ฐ ์คํ๋ฉ๋๋ค.
- afterEach๋ ์ธ๋ถ์ ์๋ ๋ด๋ถ์ ์๋ , test ๋ธ๋ก๋ง๋ค ์คํ๋ฉ๋๋ค.
- ๋ด๋ถ์ ์๋ afterEach๊ฐ ๋จผ์ ์คํ๋๊ณ , ์ดํ ์ธ๋ถ์ ์๋ afterEach๊ฐ ์คํ๋ฉ๋๋ค. (beforeEach์ ๋ฐ๋)
beforeEach๊ฐ ์ ์ง ์๋๋๋ก ์คํ๋์ง ์๋๋ค๋ฉด ์คํ์์์ ์ฐ์ ์์๋ฅผ ๋ค์ ํ ๋ฒ ์ดํด๋ณด๋ ๊ฒ์ด ์ข์ต๋๋ค.
์ฐธ๊ณ
1. https://jestjs.io/docs/setup-teardown
'Dev > ํ ์คํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
NestJS์์ createMock์ผ๋ก ๋จ์ ํ ์คํธ ๊ฐ๋จํ๊ฒ ์์ฑํ๋ ๋ฐฉ๋ฒ (0) | 2024.12.05 |
---|