> [!date] published: 2025-05-28 ## `describe` 테스트를 그룹화하는 블록 (테스트 수트 (suit)) ## `test` / `it` 개별 테스트를 정의하고 실행 `it`는 `test`의 별칭 ```ts // it should reject invalid password 같이 자연스러운 영어 문장을 만들기 위함 it("should reject invalid password", () => {}); // 위와 완전히 동일한 기능을 한다. test("should reject invalid password", () => {}); ``` ## 테스트 전후 설정/정리 [Setup and Teardown · Jest](https://jestjs.io/docs/setup-teardown) ### `beforeEach` 개별 테스트 전에 실행 ### `afterEach` 개별 테스트 이후에 실행 ### `beforeAll` 모든 테스트 전에 실행 ### `afterAll` 모든 테스트 이후에 실행 ## scope 최상위 `before*`, `after*` 훅은 파일의 모든 테스트에 적용되고, decribe 블록 안의 훅은 해당 블록 안의 테스트에만 적용된다. ## 실행 순서 describe 블록이 먼저 실행된 뒤에 실제 테스트를 실행한다 (`inner describe` 출력 참고) `beforeAll` -> `beforeEach` -> `afterEach` -> `afterAll` - `before*` 는 outer 먼저 실행 - `after*` 는 inner 먼저 실행 ```ts beforeAll(() => { console.log("outer - beforeAll"); }); beforeEach(() => { console.log("outer - beforeEach"); }); afterEach(() => { console.log("outer - afterEach"); }); afterAll(() => { console.log("outer - afterAll"); }); describe("inner", () => { beforeAll(() => { console.log("inner - beforeAll"); }); beforeEach(() => { console.log("inner - beforeEach"); }); afterEach(() => { console.log("inner - afterEach"); }); afterAll(() => { console.log("inner - afterAll"); }); console.log("inner describe"); test("inner test", () => { console.log("inner test"); }); }); // inner describe // outer - beforeAll // inner - beforeAll // outer - beforeEach // inner - beforeEach // inner test // inner - afterEach // outer - afterEach // inner - afterAll // outer - afterAll ``` ## 테스트 패턴 [c# - Differences between Given When Then (GWT) and Arrange Act Assert (AAA)? - Software Engineering Stack Exchange](https://softwareengineering.stackexchange.com/questions/308160/differences-between-given-when-then-gwt-and-arrange-act-assert-aaa) 본질적으로 동일하고 표현의 차이만 있다. ### Arrange Act Assert (AAA) [Arrange-Act-Assert: A Pattern for Writing Good Tests \| Automation Panda](https://automationpanda.com/2020/07/07/arrange-act-assert-a-pattern-for-writing-good-tests/) 좀 더 기술적/구현 중심 - **Arrange** (준비) - **Act** (실행) - **Assert** (검증) ### Given When Then (GWT) [Given When Then](https://martinfowler.com/bliki/GivenWhenThen.html) behaviour-driven development (BDD) 에서 유래된 패턴 좀 더 비즈니스/행동 중심 - **Given** (주어진 상황) - **When** (동작 실행 (이렇게 했을 때)) - **Then** (결과 검증 (그러면 이렇게 된다)) ## 관련 문서 - [[Jest Matcher]] - [[Jest Cleanup]]