Skip to content

Commit 939559d

Browse files
authored
feat: support enable coverage via --coverage (#568)
1 parent 70f09ce commit 939559d

File tree

11 files changed

+93
-23
lines changed

11 files changed

+93
-23
lines changed
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import { defineConfig } from '@rstest/core';
22

33
export default defineConfig({
4-
coverage: {
5-
enabled: true,
6-
provider: 'istanbul',
7-
},
84
setupFiles: ['./rstest.setup.ts'],
95
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineConfig } from '@rstest/core';
2+
3+
export default defineConfig({
4+
coverage: {
5+
enabled: true,
6+
provider: 'istanbul',
7+
},
8+
setupFiles: ['./rstest.setup.ts'],
9+
});

e2e/test-coverage/index.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe('test coverage-istanbul', () => {
77
it('coverage-istanbul', async () => {
88
const { expectExecSuccess, expectLog, cli } = await runRstestCli({
99
command: 'rstest',
10-
args: ['run'],
10+
args: ['run', '-c', 'rstest.enable.config.ts'],
1111
options: {
1212
nodeOptions: {
1313
cwd: join(__dirname, 'fixtures'),
@@ -72,6 +72,24 @@ describe('test coverage-istanbul', () => {
7272
).toBeTruthy();
7373
});
7474

75+
it('enable coverage with `--coverage`', async () => {
76+
const { expectExecSuccess, expectLog, cli } = await runRstestCli({
77+
command: 'rstest',
78+
args: ['run', '--coverage'],
79+
options: {
80+
nodeOptions: {
81+
cwd: join(__dirname, 'fixtures'),
82+
},
83+
},
84+
});
85+
86+
await expectExecSuccess();
87+
88+
const logs = cli.stdout.split('\n').filter(Boolean);
89+
90+
expectLog('Coverage enabled with istanbul', logs);
91+
});
92+
7593
it('coverage-istanbul with custom options', async () => {
7694
const { expectExecSuccess, expectLog, cli } = await runRstestCli({
7795
command: 'rstest',

packages/core/src/cli/commands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const applyCommonOptions = (cli: CAC) => {
3434
.option('--include <include>', 'Match test files')
3535
.option('--exclude <exclude>', 'Exclude files from test')
3636
.option('-u, --update', 'Update snapshot files')
37+
.option('--coverage', 'Enable code coverage collection')
3738
.option(
3839
'--project <name>',
3940
'Run only projects that match the name, can be a full name or wildcards pattern',

packages/core/src/cli/init.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export type CommonOptions = {
2222
exclude?: string[];
2323
reporter?: string[];
2424
project?: string[];
25+
coverage?: boolean;
2526
passWithNoTests?: boolean;
2627
printConsoleTrace?: boolean;
2728
disableConsoleIntercept?: boolean;
@@ -83,6 +84,11 @@ async function resolveConfig(
8384
config.reporters = castArray(options.reporter) as typeof config.reporters;
8485
}
8586

87+
if (options.coverage !== undefined) {
88+
config.coverage ??= {};
89+
config.coverage.enabled = options.coverage;
90+
}
91+
8692
if (options.exclude) {
8793
config.exclude = castArray(options.exclude);
8894
}

website/docs/en/config/test/coverage.mdx

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@ type CoverageOptions = {
1717
- **Default:** `undefined`
1818
- **Version:** `>=0.4.0`
1919

20-
Collect test coverage information and generate coverage reports.
20+
Collect code coverage and generate coverage reports.
2121

22-
```ts title='rstest.config.ts'
23-
import { defineConfig } from '@rstest/core';
22+
```bash
23+
$ npx rstest --coverage
2424

25-
export default defineConfig({
26-
coverage: {
27-
enabled: true,
28-
},
29-
});
25+
----------|---------|----------|---------|---------|-------------------
26+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
27+
----------|---------|----------|---------|---------|-------------------
28+
All files | 100 | 100 | 100 | 100 |
29+
index.ts | 100 | 100 | 100 | 100 |
30+
----------|---------|----------|---------|---------|-------------------
3031
```
3132

3233
## Options
@@ -35,10 +36,20 @@ export default defineConfig({
3536

3637
- **Type:** `boolean`
3738
- **Default:** `false`
39+
- **CLI:** `--coverage`, `--coverage=false`
3840

3941
Enable or disable test coverage collection.
4042

41-
```ts title='rstest.config.ts'
43+
import { Tab, Tabs } from '@theme';
44+
45+
<Tabs defaultValue='rstest.config.ts'>
46+
<Tab label="CLI">
47+
```bash
48+
npx rstest --coverage
49+
```
50+
</Tab>
51+
<Tab label="rstest.config.ts">
52+
```ts
4253
import { defineConfig } from '@rstest/core';
4354

4455
export default defineConfig({
@@ -47,6 +58,8 @@ export default defineConfig({
4758
},
4859
});
4960
```
61+
</Tab>
62+
</Tabs>
5063

5164
### provider
5265

website/docs/en/guide/basic/cli.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ Rstest CLI provides several common options that can be used with all commands:
116116
| `--reporter <reporter>` | Specify the test reporter, see [reporters](/config/test/reporters) |
117117
| `--exclude <exclude>` | Exclude files from test, see [exclude](/config/test/exclude) |
118118
| `-u, --update` | Update snapshot files, see [update](/config/test/update) |
119+
| `--coverage` | Enable code coverage collection, see [coverage](/config/test/coverage) |
119120
| `--passWithNoTests` | Allows the test suite to pass when no files are found, see [passWithNoTests](/config/test/passWithNoTests) |
120121
| `--printConsoleTrace` | Print console traces when calling any console method, see [printConsoleTrace](/config/test/printConsoleTrace) |
121122
| `--project <name>` | Only run tests for the specified project, see [Filter by project name](/guide/basic/test-filter#filter-by-project-name) |

website/docs/en/guide/start/features.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ Rstest supports simulating the DOM and browser APIs using jsdom and happy-dom. I
3434

3535
Learn more about [DOM testing](/config/test/testEnvironment#dom-testing).
3636

37+
## Code coverage
38+
39+
Rstest supports code coverage collection using Istanbul. You can enable code coverage collection by setting `coverage.enabled` to `true` in your Rstest configuration file.
40+
41+
Learn more about [Code coverage](/config/test/coverage).
42+
3743
## More capabilities
3844

3945
- Jest-compatible assertions and snapshot testing

website/docs/zh/config/test/coverage.mdx

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ type CoverageOptions = {
1919

2020
收集测试覆盖率信息并生成覆盖率报告。
2121

22-
```ts title='rstest.config.ts'
23-
import { defineConfig } from '@rstest/core';
24-
25-
export default defineConfig({
26-
coverage: {
27-
enabled: true,
28-
},
29-
});
22+
```bash
23+
$ npx rstest --coverage
24+
25+
----------|---------|----------|---------|---------|-------------------
26+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
27+
----------|---------|----------|---------|---------|-------------------
28+
All files | 100 | 100 | 100 | 100 |
29+
index.ts | 100 | 100 | 100 | 100 |
30+
----------|---------|----------|---------|---------|-------------------
3031
```
3132

3233
## 选项
@@ -35,10 +36,20 @@ export default defineConfig({
3536

3637
- **类型:** `boolean`
3738
- **默认值:** `false`
39+
- **CLI:** `--coverage`, `--coverage=false`
3840

3941
启用或禁用测试覆盖率收集。
4042

41-
```ts title='rstest.config.ts'
43+
import { Tab, Tabs } from '@theme';
44+
45+
<Tabs defaultValue='rstest.config.ts'>
46+
<Tab label="CLI">
47+
```bash
48+
npx rstest --coverage
49+
```
50+
</Tab>
51+
<Tab label="rstest.config.ts">
52+
```ts
4253
import { defineConfig } from '@rstest/core';
4354

4455
export default defineConfig({
@@ -47,6 +58,8 @@ export default defineConfig({
4758
},
4859
});
4960
```
61+
</Tab>
62+
</Tabs>
5063

5164
### provider
5265

website/docs/zh/guide/basic/cli.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ Rstest CLI 支持以下常用参数,所有命令均可使用:
115115
| `--isolate` | 在隔离环境中运行测试,详见 [isolate](/config/test/isolate) |
116116
| `--exclude <exclude>` | 排除指定文件,详见 [exclude](/config/test/exclude) |
117117
| `-u, --update` | 更新快照文件,详见 [update](/config/test/update) |
118+
| `--coverage` | 启用代码覆盖率收集,详见 [coverage](/config/test/coverage) |
118119
| `--passWithNoTests` | 当未找到测试文件时允许测试通过,详见 [passWithNoTests](/config/test/passWithNoTests) |
119120
| `--printConsoleTrace` | 调用 console 方法时打印调用栈,详见 [printConsoleTrace](/config/test/printConsoleTrace) |
120121
| `--project <name>` | 仅运行指定项目的测试,详见 [根据项目名称过滤](/guide/basic/test-filter#根据项目名称过滤) |

0 commit comments

Comments
 (0)