JEST - globalAPI/Matcher

유원근·2021년 1월 4일
0

Jest

목록 보기
1/1
  • afterAll(fn, timeout)
  • afterEach(fn, timeout)
  • beforeAll(fn, timeout)
  • beforeEach(fn, timeout)
  • describe(name, fn)
  • describe.each(table)(name, fn, timeout)
  • describe.only(name, fn)
  • describe.only.each(table)(name, fn)
  • describe.skip(name, fn)
  • describe.skip.each(table)(name, fn)
  • test(name, fn, timeout)
  • test.concurrent(name, fn, timeout)
  • test.concurrent.each(table)(name, fn, timeout)
  • test.concurrent.only.each(table)(name, fn)
  • test.concurrent.skip.each(table)(name, fn)
  • test.each(table)(name, fn, timeout)
  • test.only(name, fn, timeout)
  • test.only.each(table)(name, fn)
  • test.skip(name, fn)
  • test.skip.each(table)(name, fn)
  • test.todo(name)

위 메소드들을 그룹화시키면 아래와 같이 나눌 수 있습니다.

테스트 후 상태를 지정

  • afterAll(fn, timeout) 모든 테스트를 마친뒤 한번
  • afterEach(fn, timeout) 모든 테스트 뒤에 한번씩

테스트 전 상태를 지정

  • beforeAll(fn, timeout)
  • beforeEach(fn, timeout)
describe('addOne', () => {
  beforeAll(() => {
    console.log('beforeAll동작')
  }) // 모든테스트의 전단계에서
  afterAll(() => {
    console.log('afterAll동작')
  }) // 모든테스트를 마친뒤
  beforeEach(() => {
    console.log('beforeEach동작')
  })
  afterEach(() => {
    console.log('afterEach동작')
  })
  test('1', () => {
    console.log('테스트1')
  })
  test('2', () => {
    console.log('테스트2')
  })
})
beforeAll동작
beforeEach동작
테스트1
afterEach동작
beforeEach동작
테스트2
afterEach동작
afterAll동작

테스트들을 그룹화 할때

  • describe(name, fn)
  • describe.each(table)(name, fn, timeout)
  • describe.only(name, fn)
  • describe.only.each(table)(name, fn)
  • describe.skip(name, fn)
  • describe.skip.each(table)(name, fn)
describe('addOne', () => {
  test('인수가 숫자인 경우', () => {
    expect(addOne(2)).toBe(3)
  })

  test('인수가 문자인 경우', () => {
    expect(addOne('2')).toBe(3)
    expect(addOne('77')).toBe(78)
  })
})

addOne이라는 이름으로 두개의 테스트를 묶음

테스트

  • test(name, fn, timeout)
  • test.concurrent(name, fn, timeout)
  • test.concurrent.each(table)(name, fn, timeout)
  • test.concurrent.only.each(table)(name, fn)
  • test.concurrent.skip.each(table)(name, fn)
  • test.each(table)(name, fn, timeout)
  • test.only(name, fn, timeout)
  • test.only.each(table)(name, fn)
  • test.skip(name, fn)
  • test.skip.each(table)(name, fn)
  • test.todo(name)

matchers

한글로 일치자라고 표현할 수 있으며 실제값이 기대하는 값과 일치하는지를 확인합니다.

test('two plus two is four', () => {
  expect(2 + 2).toBe(4);
});

매우 많은 matchers가 있는데, 몇가지만 알아보겠습니다.

expect(value)

실제값을 가지고 matcher를 붙이는 역할을 합니다.
value부분에 기대하는 실제값을 넣어줍니다.

toBe(value)

가장 많이 사용하는 matcher입니다.
기본적으로 원시데이터를 비교하는 역할을 하는데 다음과 같이 사용할 수 있습니다.

const can = {
  name: 'pamplemousse',
  ounces: 12,
};

describe('the can', () => {
  test('has 12 ounces', () => {
    expect(can.ounces).toBe(12);
  });

  test('has a sophisticated name', () => {
    expect(can.name).toBe('pamplemousse');
  });
});

toBe()안에 들어있는 인자와 결과값이 같을경우 테스트에 통과합니다.

.toEqual(value)

toBe()가 원시데이터를 비교할때 사용했다면, toEqual은 객체데이터를 비교할 때 사용합니다.

const can1 = {
  flavor: 'grapefruit',
  ounces: 12,
};
const can2 = {
  flavor: 'grapefruit',
  ounces: 12,
};

describe('the La Croix cans on my desk', () => {
  test('have all the same properties', () => {
    expect(can1).toEqual(can2);
  });
  test('are not the exact same can', () => {
    expect(can1).not.toBe(can2);
  });
});

0개의 댓글