Jest 사용법 - Setup and Teardown

Jin·2022년 7월 18일
0

jest

목록 보기
3/4
post-thumbnail

1. Repeating Setup

  • 만약 만은 테스트를 위해 반복적으로 할 작업이 있다면, 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();
});

beforeEachafterEach는 비동기 코드도 같은 방법으로 핸들할 수 있다. 둘다 done 을 가질 수 있고 혹은 promise를 return 할 수도 있다.

beforeEach(() => {
  return initalizeCityDatabase();
});

2. One-Time Setup

  • file 제일 처음에 한번에 setup이 필요한 경우가 있을 수 있다.
  • 만약 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();
});

3. Scoping

  • 기본적으로, beforeAllafterAll blocks은 해당 파일의 모든 테스트에 적용이 된다.
  • describe를 사용하여 테스트들을 함께 그룹 지을 수 있다.
  • 만약 테스트들이 describe block 안에 있다면, beforeAllafterAlldescribe 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');
  });
});
  • 제일 위의 beforeEachdescribe 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

3. Order of Execution

  • Jest는 실제 test를 실행하기 이전에 test file의 모든 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

4. General Advice

  • 만약 테스트가 실패하면, 제일 먼저 확인해봐야하는것은 실패하는 테스트만 개별로 실행 했을때에도 실패하는지 확인 해봐야한다.
  • 임시로 testtest.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

profile
내가 다시 볼려고 작성하는 블로그. 아직 열심히 공부중입니다 잘못된 내용이 있으면 댓글로 지적 부탁드립니다.

0개의 댓글