beforeEach
혹은 afterEach
훅을 사용할수 있다.initializeCityDatabase()
함수가 있고, 모든 함수가 호출 된 이후에 실행되어야 하는 clearCityDatabase()
함수가 있다고 가정해보자.beforeEach(() => {
initializeCityDatabase();
});
afterEach(() => {
clearCityDatabase();
});
test('city databse has Vienna', ()=>{
expect(isCity('Vienna')).toBeTruthy();
});
test('city databse has Seoul', ()=>{
expect(isCity('Seoul')).toBeTruthy();
});
beforeEach
와 afterEach
는 비동기 코드도 같은 방법으로 핸들할 수 있다. 둘다 done
을 가질 수 있고 혹은 promise를 return 할 수도 있다.
beforeEach(() => {
return initalizeCityDatabase();
});
initializeCityDatabse()
와 clearCityDatabase()
가 promises를 리턴한다면, city db는 테스트들 사이에 계속 재사용 될수 있다.beforeAll(() => {
return initializeCityDatabse();
});
afterAll(() => {
return clearCityDatabase();
});
test('city database has Vienna', () => {
expect(isCity('Vienna')).toBeTruthy();
});
test('city database has Seoul', () => {
expect(isCity('Seoul')).toBeTruthy();
});
beforeAll
과 afterAll
blocks은 해당 파일의 모든 테스트에 적용이 된다. describe
를 사용하여 테스트들을 함께 그룹 지을 수 있다.describe
block 안에 있다면, beforeAll
과 afterAll
은 describe
block 안에 있는 테스들에만 적용이 된다.beforeEach(() => {
return initializeCityDatabase();
});
test('city db has Vienna', () => {
expect(isCity('Vienna')).toBeTruthy();
});
test('city db has Seoul', () => {
expect(isCity('Seoul')).toBeTruthy();
});
describe('matching cities to foods', () => {
beforeEach(() => {
return initializeFoodDatabase();
});
test('Vienna <3 veal', () => {
expect(isValidCityFoodPair('Vienna', 'Wiener Schnitzel')).toBe(true));
});
test('Seoul is famous for Sushi', () => {
expect(isValidCityFoodPair('Seoul', 'Sushi')).toBe('false');
});
});
beforeEach
는 describe
block 안의 beforeEach
보다 이전에 실행이 된다.beforeAll(() => console.log('1 - beforeAll'));
afterAll(() => console.log('1 - afterAll'));
beforeEach(() => console.log('1 - beforeEach'));
afterEach(() => console.log('1 - afterEach'));
test('', () => console.log('1 - test'));
describe('Scoped / Nested block', () => {
beforeAll(() => console.log('2 - beforeAll'));
afterAll(() => console.log('2 - afterAll'));
beforeEach(() => console.log('2 - beforeEach'));
afterEach(() => console.log('2 - afterEach'));
test('', () => console.log('2 - test'));
});
//1 - beforeAll
//1 - beforeEach
//1 - test
//1 - afterEach
//2 - beforeAll
//1 - beforeEach
//2 - beforeEach
//2 - test
//2 - afterEach
//1 - afterEach
//2 - afterAll
//1 - afterAll
describe
를 먼저 실행한다.describe
blocks 들이 완료되면, 기본적으로 Jest는 모든 테스트를 순서대로 실행한다.describe('describe outer', () => {
console.log('describe outer-a');
describe('describe inner 1', () => {
console.log('describe inner 1');
test('test 1', () => console.log('test 1'));
});
console.log('describe outer-b');
test('test 2', () => console.log('test 2'));
describe('describe inner 2', () => {
console.log('describe inner 2');
test('test 3', () => console.log('test 3'));
});
console.log('describe outer-c');
});
// describe outer-a
// describe inner 1
// describe outer-b
// describe inner 2
// describe outer-c
// test 1
// test 2
// test 3
test
를 test.only
로 변경해서 확인해보자test.only('this will be the only test that runs', () => {
expect(true).toBe(false);
});
test('this test will not run', () => {
expect('A').toBe('A');
});
beforeEac
함수를 통해서 해당 테스트 실행 전의 상태를 확인하는 것도 좋은 디버깅 방법 중 하나이다.references: https://jestjs.io/docs/setup-teardown