From 01f61090134551a72affbec72ceaa69df1043d78 Mon Sep 17 00:00:00 2001 From: cenfun Date: Sun, 12 May 2024 11:12:00 +0800 Subject: [PATCH] docs --- README.md | 29 ++++++++++++++++++++++------- README.zh-Hans.md | 28 ++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index f0d587ee..12789a98 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ - [Collecting V8 Coverage Data from Node.js](#collecting-v8-coverage-data-from-nodejs) - [Collecting V8 Coverage Data with `CDPClient` API](#collecting-v8-coverage-data-with-cdpclient-api) - [V8 Coverage Data API](#v8-coverage-data-api) -* [Using `entryFilter` and `sourceFilter` to filter the results for V8 report](#using-entryfilter-and-sourcefilter-to-filter-the-results-for-v8-report) +* [Filtering Results](#filtering-results) * [Resolve `sourcePath` for the Source Files](#resolve-sourcepath-for-the-source-files) * [Adding Empty Coverage for Untested Files](#adding-empty-coverage-for-untested-files) * [onEnd Hook](#onend-hook) @@ -53,6 +53,7 @@ - [ts-node](#ts-node) - [AVA](#ava) - [Codecov](#codecov) + - [Codacy](#codacy) - [Coveralls](#coveralls) - [Sonar Cloud](#sonar-cloud) * [Contributing](#contributing) @@ -467,7 +468,7 @@ export interface ScriptCoverage { export type V8CoverageData = ScriptCoverage[]; ``` -## Filtering results +## Filtering Results ### Using `entryFilter` and `sourceFilter` to filter the results for V8 report When V8 coverage data collected, it actually contains the data of all entry files, for example: @@ -518,19 +519,33 @@ const coverageOptions = { } }; ``` -In fact, the `minimatch` patterns will be transformed to a function like: +As CLI args (JSON-like string. Added in: v2.8): +```sh +mcr --sourceFilter "{'**/node_modules/**':false,'**/**':true}" +``` +Note, those patterns will be transformed to a function, and the order of the patterns will impact the results: ```js const coverageOptions = { - // '**/node_modules/**': false, - // '**/vendor.js': false, - // '**/src/**': true entryFilter: (entry) => { if (minimatch(entry.url, '**/node_modules/**')) { return false; } if (minimatch(entry.url, '**/vendor.js')) { return false; } if (minimatch(entry.url, '**/src/**')) { return true; } return false; // else unmatched } - // Note, the order of the patterns will impact the results +}; +``` + +### Using `filter` instead of `entryFilter` and `sourceFilter` +If you don't want to define both `entryFilter` and `sourceFilter`, you can use `filter` instead. (Added in: v2.8) +```js +const coverageOptions = { + // combined patterns + filter: { + '**/node_modules/**': false, + '**/vendor.js': false, + '**/src/**': true + '**/**': true + } }; ``` diff --git a/README.zh-Hans.md b/README.zh-Hans.md index e5848b70..564a223b 100644 --- a/README.zh-Hans.md +++ b/README.zh-Hans.md @@ -21,7 +21,7 @@ - [从Node.js](#collecting-v8-coverage-data-from-nodejs) - [使用`CDPClient`API](#collecting-v8-coverage-data-with-cdpclient-api) - [参考V8覆盖率的API](#v8-coverage-data-api) -* [使用 `entryFilter` 和 `sourceFilter` 来过滤V8覆盖率数据](#using-entryfilter-and-sourcefilter-to-filter-the-results-for-v8-report) +* [过滤V8覆盖率数据](#filtering-results) * [使用 `sourcePath` 修改源文件路径](#resolve-sourcepath-for-the-source-files) * [为未测试的文件添加空的覆盖率报告](#adding-empty-coverage-for-untested-files) * [onEnd回调函数](#onend-hook) @@ -53,6 +53,7 @@ - [ts-node](#ts-node) - [AVA](#ava) - [Codecov](#codecov) + - [Codacy](#codacy) - [Coveralls](#coveralls) - [Sonar Cloud](#sonar-cloud) * [Contributing](#contributing) @@ -471,6 +472,7 @@ export interface ScriptCoverage { export type V8CoverageData = ScriptCoverage[]; ``` +## Filtering Results ## Using `entryFilter` and `sourceFilter` to filter the results for V8 report 当收集到V8的覆盖数据时,它实际上包含了所有的入口文件的覆盖率数据, 比如有以下3个文件: @@ -521,19 +523,33 @@ const coverageOptions = { } }; ``` -这些`minimatch`匹配的运行逻辑,大概相当于: +作为CLI参数(JSON字符串,Added in: v2.8): +```sh +mcr --sourceFilter "{'**/node_modules/**':false,'**/**':true}" +``` +注意,这些匹配实际上会转换成一个过滤函数(如下),所以如果一个匹配成功则会直接返回,后面的将不再继续匹配。请注意先后顺序,如果存在包含关系的,可以调整上下顺序,最后如果都未匹配,则默认返回false ```js const coverageOptions = { - // '**/node_modules/**': false, - // '**/vendor.js': false, - // '**/src/**': true entryFilter: (entry) => { if (minimatch(entry.url, '**/node_modules/**')) { return false; } if (minimatch(entry.url, '**/vendor.js')) { return false; } if (minimatch(entry.url, '**/src/**')) { return true; } return false; // else unmatched } - // 注意,前面的如果已经匹配到会直接返回,后面的不会继续匹配,所以如果存在包含关系的,需要注意每个匹配的上下顺序,最后如果都未匹配,则默认返回false +}; +``` + +### Using `filter` instead of `entryFilter` and `sourceFilter` +如果你不想定义两个过滤器,可以使用 `filter` 选项代替,可以将多个匹配合并在一起. (Added in: v2.8) +```js +const coverageOptions = { + // combined patterns + filter: { + '**/node_modules/**': false, + '**/vendor.js': false, + '**/src/**': true + '**/**': true + } }; ```