const employee = {
makeChange: function(bill, price) {
return bill - price;
}
};
module.exports = employee;
tobe
test('Check the result of + ', () => {
//整數可以使用 toBe 或 toEqual 斷言
expect(1 + 1).toBe(2)
expect(1 + 2.toBe(8)
expect(2).toEqual(2)
//測試輸出值是否大於期望值
expect(5).toBeGreaterThan(4)
//測試輸出值是否大於等於期望值
expect(5).toBeGreaterThanOrEqual(5)
//測試輸出值是否小於期望值
expect(5).toBeLessThan(6)
//測試輸出值是否小於期望值
expect(5).toBeLessThanOrEqual(5)
//會忽略些微的誤差
expect(0.1 + 0.2).toBeCloseTo(0.3)
//需完全相等
expect(0.1 + 0.2).toBe(0.3)
})
type
test('Special value',()=>{
//期望值為 true
expect(true).toBeTruthy()
//期望值為 false
expect(false).toBeFalsy()
//期望值為 null
expect(null).toBeNull()
//期望值為 undefined
expect(undefined).toBeUndefined()
//期望值為 undefined 之外的值
expect(null).toBeDefined()
})
toContain
test('For array test in jest',()=>{
let arrA = ['A','B','C']
//檢查陣列內是否含有某值
expect(arrA).toContain('B')
//搭配迴圈檢查每個位置都不等於空
for(let i in arrA){
expect(arrA[i]).not.toBe('')
}
})
toEqual
test('Check the object type', () => {
let peopleA = {
name: 'tim'
}
peopleA.age = 25
//測試字串
expect(peopleA.name).toBe('tim')
//測試物件
expect(peopleA).toEqual({ name: 'tim', age: 25 })
//測試物件
expect(peopleA).toEqual({ name: 'joy', age: 27 })
//預防函式回傳某個結果可以使用 not
//確認 name 不等於空
expect(peopleA.name).not.toBe('')
peopleA.name = ''
//如果 name 是空的
expect(peopleA.name).not.toBe('')
})
toMatch_rugex.test
test('Use toMatch test',()=>{
//搭配正規表達式
expect('https://www.ec.co.jp/prod/1069433750').toMatch(/prod/)
expect('https://www.ec.co.jp/p/1069433750').toMatch(/prod/)
}