บทความนี้สรุปการใช้งาน expect matcher ที่มีให้ใช้ใน jest โดยเราสามารถเข้าไปดูเพิ่มเติมได้ที่ https://jestjs.io/docs/expect
Truthiness
ถ้าเรามีการตรวจสอบค่าว่าเป็น undefined
, null
หรือ false
ใน jest มี helpers ที่ช่วยให้เราตรวจสอบค่าต่างๆ เหล่านี้ได้
toBeNull
เป็นnull
เท่านั้นtoBeUndefined
เป็นundefined
เท่านั้นtoBeDefined
เป็นสิ่งที่ตรงกันข้ามกับtoBeUndefinition
toBeTruthy
จับคู่ทุกสิ่งที่คำสั่งif
ถือว่าเป็นจริงtoBeFalsy
จับคู่อะไรก็ตามที่คำสั่งif
ถือว่าเป็นเท็จ
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()
})
truthiness
ถ้าเราใช้ .not
ไว้ข้างหน้าเท่ากับตรงกันข้าม
Numbers
วิธีเปรียบเทียบตัวเลขส่วนใหญ่จะมีค่าที่เทียบเท่ากัน
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)
})
number
ในกรณีที่อยากเปรียบเทียบค่า floating point ให้ใช้ useBeCloseTo
แทนการใช้ toEqual
หรือ toBe
เพื่อไม่ให้เกิดข้อผิดพลาดในการปัดเศษเล็กๆ น้อยๆ
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.
})
floating point
Strings
คุณสามารถตรวจสอบสตริงกับนิพจน์ทั่วไป ด้วย toMatch
:
describe('strings', () => {
test('there is no I in team', () => {
expect('team').not.toMatch(/I/)
})
test('but there is a "stop" in Christoph', () => {
expect('Christoph').toMatch(/stop/)
})
})
string
Arrays and iterables
เราสามารถตรวจสอบว่า array หรือ iterable (วนซ้ำ) ว่ามีข้อมูลที่ต้องการตรวจสอบมีหรือไม่ ให้ใช้ toContain
:
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')
})
array and iterables
Exceptions
หากเราต้องการทดสอบว่าฟังก์ชันใดฟังก์ชันหนึ่งเกิดข้อผิดพลาด เมื่อมีการเรียกใช้หรือไม่ ให้ใช้ toThrow
:
function compileAndroidCode() {
throw new Error('you are using the wrong JDK!')
}
test('compiling android goes as expected', () => {
expect(() => compileAndroidCode()).toThrow()
expect(() => compileAndroidCode()).toThrow(Error)
// ตรวจสอบข้อความที่ต้องการอยู่ในประโยคหรือไหม หรือจะใช้ regexp ก็ได้
expect(() => compileAndroidCode()).toThrow('you are using the wrong JDK')
expect(() => compileAndroidCode()).toThrow(/JDK/)
// หรือจับคู่ข้อความที่แสดงแบบตรงตัวโดยใช้ regexp
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK$/) // Test fails
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK!$/) // Test pass
})
การใช้งานเบื้องต้นจะประมาณนี้ เราสามารถเข้าไปดูวิธีการใช้เพิ่มเติมได้ที่: https://jestjs.io/docs/expect