Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed May 12, 2024
1 parent f8f1e73 commit 01f6109
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
29 changes: 22 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -53,6 +53,7 @@
- [ts-node](#ts-node)
- [AVA](#ava)
- [Codecov](#codecov)
- [Codacy](#codacy)
- [Coveralls](#coveralls)
- [Sonar Cloud](#sonar-cloud)
* [Contributing](#contributing)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
}
};
```

Expand Down
28 changes: 22 additions & 6 deletions README.zh-Hans.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -53,6 +53,7 @@
- [ts-node](#ts-node)
- [AVA](#ava)
- [Codecov](#codecov)
- [Codacy](#codacy)
- [Coveralls](#coveralls)
- [Sonar Cloud](#sonar-cloud)
* [Contributing](#contributing)
Expand Down Expand Up @@ -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个文件:
Expand Down Expand Up @@ -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
}
};
```
Expand Down

0 comments on commit 01f6109

Please sign in to comment.