deno coverage
命令行用法
deno coverage [OPTIONS] [files]...
从覆盖率配置文件打印覆盖率报告。
使用 deno test 收集覆盖率配置文件
deno test --coverage=cov_profile
将报告打印到标准输出
deno coverage cov_profile
包含以文件方案开头的 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
参数,则报告将写入标准输出。
包含与排除 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
注释。
// deno-coverage-ignore
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-stop
注释之前,// deno-coverage-ignore-start
注释之后的所有代码都将被忽略。
每个 // deno-coverage-ignore-start
注释必须由 // deno-coverage-ignore-stop
注释终止,并且忽略范围不能嵌套。如果未满足这些要求,某些行可能会无意中包含在覆盖率报告中。deno coverage
命令将记录任何无效注释的警告。
// deno-coverage-ignore-start
if (condition) {
// deno-coverage-ignore-start - A warning will be logged because the previous
// coverage range is unterminated.
console.log("this code is ignored");
// deno-coverage-ignore-stop
}
// deno-coverage-ignore-stop
// ...
// deno-coverage-ignore-start - This comment will be ignored and a warning will
// be logged, because this range is unterminated.
console.log("this code is not ignored");
在覆盖率注释中,覆盖率指令前只能是空白字符。但是,任何文本都可以在指令后出现。
// deno-coverage-ignore Trailing text is allowed.
console.log("This line is ignored");
// But leading text isn't. deno-coverage-ignore
console.log("This line is not ignored");
覆盖率注释必须以 //
开头。以 /*
开头的注释不是有效的覆盖率注释。
// deno-coverage-ignore
console.log("This line is ignored");
/* deno-coverage-ignore */
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
注意:您也可以通过
DENO_COVERAGE_DIR
环境变量设置覆盖率目录。DENO_COVERAGE_DIR=custom_profile_name deno test deno coverage custom_profile_name
仅包含与特定模式匹配的覆盖率——在本例中,仅包含来自 main.ts 的测试
deno coverage --include="main.ts"
将默认覆盖率配置文件中的测试覆盖率导出到 lcov 文件
deno test --coverage
deno coverage --lcov --output=cov.lcov