您的位置:首页 > 文旅 > 美景 > jest测试

jest测试

2024/10/5 8:21:28 来源:https://blog.csdn.net/xyc1211/article/details/139300108  浏览:    关键词:jest测试

文章目录

  • 测试
    • test
    • expect
    • toThrow
  • async
    • Promise
    • Async/Await
  • 作用域
    • 一次性设置 beforeAll` and `afterAll
    • 重复设置 beforeEach` 和 `afterEach
    • `describe` 块
  • mock
  • cli
  • 配置
  • 覆盖率测试报告

测试

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');}
}

作用域

一次性设置 beforeAllandafterAll

每个文件前后执行

重复设置 beforeEachafterEach

每个test前后执行

//每个测试之前调用方法 initializeCityDatabase()
beforeEach(() => {initializeCityDatabase();
});
//每个测试后,调用方法 clearCityDatabase()
afterEach(() => {clearCityDatabase();
});

describe

describe('matching cities to foods', () => {// Applies only to tests in this describe blockbeforeEach(() => {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-37error                                 |     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 | 20validationConverter.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	测试运行的总时间

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com