文章目录
测试
test
test('name', () => {function} }
expect
() => {
expect(期望value).toBe(匹配value);
}
toThrow
测试某函数在调用时是否抛出了错误
() => {
expect(期望value).toThrow(Error);
}
async
Promise
测试fetchData()返回一个Promise
() => {
fetchData().then(
data => {
expect(data).toBe('peanut butter');
}
);
}
Async/Await
async () => {
const data = await fetchData();
expect(data).toBe('peanut butter');
}
async () => {
expect.assertions(1);
try {
await fetchData();
} catch (e) {
expect(e).toMatch('error');
}
}
作用域
一次性设置 beforeAlland
afterAll
每个文件前后执行
重复设置 beforeEach和
afterEach
每个test前后执行
//每个测试之前调用方法 initializeCityDatabase()
beforeEach(() => {
initializeCityDatabase();
});
//每个测试后,调用方法 clearCityDatabase()
afterEach(() => {
clearCityDatabase();
});
describe
块
describe('matching cities to foods', () => {
// Applies only to tests in this describe block
beforeEach(() => {
return initializeFoodDatabase();
});
test('Vienna <3 veal', () => {
expect(isValidCityFoodPair('Vienna', 'Wiener Schnitzel')).toBe(true);
});
test('San Juan <3 plantains', () => {
expect(isValidCityFoodPair('San Juan', 'Mofongo')).toBe(true);
});
});
mock
cli
# 运行所有测试
jest
# 运行指定文件名称
jest path/to/my-test.js
jest --verbose --no-cache --coverage --detectOpenHandles
--verbose 选项会以详细模式显示测试结果
--no-cache 选项禁用了测试结果的缓存
--coverage 选项会生成测试覆盖率报告
--detectOpenHandles 选项会检测未关闭的资源句柄,以帮助找出可能的内存泄漏问题。
配置
Jest会自动查找目录下文件名为 jest.config.js|ts|mjs|cjs|json
的配置文件,
您可以使用 --config
标记来指明配置文件的路径。
{
//Jest 查找测试文件的根目录
roots: [array<string>],
// 转换 Node 不支持的某些语法
transform: [object<string, pathToTransformer | [pathToTransformer, object]>],
// 忽略 将跳过覆盖范围信息。
coveragePathIgnorePatterns: [array<string>],
// 报告格式
coverageReporters: [array<string | [string, options]>],
// 模块解析时的目录列表
moduleDirectories: [array<string>],
// 模块解析时的额外模块路径
modulePaths: [array<string>],
// 将路径映射为模块名称
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {
prefix: '<rootDir>/',
}),
preset: 'ts-jest',
};
覆盖率测试报告
---------------------------------------|---------|----------|---------|---------|------------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 文件名或文件路径 语句覆盖率 分支覆盖率 函数覆盖率 行覆盖率 未覆盖的代码行号
---------------------------------------|---------|----------|---------|---------|------------------------
All files | 87.89 | 52.99 | 88.88 | 87.2 |
application/httpController/health | 100 | 59.09 | 100 | 100 |
API.ts | 100 | 59.09 | 100 | 100 | 12-15
application/httpController/middleware | 72.54 | 30.43 | 100 | 70.83 |
error.ts | 100 | 100 | 100 | 100 |
errorResponse.ts | 100 | 100 | 100 | 100 |
trace.ts | 100 | 100 | 100 | 100 |
util.ts | 51.72 | 23.8 | 100 | 50 | 21,24-37
error | 100 | 100 | 100 | 100 |
errorCode.ts | 100 | 100 | 100 | 100 |
index.ts | 100 | 100 | 100 | 100 |
service/workflow/converter | 88.23 | 60.6 | 94.73 | 88.07 |
index.ts | 97.82 | 58.82 | 90 | 97.77 | 20
validationConverter.ts | 84.11 | 61.22 | 100 | 83.96 | 16,19,22,26-29,36,90,106,210,226,229,232,236,242-245
util | 77.77 | 0 | 57.14 | 62.5 |
dateHandlers.ts | 77.77 | 0 | 57.14 | 62.5 | 29-32,38,42
---------------------------------------|---------|----------|---------|---------|------------------------------------------------------
Test Suites: 5 passed 通过的测试套件, 5 total 总测试套件
Tests: 15 passed 通过的测试用例, 15 total
Snapshots: 0 total 快照测试的数量
Time: 7.472 s 测试运行的总时间