deno 代码覆盖
命令行用法
deno coverage [OPTIONS] [files]...
从覆盖率配置文件打印覆盖率报告。
使用 deno test 收集覆盖率配置文件
deno test --coverage=cov_profile
将报告打印到 stdout
deno coverage cov_profile
包含以 file 模式开头的 URL,并排除以 test.ts
和 test.js
结尾的文件,要使 URL 匹配,它必须匹配包含模式且不匹配排除模式
deno coverage --include="^file:" --exclude="test\.(ts|js)" cov_profile
使用 lcov 格式写入报告
deno coverage --lcov --output=cov.lcov cov_profile/
从 lcov 生成 HTML 报告
genhtml -o html_cov cov.lcov
选项 Jump to heading
--detailed
Jump to heading
以详细格式在终端中输出覆盖率报告。
--exclude
Jump to heading
从报告中排除源文件。
--html
Jump to heading
以 HTML 格式在给定目录中输出覆盖率报告。
--ignore
Jump to heading
忽略覆盖率文件。
--include
Jump to heading
在报告中包含源文件。
--lcov
Jump to heading
以 lcov 格式输出覆盖率报告。
--output
Jump to heading
以 lcov 格式将覆盖率报告导出到给定文件。如果未指定 --output
参数,则报告将写入 stdout。
包含和排除 Jump to heading
默认情况下,覆盖率包含本地文件系统上的任何代码及其导入。
你可以使用 --include
和 --exclude
选项自定义包含和排除。
你可以使用 --include
选项并自定义正则表达式模式,以扩展覆盖范围,包含不在本地文件系统上的文件。
deno coverage --include="^file:|https:"
默认的包含模式应该足以满足大多数用例,但你可以自定义它,使其更具体地指定哪些文件包含在你的覆盖率报告中。
默认情况下,名称中包含 test.js
、test.ts
、test.jsx
或 test.tsx
的文件将被排除。
这等效于
deno coverage --exclude="test\.(js|mjs|ts|jsx|tsx)$"
此默认设置可防止你的测试代码对覆盖率报告做出贡献。要使 URL 匹配,它必须匹配包含模式且不匹配排除模式。
忽略代码 Jump to heading
通过添加覆盖率忽略注释,可以在生成的覆盖率报告中忽略代码。忽略代码中的分支和行将从报告中排除。忽略的分支和行不计为覆盖行。相反,忽略的代码行被视为空行。
要忽略整个文件,请在文件顶部添加 // deno-coverage-ignore-file
注释。
// deno-coverage-ignore-file
// all code in this file is ignored
忽略的文件将不会出现在覆盖率报告中。
要忽略单行,请在你想要忽略的代码的上一行添加 // deno-coverage-ignore-next
注释。
// deno-coverage-ignore-next
console.log("this line is ignored");
要忽略多行,请在你想要忽略的代码上方添加 // deno-coverage-ignore-start
注释,并在下方添加 // deno-coverage-ignore-stop
注释。
// deno-coverage-ignore-start
if (condition) {
console.log("both the branch and lines are ignored");
}
// deno-coverage-ignore-stop
// deno-coverage-ignore-start
注释之后的所有代码都将被忽略,直到到达 // deno-coverage-ignore-stop
。但是,如果有多个连续的 start 注释,则每个注释都必须由相应的 stop 注释终止。
// deno-coverage-ignore-start
if (condition) {
// deno-coverage-ignore-start
console.log("this line is ignored");
// deno-coverage-ignore-stop
console.log("this line is also ignored");
}
// deno-coverage-ignore-stop
console.log("this line is not ignored");
在覆盖率注释中,只有空格可以位于覆盖率指令之前。但是,任何文本都可以跟随指令。
// deno-coverage-ignore-next Trailing text is allowed.
console.log("This line is ignored");
// But leading text isn't. deno-coverage-ignore-next
console.log("This line is not ignored");
覆盖率注释必须以 //
开头。以 /*
开头的注释不是有效的覆盖率注释。
// deno-coverage-ignore-next
console.log("This line is ignored");
/* deno-coverage-ignore-next */
console.log("This line is not ignored");
输出格式 Jump to heading
默认情况下,我们支持 Deno 自己的覆盖率格式 - 但你也可以以 lcov 格式或 html 格式输出覆盖率报告。
deno coverage --lcov --output=cov.lcov
此 lcov 文件可以与支持 lcov 格式的其他工具一起使用。
deno coverage --html
这将以 html 文件形式输出覆盖率报告
示例 Jump to heading
从你的工作区中的默认覆盖率配置文件生成覆盖率报告
deno test --coverage
deno coverage
从具有自定义名称的覆盖率配置文件生成覆盖率报告
deno test --coverage=custom_profile_name
deno coverage custom_profile_name
仅包含与特定模式匹配的覆盖率 - 在这种情况下,仅包含来自 main.ts 的测试
deno coverage --include="main.ts"
将测试覆盖率从默认覆盖率配置文件导出到 lcov 文件
deno test --coverage
deno coverage --lcov --output=cov.lcov