const fn = {
add: (num1, num2) => num1 + num2,
}
beforeEach()
: 각 테스트 직전에 실행let num = 10;
beforEach(() => {
num = 0; // 테스트가 실행되기 전에 num이 0으로 할당됨
});
test("0 + 1 = 1", () => {
num = fn.add(num, 1);
expect(num).toBe(1);
});
test("0 + 2 = 2", () => {
num = fn.add(num, 2);
expect(num).toBe(2);
});
test("0 + 3 = 3", () => {
num = fn.add(num, 3);
expect(num).toBe(3);
});
test("0 + 4 = 4", () => {
num = fn.add(num, 4);
expect(num).toBe(4);
});
afterEach()
: 각 테스트 직후에 실행let num = 10;
afterEach(() => {
num = 0; // 첫 번째 테스트 케이스가 실행된 후 num이 0으로 할당됨
});
// 첫 번째 테스트 케이스만 실패
test("0 + 1 = 1", () => {
num = fn.add(num, 1);
expect(num).toBe(1);
});
test("0 + 2 = 2", () => {
num = fn.add(num, 2);
expect(num).toBe(2);
});
test("0 + 3 = 3", () => {
num = fn.add(num, 3);
expect(num).toBe(3);
});
test("0 + 4 = 4", () => {
num = fn.add(num, 4);
expect(num).toBe(4);
});
beforeAll()
/ afterAll()
: 각 테스트 전후에 실행하지 않고 최초에 한 번, 최후에 한 번 실행example) 테스트 전에 userDB에 접속하고 테스트 이후에는 userDB 접속을 끊는 작업이 있다고 가정한다.
const fn = {
connectUserDB: () => {
return new Promise((res) => {
setTimeout(() => {
res({
name: "FuBao",
age: 3,
gender: "female",
});
}, 500);
});
},
disconnectDB: () => {
return new Promise((res) => {
setTimeout(() => {
res();
}, 500);
});
},
};
let user;
beforeAll(async () => {
user = await fn.connectUserDB();
});
afterAll(async () => {
return fn.disconnectDB();
});
test("Name is FuBao", () => {
expect(user.name).toBe("FuBao");
});
test("Age is 3", () => {
expect(user.age).toBe(3);
});
test("Sex is female", () => {
expect(user.gender).toBe("female");
});
최상위 수준 before*
및 after*
hooks는 파일의 모든 테스트에 적용된다. describe()
블록 내부에 선언된 hooks는 해당 설명 블록 내의 테스트에만 적용된다.
describe()
: 여러 연관된 테스트 함수들을 그룹화// 주석 : 코드 실행 순서
beforeAll(() => console.log("밖 beforeAll")); // 1
beforeEach(() => console.log("밖 beforeEach")); // 2, 6
afterEach(() => console.log("밖 afterEach")); // 4, 10
afterAll(() => console.log("밖 afterAll")); // 마지막
test("0 + 1 = 1", () => {
expect(fn.add(0.1)).toBe(1); // 3
});
describe("describe", () => {
beforeAll(() => console.log("안 beforeAll")); // 5
beforeEach(() => console.log("안 beforeEach")); // 7
afterEach(() => console.log("안 afterEach")); // 9
afterAll(() => console.log("안 afterAll")); // 마지막 - 1
test("0 + 1 = 1", () => {
expect(fn.add(0.1)).toBe(1); // 8
});
});
.only
: 여러 테스트 코드 중 하나의 테스트만 실행test.only
로 변경한다.let num = 0;
test("0 + 1 = 1", () => {
expect(fn.add(num, 1)).toBe(1);
});
test("0 + 2 = 2", () => {
expect(fn.add(num, 2)).toBe(2);
});
test("0 + 3 = 3", () => {
expect(fn.add(num, 3)).toBe(3);
});
test("0 + 4 = 4", () => {
expect(fn.add(num, 4)).toBe(4);
num = 10;
});
test("0 + 5 = 5", () => {
expect(fn.add(num, 5)).toBe(6); // failed test
});
FAIL ./mock.test.js
✓ 0 + 1 = 1 (2 ms)
✓ 0 + 2 = 2 (1 ms)
✓ 0 + 3 = 3
✓ 0 + 4 = 4
✕ 0 + 5 = 5 (1 ms)
→ 실패한 테스트만 실행하기 위해 해당 테스트 명령을 임시로 test.only
로 변경
(...생략)
test.only("0 + 5 = 5", () => {
expect(fn.add(num, 5)).toBe(6);
});
FAIL ./mock.test.js
✕ 0 + 5 = 5 (1 ms)
○ skipped 0 + 1 = 1
○ skipped 0 + 2 = 2
○ skipped 0 + 3 = 3
○ skipped 0 + 4 = 4
→ 결과를 통해, 코드 자체에 문제가 있다는 것을 파악한 후 해당 테스트 코드 수정
let num = 0;
test("0 + 1 = 1", () => {
expect(fn.add(num, 1)).toBe(1);
});
test("0 + 2 = 2", () => {
expect(fn.add(num, 2)).toBe(2);
});
test("0 + 3 = 3", () => {
expect(fn.add(num, 3)).toBe(3);
});
test("0 + 4 = 4", () => {
expect(fn.add(num, 4)).toBe(4);
num = 10; // num의 값이 10으로 변경됨
});
test("0 + 5 = 5", () => {
expect(fn.add(num, 5)).toBe(5);
});
FAIL ./mock.test.js
✓ 0 + 1 = 1 (2 ms)
✓ 0 + 2 = 2 (1 ms)
✓ 0 + 3 = 3 (1 ms)
✓ 0 + 4 = 4
✕ 0 + 5 = 5
→ 여전히 문제 발생, 해당 테스트 코드 자체에는 문제가 없기 때문에 외부 요인으로 문제 발생
→ 네 번째 테스트 코드에서 num의 값이 10으로 변경됨
.skip
: 해당 테스트 코드 제외하고 실행test.skip
으로 변경하여 문제 되는 코드를 건너뛴다.test.skip("0 + 4 = 4", () => {
expect(fn.add(num, 4)).toBe(4);
num = 10; // num의 값이 10으로 변경됨
});
PASS ./mock.test.js
✓ 0 + 1 = 1 (3 ms)
✓ 0 + 2 = 2
✓ 0 + 3 = 3
✓ 0 + 5 = 5 (1 ms)
○ skipped 0 + 4 = 4
Test Suites: 1 passed, 1 total
Tests: 1 skipped, 4 passed, 5 total
참고문헌,
https://jestjs.io/docs/setup-teardown#general-advice,
[Youtube]Jest 강좌 - 코딩앙마,