简中文档
On this page

deno coverage

Command line usage

deno coverage [OPTIONS] [files]...

Print coverage reports from coverage profiles.

Collect a coverage profile with deno test:

deno test --coverage=cov_profile

Print a report to stdout:

deno coverage cov_profile

Include urls that start with the file schema and exclude files ending with test.ts and test.js, for an url to match it must match the include pattern and not match the exclude pattern:

deno coverage --include="^file:" --exclude="test\.(ts|js)" cov_profile

Write a report using the lcov format:

deno coverage --lcov --output=cov.lcov cov_profile/

Generate html reports from lcov:

genhtml -o html_cov cov.lcov

Options Jump to heading

--detailed Jump to heading

Output coverage report in detailed format in the terminal.

--exclude Jump to heading

Exclude source files from the report.

--html Jump to heading

Output coverage report in HTML format in the given directory.

--ignore Jump to heading

Ignore coverage files.

--include Jump to heading

Include source files in the report.

--lcov Jump to heading

Output coverage report in lcov format.

--output Jump to heading

Exports the coverage report in lcov format to the given file. If no --output arg is specified then the report is written to stdout.

包含和排除 Jump to heading

默认情况下,覆盖率包括您本地文件系统中存在的任何代码及其导入。

您可以通过使用 --include--exclude 选项来自定义包含和排除。

您可以通过使用 --include 选项并自定义正则表达式模式来扩展覆盖率,以包括不在本地文件系统上的文件。

deno coverage --include="^file:|https:"

默认的包含模式应足以满足大多数用例,但您可以自定义它以更具体地指定哪些文件包含在您的覆盖率报告中。

文件名中包含 test.jstest.tstest.jsxtest.tsx 的文件默认被排除。

这相当于:

deno coverage --exclude="test\.(js|mjs|ts|jsx|tsx)$"

此默认设置防止您的测试代码为您的覆盖率报告做出贡献。URL 要匹配,它必须与包含模式匹配,且不与排除模式匹配。

忽略代码 Jump to heading

通过添加覆盖忽略注释,可以在生成的覆盖率报告中忽略代码。被忽略代码中的分支和行将被排除在报告之外。被忽略的分支和行不计算为覆盖行。相反,被忽略的代码行被视为空行。

要忽略整个文件,请在文件顶部添加 // deno-coverage-ignore-file 注释。

// deno-coverage-ignore-file

// 此文件中的所有代码被忽略

被忽略的文件将不会出现在覆盖率报告中。

要忽略单个行,请在要忽略的代码上方的行添加 // deno-coverage-ignore-next 注释。

// deno-coverage-ignore-next
console.log("这一行被忽略");

要忽略多行,请在要忽略的代码上方添加 // deno-coverage-ignore-start 注释,并在下方添加 // deno-coverage-ignore-stop 注释。

// deno-coverage-ignore-start
if (condition) {
  console.log("分支和行都被忽略");
}
// deno-coverage-ignore-stop

// deno-coverage-ignore-start 注释后面的所有代码都将被忽略,直到达到 // deno-coverage-ignore-stop。但是,如果有多个连续的开始注释,则每个这样的注释必须由相应的停止注释终止。

// deno-coverage-ignore-start
if (condition) {
  // deno-coverage-ignore-start
  console.log("这一行被忽略");
  // deno-coverage-ignore-stop
  console.log("这一行也被忽略");
}
// deno-coverage-ignore-stop

console.log("这一行没有被忽略");

在覆盖注释中,只有空格可以位于覆盖指令之前。然而,任何文本可以跟随该指令。

// deno-coverage-ignore-next 尾随文本是允许的。
console.log("这一行被忽略");

// 但前导文本是不允许的。 deno-coverage-ignore-next
console.log("这一行没有被忽略");

覆盖注释必须以 // 开头。以 /* 开头的注释不是有效的覆盖注释。

// deno-coverage-ignore-next
console.log("这一行被忽略");

/* deno-coverage-ignore-next */
console.log("这一行没有被忽略");

输出格式 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

你找到了你需要的东西吗?

编辑此页面
隐私政策