Jest에서는 "Matchers"를 사용해 다양한 방법으로 테스트합니다.
Jest의 기본 구조를 알아보고 Jest에 있는 "Matchers"에 대해 알아 봅시다.
Jest는 describe함수와 test함수를 통해 테스트 코드를 구조화하고 조직화 합니다.
describe
테스트를 그룹화 하는데 사용합니다.
관련된 테스트 케이스들을 논리적으로 묶을 수 있습니다.
공통된 설정과 정리 코드를 공유할 수 있습니다.
test
실제 테스트 케이스를 정의하는데 사용됩니다.
it함수로 대신해서 쓸 수 있습니다.
test는 하나의 기능이나 동작을 검증하는데 사용됩니다.
아래는 describe와 test를 같이 쓴 예제입니다.
describe로 논리적인 테스트 케이스를 만들고
describe안에 하나의 테스트인 test를 정의하고 있습니다.
describe('Math operations', () => {
test('adds 1 + 2 to equal 3', () => {
expect(1 + 2).toBe(3);
});
test('subtracts 5 - 2 to equal 3', () => {
expect(5 - 2).toBe(3);
});
});
expect함수는 로직이나 함수를 실행해 테스트합니다.
아래는 expect의 예시입니다.
test('two plus two is four', () => {
expect(2 + 2).toBe(4);
});
expect함수는 expectation 객체를 반환합니다.
expectation객체는 많은 Matcher 함수를 가지고 있습니다.
expect함수가 테스트한 값을 확인합니다.
아래는 expect함수를 사용한 예제입니다.
test('two plus two is four', () => {
expect(2 + 2).toBe(4);
});
expect함수가 테스트한 값을 확인합니다.
객체의 값도 확인 가능합니다.
test('object assignment', () => {
const data = {one: 1};
data['two'] = 2;
expect(data).toEqual({one: 1, two: 2});
});
expect함수가 테스한 값이 Null인지 확인합니다.
값이 존재하는지 학인합니다.
즉, 값이 Undefined가 아닌지 확인합니다.
값이 Undefined인지 확인합니다.
값이 참 값인지 확인합니다.
값이 거짓 값인지 확인합니다.
test('null', () => {
const n = null;
expect(n).toBeNull();
expect(n).toBeDefined();
expect(n).not.toBeUndefined();
expect(n).not.toBeTruthy();
expect(n).toBeFalsy();
});
test('zero', () => {
const z = 0;
expect(z).not.toBeNull();
expect(z).toBeDefined();
expect(z).not.toBeUndefined();
expect(z).not.toBeTruthy();
expect(z).toBeFalsy();
});
특정 값보다 큰지 확인하기 위해 사용합니다.
특정 값보다 크거나 같은지 확인하기 위해 사용됩니다.
특정 값보다 작은지 확인하기 위해 사용됩니다.
특정 값보다 작거나 같은지 확인하기 위해 사용됩니다.
test('two plus two', () => {
const value = 2 + 2;
expect(value).toBeGreaterThan(3);
expect(value).toBeGreaterThanOrEqual(3.5);
expect(value).toBeLessThan(5);
expect(value).toBeLessThanOrEqual(4.5);
// toBe and toEqual are equivalent for numbers
expect(value).toBe(4);
expect(value).toEqual(4);
});
Float 값을 확인하기 위한 용도로 사용합니다.
test('adding floating point numbers', () => {
const value = 0.1 + 0.2;
//expect(value).toBe(0.3); This won't work because of rounding error
expect(value).toBeCloseTo(0.3); // This works.
});
특정 정규표현식과 매칭되는지 확인합니다.
test('there is no I in team', () => {
expect('team').not.toMatch(/I/);
});
test('but there is a "stop" in Christoph', () => {
expect('Christoph').toMatch(/stop/);
});
배열이나 이터러블이 특정 값을 포함하는지 확인합니다.
const shoppingList = [
'diapers',
'kleenex',
'trash bags',
'paper towels',
'milk',
];
test('the shopping list has milk on it', () => {
expect(shoppingList).toContain('milk');
expect(new Set(shoppingList)).toContain('milk');
});
예외가 던져졌는지 또는 예외가 던져졌을 때 메세지가 매칭되는지 확인합니다.
코드를 입력하세요
throw new Error('you are using the wrong JDK!');
}
test('compiling android goes as expected', () => {
expect(() => compileAndroidCode()).toThrow();
expect(() => compileAndroidCode()).toThrow(Error);
// You can also use a string that must be contained in the error message or a regexp
expect(() => compileAndroidCode()).toThrow('you are using the wrong JDK');
expect(() => compileAndroidCode()).toThrow(/JDK/);
// Or you can match an exact error message using a regexp like below
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK$/); // Test fails
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK!$/); // Test pass
});