글 작성자: 망고좋아
반응형

🎯 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)
})
  • 반환되는 값에 해당 값이 포함되어 있는지 체크
반응형