06|Jest中的钩子函数

2020-05-22  本文已影响0人  雪燃归来

Counter.js

export class Counter{
    constructor(){
        this.number = 0
    }
    addOne(){
        this.number += 1
    }
    addTwo(){
        this.number += 2
    }
    minuOne(){
        this.number -= 1
    }
    minuTwo(){
        this.number -= 2
    }
}

Counter.test.js

import { Counter } from './Counter'

describe('测试Counter的方法', () => {
    let counter = null;
    beforeAll(() => {
        console.log('所有测试实例运行之前执行')
    })
    afterAll(() => {
        console.log('所有实例运行之后执行')
    })
    beforeEach(() => {
        counter = new Counter()
        console.log('每个测试实例运行之前执行')
    })
    afterEach(() => {
        console.log('每个测试实例运行之后执行')
    })

    describe('测试加法相关的方法', () => {
        test('测试 Counter中的 addOne方法',() => {
            counter.addOne();
            expect(counter.number).toBe(1)
        })
        test('测试 Counter中的 addTwo方法',() => {
            counter.addTwo();
            expect(counter.number).toBe(2)
        })
    });

    describe('测试减法相关的方法', () => {
        test('测试 Counter中的 minuOne方法', () => {
            counter.minuOne()
            expect(counter.number).toBe(-1)
        })
        test('测试 Counter中的 minuOne方法', () => {
            counter.minuTwo()
            expect(counter.number).toBe(-2)
        })
    });
});
上一篇 下一篇

猜你喜欢

热点阅读