4、Jest四个钩子函数、测试用例分组和钩子函数作用域

442 阅读3分钟

一、Jest四个钩子函数

beforeAll()

beforeAll()钩子函数的意思是在所有测试用例之前进行执行。就在昨天的代码上进行测试吧,如下:

import {asyncTest1,asyncTest2,asyncTest3,asyncTest4} from './asyncTest'
//beforeAll()在所有测试用例之前进行执行
beforeAll(()=>{
    console.log('查询1888****966号码归属地')
})

test('asyncTest4 归属地查询', async()=>{
    const response  = await asyncTest4('1888****966')
    expect(response.data).toEqual({
        "province": "海南",
        "city": "海口",
        "postcode": "570000",
        "areacode": "0898",
        "sp": "移动",
        "phone": "1888****966"
    })
})

image.png

afterAll()

afterAll()钩子函数是在完成所有测试用例之后才执行的函数。

afterAll(()=>{
    console.log('归属地查询完成')
})

beforeEach()

beforeEach()钩子函数,是在每个测试用例前都会执行一次的钩子函数。

beforeEach(()=>{
    console.log('开始查询归属地信息!!!')
})

afterEach()

afterEach()钩子函数,是在每次测试用例完成测试之后执行一次的钩子函数。

afterEach(()=>{
    console.log('查询结束!!!')
})

二、测试用例分组

Jest为我们提供了一个分组的语法describe(),这个方法接受两个参数,如下:

// asyncTest1和asyncTest2为一组
describe('asyncTest1和asyncTest2',()=>{
    test('asyncTest1 回调函数式测试',(done)=>{
        asyncTest1((data)=>{
            expect(data).toEqual({
                "province": "海南",
                "city": "海口",
                "postcode": "570000",
                "areacode": "0898",
                "sp": "移动",
                "phone": "1888****966"
            })
     
        },"1888****966")
        done() //加入一个done方法,保证我们的回调已经完成了,这时候我们表示测试完成。
       })
    
    test('asyncTest2 直接返回promise',()=>{
       return asyncTest2('1888****966').then((res)=>{
            expect(res.data).toEqual({
                "province": "海南",
                "city": "海口",
                "postcode": "570000",
                "areacode": "0898",
                "sp": "移动",
                "phone": "1888****966"
            })
        })
    })
    
})


// asyncTest3和asyncTest4为一组
describe('asyncTest3和asyncTest4',()=>{
    test('asyncTest3 404测试', ()=>{
        // expect.assertions(1)  // 断言,必须执行一次expect
        return asyncTest3('1888****966').catch((e)=>{
          expect(e.toString().indexOf('404')> -1).toBe(true)
        })
    })
    test('asyncTest4 async...await测试', async()=>{
        const response  = await asyncTest4('1888****966')
        expect(response.data).toEqual({
            "province": "海南",
            "city": "海口",
            "postcode": "570000",
            "areacode": "0898",
            "sp": "移动",
            "phone": "1888****966"
        })
    })
})

image.png

三、钩子函数作用域

Jest中钩子函数的作用域有下面三个特色。

  • 钩子函数在父级分组可作用域子集,类似继承
  • 钩子函数同级分组作用域互不干扰,各起作用
  • 先执行外部的钩子函数,再执行内部的钩子函数

Jest默认最外层有一个describe(),隐藏未显示。

describe('父级',()=>{

    describe('asyncTest1和asyncTest2子集分组',()=>{
        test('asyncTest1 回调函数式测试',(done)=>{
            asyncTest1((data)=>{
                expect(data).toEqual({
                    ....
                })
        
            },"***")
        
            done() //加入一个done方法,保证我们的回调已经完成了,这时候我们表示测试完成。
        
        })
        
        test('asyncTest2 直接返回promise',()=>{
        return asyncTest2('***').then((res)=>{
                expect(res.data).toEqual({
                    ....
                })
            })
        })
        
    })
    // asyncTest3和asyncTest4为一组
    describe('asyncTest3和asyncTest4子集分组',()=>{
        test('asyncTest3 404测试', ()=>{
            // expect.assertions(1)  // 断言,必须执行一次expect
            return asyncTest3('***').catch((e)=>{
            expect(e.toString().indexOf('404')> -1).toBe(true)
            })
        })
        test('asyncTest4 async...await测试', async()=>{
            const response  = await asyncTest4('***')
            expect(response.data).toEqual({
               ....
            })
        })
    })

})

这时候,在父级中添加钩子函数,其也会作用到子集上面。如果两个子集同级中添加各自的钩子函数,是互不影响,各起作用的。钩子函数执行顺序是,先执行外部的钩子函数,再执行内部的钩子函数。

四、补充:only的使用

test.only('asyncTest3 404测试', ()=>{
    // expect.assertions(1)  // 断言,必须执行一次expect
    return asyncTest3('18889999966').catch((e)=>{
    expect(e.toString().indexOf('404')> -1).toBe(true)
    })
})

如果想单独测试某个用例,只需要使用only,其它的用例都会skipped掉,只执行这一个。

image.png

学习日期:2022/1/21

视频参考www.bilibili.com/video/BV1yA…

文档参考jspang.com/detailed?id…

仅供个人学习和记录