Globals Api
Api | 主要用途 | 其它 |
---|---|---|
describe | 一个describe为一组,不推荐describe嵌套 | describe(name, fn) |
test(it) | 一个describe可以有多个it,最常用的组合方式 | test(name, fn, timeout) |
afterAll | 清理一些跨测试共享的全局设置状态 | 在所有test执行完毕后执行一次 |
afterEach | 清理每个测试创建的一些临时状态 | 在每一个test执行完后都会执行 |
beforeAll | 设置一些测试使用的全局状态 | 在所有test执行之前执行一次 |
beforeEach | 重置一些被测试使用的全局状态 | 在每一个test执行前都会执行 |
test.each | 优化重复使用不同的数据,但结构相同的测试 | 同describe.each用法一样 |
describe.each | 优化重复使用不同的数据,但结构相同的测试 | 接受一个二维数组或者数组对象(例子) |
describe.only | 只运行一个describe | 一般不用,出于某些特殊原因,只运行主要describe |
describe.only.each | only和each的结合 | 无 |
describe.skip | 跳过当前test | 通常是临时注释掉一大块测试的更干净的替代方法 |
describe.skip.each | skip和each的结合 | 无 |
test.concurrent | 多个concurrent测试同时运行 | 无 |
test.concurrent.only.each | concurrent和only和each的结合 | 无 |
test.only | 出于某些特殊原因,只运行主要test | 只运行一个test |
test.only.each | only和each的结合 | 无 |
test.skip | 跳过当前test | 无 |
test.skip.each | skip和each的结合 | 无 |
test.todo | 加回调会报错,只给一个参数,用于编写测试计划 | 无 |
Expect Api
Api | 主要用途 | 其它 |
---|---|---|
expect | 测试值时都会使用该函数 | expect与匹配器 组合起来断言 |
expect.extend | 扩展自定义匹配器 | 支持快照支持异步 |
expect.anything | 匹配除null或undefined之外的任何内容 | null或undefined会报错 |
expect.any | 通过类型去断言 | 类型错误将报错 |
expect.arrayContaining | 支持匹配数组中的部分元素 | 被断言的数组参数可以多不可以少 |
expect.stringContaining | 支持匹配字符串中的部分字符 | 被断言的字符可以多不可以少 |
expect.assertions | 验证在测试期间调用了一定数量的断言 | 是异步的 |
expect.hasAssertions | 验证在测试期间至少调用了一个断言 | 是异步的 |
expect.objectContaining | 验证中对象是否包含某项属性 | 无 |
.toBeInstanceOf | 断言对象是一个类的实例 | 无 |
.not | 取反 | 无 |
.toBe | 严格相等 | 无 |
.toEqual | 值相同即可 | 无 |
.toBeFalsy | 断言为假 | 无 |
.toBeTruthy | 断言为true | 无 |
.toBeNull | 断言为null | 无 |
.toBeUndefined | 断言为undefined | 无 |
.toBeNaN | 断言为NaN | 无 |
.toContain | 断言数组或字符串中是否包含 | 严格相等 |
.toContainEqual | 断言数组或字符串中是否包含 | 值相同即可 |
.toMatch | 断言字符串 | 支持正则 |
.toThrow | 断言抛出错误 | 无 |
数字相关
Api | 主要用途 | 其它 |
---|---|---|
.toBeGreaterThan | 断言大于 | 无 |
.toBeGreaterThanOrEqual | 断言大于等于 | 无 |
.toBeLessThan | 断言小于 | 无 |
.toBeLessThanOrEqual | 断言小于等于 | 无 |
.toBeCloseTo | 断言浮点数 | 解决0.1+0.2 !== 0.3的问题 |
函数相关
Api | 主要用途 | 其它 |
---|---|---|
.toBeCalled | 函数将被调用 | 别名:toHaveBeenCalled |
.toHaveBeenCalledTimes | 断言函数调用的次数 | 可配合toBe等其它的匹配器 |
.toHaveBeenCalledWith | 断言函数调用的参数 | 别名:toBeCalledWith |
.toHaveBeenLastCalledWith | 断言指定函数指定第几次调用的参数 | 别名:lastCalledWith |
.toHaveBeenNthCalledWith | 断言最后一次函数调用的参数 | 别名:nthCalledWith |
.toHaveReturned | 断言函数有返回值 | 别名:toReturn |
.toHaveReturnedTimes | 断言函数返回次数 | 别名:toReturnTimes |
.toHaveReturnedWith | 断言函数具体的返回值 | 别名:toReturnWith |
.toHaveLastReturnedWith | 断言函数最后一次的返回值 | 别名:lastReturnedWith |
.toHaveNthReturnedWith | 断言函数指定次数的返回值 | 别名:nthReturnedWith |
.toHaveLength | 断言数组或者字符串的长度 | 无 |
.toHaveProperty | 断言对象中是否包含某项属性 | 支持对象参数路径,断言指定的值 |
.resolves | 断言Promise成功回调 | 可配合toBe等其它的匹配器 |
.rejects | 断言Promise失败的回调 | 可配合toBe等其它的匹配器 |
mock 函数
mockName
指定mock函数的名字,这在报错的时候非常有用
const myMockFn = jest.fn().mockName('myMockFn');
mockFn.mock.results
指定函数返回值
fn.mock.results = [
{
type: 'throw',
value: {
/* Error instance */
},
},
{
type: 'return',
value: 'result2',
},
];
//期待最后一次返回结果为'result2'
expect(fn).toHaveLastReturnedWith( 'result2')
mockFn.mockClear
重置存储在mockFn.mock.calls
,mockFn.mock.instances
和mockFn.mock.results
数组中的所有信息。
fn.mock.results = [
{
type: 'return',
value: 'result2',
},
];
fn.mockClear()
//会报错,因为清空了mock
expect(fn).toHaveLastReturnedWith( 'result2')
mockFn.mockReset
执行所有mockFn.mockClear()
操作,并删除任何模拟的返回值或实现
const fn = jest.fn()
fn.mock.results = [
{
type: 'return',
value: 'result2',
},
];
fn.mockReset()
//完全重置回其初始状态时jest.fn()
expect(fn).not.toReturn()
mockFn.mockImplementation
const mockFn = jest.fn().mockImplementation(scalar => 42 + scalar);
// or: jest.fn(scalar => 42 + scalar);
const a = mockFn(0);
const b = mockFn(1);
a === 42; // true
b === 43; // true
mockFn.mock.calls[0][0] === 0; // true
mockFn.mock.calls[1][0] === 1; // true
mockFn.mockReturnValue
指定返回值
const mock = jest.fn();
mock.mockReturnValue(42);
mock()
expect(mock).toReturnWith(42)
//下面三种方式都可以达到上面的效果
//const mock = jest.fn(()=>42);
//use mockImplementation
//const mock = jest.fn().mockImplementation(()=>42);
//mock.mock.results = [{type:'return',value:42}]
mockFn.mockResolvedValue
模拟成功的回调
test('async test', async () => {
//const asyncMock = jest.fn(()=> Promise.resolve(42));
const asyncMock = jest.fn().mockResolvedValue(43);
await asyncMock(); // 43
});
mockFn.mockRejectedValue
模拟失败的回调
test('async test', async () => {
//const asyncMock = jest.fn(()=> Promise.reject(42));
const asyncMock = jest.fn().mockRejectedValue(43);
await asyncMock(); // 43
});
mock Modules
模拟axios
//模拟模块,可以搭配mockResolvedValue和Implementations等api使用
import axios from 'axios';
jest.mock('axios');
//模拟一个文件中部分函数
jest.mock('path', () => {
const originalModule = jest.requireActual('path');
return {
__esModule: true,
...originalModule,
functionName: jest.fn(() => true),
};
});