Jest에는 전역 환경에 있는 함수와 객체들이 있는데 그 중에서 before, after, only, skip 를 araboza.
1. Before / After
Before, After 는 테스트 함수 실행 전 후에 실행되는 함수이다.
뒤에 Each와 All을 붙여서 각각 사용할 수 있다.
1.1. BeforeEach / AfterEach
Each를 붙이면 각 test 함수마다 실행된다.
let num = 10;
//각 테스트 전마다 실행
beforeEach(() => {
console.log("test start");
num = 0;
});
//각 테스트 직후마다 실행
afterEach(() => {
console.log("test end");
});
test("+1", () => {
console.log("test1");
num += 1;
expect(num).toBe(1);
});
test("+2", () => {
console.log("test2");
num += 2;
expect(num).toBe(2);
});
test1, test2 전후로 beforeEach, AfterEach가 한번씩 실행된다.
1.2. BeforeAll / AfterAll
beforeAll과 afterAll은 모든 테스트 전후에 한번만 실행된다.
//모든 테스트를 통틀어서 직전 한번만 실행
beforeAll(() => {
console.log("test start");
num = 0;
});
//모든 테스트가 끝나고 한번만 실행
afterAll(() => {
console.log("test end");
});
test("+1", () => {
console.log("test1");
num += 1;
expect(num).toBe(1);
});
test("+2", () => {
console.log("test2");
num += 2;
expect(num).toBe(2);
});
test1, test2 전후에 한번만 콘솔이 찍히고
num = 0 이 test 실행 전 한번만 할당돼서
test2에선 2를 기대했지만 실제론 값이 3이 되었기 때문에 fail이 된다.
2. only
only가 붙은 테스트만 실행되고 나머지 테스트는 skip 된다.
특정 테스트만 실행하고 싶을 때 유용하다.
test("test1", () => {
console.log("test1");
});
//only 만 실행
test.only("test2", () => {
console.log("test2");
});
test("test3", () => {
console.log("test3");
});
3. skip
skip이 붙은 테스트만 skip되고 나머지는 실행된다.
test("test1", () => {
console.log("test1");
});
//skip 만 건너뜀
test.skip("test2", () => {
console.log("test2");
});
test("test3", () => {
console.log("test3");
});
참고
https://jestjs.io/docs/api#testonlyname-fn-timeout
https://www.youtube.com/watch?v=TRZ2XdmctSQ&list=RDCMUCxft4RZ8lrK_BdPNz8NOP7Q&index=1
'Web > JEST' 카테고리의 다른 글
JEST - snapshot (1) | 2023.12.07 |
---|---|
JEST - Mock (2) | 2023.12.06 |
JEST - Matcher (2) | 2023.12.05 |