[Jest] Assertion Matchers 활용
글 작성자: 망고좋아
반응형
🎯 Assertion Matchers 활용
- toBe() : 객체의 내용을 비교
- toEqual() : 객체 자체를 비교할
- toContain()
- toMatch()
- toThrow()
function isPythagorean(a, b, c) {
return a * a + b * b === c * c
}
function createTodo(id, title, content) {
return { id, title, content }
}
function transformUser(user) {
const { name, age, address } = user
return [name, age, address]
}
📝 toBe()
test('Should 3, 4, 5 pythagorean', () => {
expect(isPythagorean(3, 4, 5)).toBe(true)
})
test('Should 3, 4, 6 not pythagorean', () => {
expect(isPythagorean(3, 4, 6)).toBe(false)
})
- toBe는 Object.is를 이용해서 테스트한다.
📝 toEqual()
test('Should create user', () => {
const id = 1, title = "Test todo", content = "Test content";
expect(createUser(id, title, content)).toEqual({ id, title, content })
})
- toEqual()는 deep equality를 체크를 담당한다.
📝 toMatch()
test('Should create user', () => {
const id = 1, title = "Test todo", content = "Test content";
expect(createUser(id, title, content)).title).toMatch("Test todo")
})
- createUser의 title이 "Test todo"와 같은지 아닌지 테스트하는 예제
📝 toContain()
test('Should contain name after transformUser', () => {
const user = { name: "test name", age: 20, address: "test address" }
expect(transformUser(user)).toContain("test name")
})
test('Should contain name after transformUser', () => {
const user = { name: "test name", age: 20, address: "test address" }
expect(transformUser(user)).not.toContain(30)
})
- 반환되는 값에 해당 값이 포함되어 있는지 체크
반응형
'프로그래밍 > testing' 카테고리의 다른 글
[react-testing-library] 유저 이벤트 (0) | 2022.01.17 |
---|---|
[react-testing-library] 쿼리의 우선순위 (0) | 2022.01.17 |
[react-testing-library] react-testing-library란? (0) | 2022.01.17 |
[Jest] Async assertion (0) | 2022.01.17 |
[jest] jest란? (0) | 2022.01.17 |
댓글
이 글 공유하기
다른 글
-
[react-testing-library] 쿼리의 우선순위
[react-testing-library] 쿼리의 우선순위
2022.01.17 -
[react-testing-library] react-testing-library란?
[react-testing-library] react-testing-library란?
2022.01.17 -
[Jest] Async assertion
[Jest] Async assertion
2022.01.17 -
[jest] jest란?
[jest] jest란?
2022.01.17