Jest中的钩子函数

在jest中,如果测试用例中需要使用到某个对象 或 在执行测试代码的某个时刻需要做一些必要的处理,直接在测试文件中写基础代码是不推荐的,可以使用jest的钩子函数。

钩子函数的作用:在代码执行的某个时刻,会自动运行的一个函数。

=> 常用的钩子函数

beforeAll
  • 在所有测试用例执行之前执行
beforeEach
  • 每个测试用例执行前执行,可让每个测试用例中使用的变量互不影响,因为分别为每个测试用例实例化了一个对象
afterAll
  • 等所有测试用例都执行之后执行 ,可以在测试用例执行结束时,做一些处理
afterEach
  • 每个测试用例执行结束时,做一些处理

=> describe测试用例分组

import Counter from './Counter'

// 使用类中的方法,首先要实例化
let counter = null

beforeEach(() => { // 每个测试用例执行前执行,可让每个测试用例中使用的变量互不影响,因为分别为每个测试用例实例化了一个对象
	counter = new Counter()
})

// 测试用例分组,让测试代码更清晰
describe('Counter 的测试代码', () => {
	describe('Counter 中的加法测试代码', () => {
		test('测试 Counter 中的 addOne 方法', () => {
			counter.addOne()
			expect(counter.number).toBe(1)
		})
		
		test('测试 Counter 中的 addTwo 方法', () => {
			counter.addTwo()
			expect(counter.number).toBe(2)
		})
	})
	
	describe('Counter 中的减法测试代码', () => {
		test('测试 Counter 中的 minusOne 方法', () => {
			counter.minusOne()
			expect(counter.number).toBe(-1)
		})
	
		test('测试 Counter 中的 minusTwo 方法', () => {
			counter.minusTwo()
			expect(counter.number).toBe(-2)
		})
	})	
})

执行npm test:测试结果缩进显示,清晰易读。
在这里插入图片描述

=> 钩子函数的作用域

钩子函数的作用域为: 所在的describe分组;
例子:

import Counter from './Counter'

// 使用类中的方法,首先要实例化
let counter = null
beforeAll(() => {
	console.log('外部的 beforeAll 执行')
})

beforeEach(() => { 
	counter = new Counter()
	console.log('外部的 beforeEach 执行')
})

describe('Counter 的测试代码', () => {
	describe('Counter 中的加法测试代码', () => {
		beforeAll(() => {
			console.log('内部的 beforeAll 执行')
		})
		test('测试 Counter 中的 addOne 方法', () => {
			counter.addOne()
			expect(counter.number).toBe(1)
		})
		
		test('测试 Counter 中的 addTwo 方法', () => {
			counter.addTwo()
			expect(counter.number).toBe(2)
		})
	})
	
	describe('Counter 中的减法测试代码', () => {
		test('测试 Counter 中的 minusOne 方法', () => {
			counter.minusOne()
			expect(counter.number).toBe(-1)
		})
	
		test('测试 Counter 中的 minusTwo 方法', () => {
			counter.minusTwo()
			expect(counter.number).toBe(-2)
		})
	})	
})

打印结果:

由打印结果可知,写在内层describe中的beforeAll钩子函数只作用了当前的分组,所以可得:钩子函数的作用域为自身所在的describe分组。

=> test.only的使用

当我们的测试用例非常多时,也可以只对单个测试用例进行调试,使用test.only即可。

import Counter from './Counter'

// 使用类中的方法,首先要实例化
let counter = null
beforeAll(() => {
	console.log('外部的 beforeAll 执行')
})

beforeEach(() => {
	counter = new Counter()
	console.log('外部的 beforeEach 执行')
})

describe('Counter 的测试代码', () => {
	describe('Counter 中的加法测试代码', () => {
		beforeAll(() => {
			console.log('内部的 beforeAll 执行')
		})
		test.only('测试 Counter 中的 addOne 方法', () => {
			counter.addOne()
			expect(counter.number).toBe(1)
		})
		
		test('测试 Counter 中的 addTwo 方法', () => {
			counter.addTwo()
			expect(counter.number).toBe(2)
		})
	})
	
	describe('Counter 中的减法测试代码', () => {
		test('测试 Counter 中的 minusOne 方法', () => {
			counter.minusOne()
			expect(counter.number).toBe(-1)
		})
	
		test('测试 Counter 中的 minusTwo 方法', () => {
			counter.minusTwo()
			expect(counter.number).toBe(-2)
		})
	})	
})

执行npm test:
在这里插入图片描述
当使用test.only时,只有当前测试用例被执行,其他用例均不执行。

=> describe中的基础代码执行顺序

import Counter from './Counter'

// 使用类中的方法,首先要实例化
let counter = null
beforeEach(() => {
	counter = new Counter()
	console.log('外部的 beforeEach 执行')
})

describe('Counter 的测试代码', () => {
	console.log('describe 11111')
	describe('Counter 中的加法测试代码', () => {
		console.log('describe 22222')
		beforeAll(() => {
			console.log('内部的 beforeAll 执行')
		})
		test.only('测试 Counter 中的 addOne 方法', () => {
			counter.addOne()
			expect(counter.number).toBe(1)
		})
		
		test('测试 Counter 中的 addTwo 方法', () => {
			counter.addTwo()
			expect(counter.number).toBe(2)
		})
	})
	
	describe('Counter 中的减法测试代码', () => {
		console.log('describe 33333')
		test('测试 Counter 中的 minusOne 方法', () => {
			counter.minusOne()
			expect(counter.number).toBe(-1)
		})
	
		test('测试 Counter 中的 minusTwo 方法', () => {
			counter.minusTwo()
			expect(counter.number).toBe(-2)
		})
	})	
})

执行npm test:

由打印结果可以看出,describe中的基础代码并没有按照我们的意愿去执行,而是最先执行了,所以当我们在写测试代码的基础代码时,一定要在钩子函数内完成。

发布了54 篇原创文章 · 获赞 22 · 访问量 7223

猜你喜欢

转载自blog.csdn.net/Riona_cheng/article/details/102296427