Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions e2e/test-coverage/fixtures/rstest.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { defineConfig } from '@rstest/core';

export default defineConfig({
coverage: {
enabled: true,
provider: 'istanbul',
},
setupFiles: ['./rstest.setup.ts'],
});
9 changes: 9 additions & 0 deletions e2e/test-coverage/fixtures/rstest.enable.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from '@rstest/core';

export default defineConfig({
coverage: {
enabled: true,
provider: 'istanbul',
},
setupFiles: ['./rstest.setup.ts'],
});
20 changes: 19 additions & 1 deletion e2e/test-coverage/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('test coverage-istanbul', () => {
it('coverage-istanbul', async () => {
const { expectExecSuccess, expectLog, cli } = await runRstestCli({
command: 'rstest',
args: ['run'],
args: ['run', '-c', 'rstest.enable.config.ts'],
options: {
nodeOptions: {
cwd: join(__dirname, 'fixtures'),
Expand Down Expand Up @@ -72,6 +72,24 @@ describe('test coverage-istanbul', () => {
).toBeTruthy();
});

it('enable coverage with `--coverage`', async () => {
const { expectExecSuccess, expectLog, cli } = await runRstestCli({
command: 'rstest',
args: ['run', '--coverage'],
options: {
nodeOptions: {
cwd: join(__dirname, 'fixtures'),
},
},
});

await expectExecSuccess();

const logs = cli.stdout.split('\n').filter(Boolean);

expectLog('Coverage enabled with istanbul', logs);
});

it('coverage-istanbul with custom options', async () => {
const { expectExecSuccess, expectLog, cli } = await runRstestCli({
command: 'rstest',
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/cli/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const applyCommonOptions = (cli: CAC) => {
.option('--include <include>', 'Match test files')
.option('--exclude <exclude>', 'Exclude files from test')
.option('-u, --update', 'Update snapshot files')
.option('--coverage', 'Enable code coverage collection')
.option(
'--project <name>',
'Run only projects that match the name, can be a full name or wildcards pattern',
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/cli/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type CommonOptions = {
exclude?: string[];
reporter?: string[];
project?: string[];
coverage?: boolean;
passWithNoTests?: boolean;
printConsoleTrace?: boolean;
disableConsoleIntercept?: boolean;
Expand Down Expand Up @@ -83,6 +84,11 @@ async function resolveConfig(
config.reporters = castArray(options.reporter) as typeof config.reporters;
}

if (options.coverage !== undefined) {
config.coverage ??= {};
config.coverage.enabled = options.coverage;
}

if (options.exclude) {
config.exclude = castArray(options.exclude);
}
Expand Down
31 changes: 22 additions & 9 deletions website/docs/en/config/test/coverage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ type CoverageOptions = {
- **Default:** `undefined`
- **Version:** `>=0.4.0`

Collect test coverage information and generate coverage reports.
Collect code coverage and generate coverage reports.

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

export default defineConfig({
coverage: {
enabled: true,
},
});
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
index.ts | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
```

## Options
Expand All @@ -35,10 +36,20 @@ export default defineConfig({

- **Type:** `boolean`
- **Default:** `false`
- **CLI:** `--coverage`, `--coverage=false`

Enable or disable test coverage collection.

```ts title='rstest.config.ts'
import { Tab, Tabs } from '@theme';

<Tabs defaultValue='rstest.config.ts'>
<Tab label="CLI">
```bash
npx rstest --coverage
```
</Tab>
<Tab label="rstest.config.ts">
```ts
import { defineConfig } from '@rstest/core';

export default defineConfig({
Expand All @@ -47,6 +58,8 @@ export default defineConfig({
},
});
```
</Tab>
</Tabs>

### provider

Expand Down
1 change: 1 addition & 0 deletions website/docs/en/guide/basic/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ Rstest CLI provides several common options that can be used with all commands:
| `--reporter <reporter>` | Specify the test reporter, see [reporters](/config/test/reporters) |
| `--exclude <exclude>` | Exclude files from test, see [exclude](/config/test/exclude) |
| `-u, --update` | Update snapshot files, see [update](/config/test/update) |
| `--coverage` | Enable code coverage collection, see [coverage](/config/test/coverage) |
| `--passWithNoTests` | Allows the test suite to pass when no files are found, see [passWithNoTests](/config/test/passWithNoTests) |
| `--printConsoleTrace` | Print console traces when calling any console method, see [printConsoleTrace](/config/test/printConsoleTrace) |
| `--project <name>` | Only run tests for the specified project, see [Filter by project name](/guide/basic/test-filter#filter-by-project-name) |
Expand Down
6 changes: 6 additions & 0 deletions website/docs/en/guide/start/features.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ Rstest supports simulating the DOM and browser APIs using jsdom and happy-dom. I

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

## Code coverage

Rstest supports code coverage collection using Istanbul. You can enable code coverage collection by setting `coverage.enabled` to `true` in your Rstest configuration file.

Learn more about [Code coverage](/config/test/coverage).

## More capabilities

- Jest-compatible assertions and snapshot testing
Expand Down
31 changes: 22 additions & 9 deletions website/docs/zh/config/test/coverage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ type CoverageOptions = {

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

```ts title='rstest.config.ts'
import { defineConfig } from '@rstest/core';

export default defineConfig({
coverage: {
enabled: true,
},
});
```bash
$ npx rstest --coverage

----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
index.ts | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
```

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

- **类型:** `boolean`
- **默认值:** `false`
- **CLI:** `--coverage`, `--coverage=false`

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

```ts title='rstest.config.ts'
import { Tab, Tabs } from '@theme';

<Tabs defaultValue='rstest.config.ts'>
<Tab label="CLI">
```bash
npx rstest --coverage
```
</Tab>
<Tab label="rstest.config.ts">
```ts
import { defineConfig } from '@rstest/core';

export default defineConfig({
Expand All @@ -47,6 +58,8 @@ export default defineConfig({
},
});
```
</Tab>
</Tabs>

### provider

Expand Down
1 change: 1 addition & 0 deletions website/docs/zh/guide/basic/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Rstest CLI 支持以下常用参数,所有命令均可使用:
| `--isolate` | 在隔离环境中运行测试,详见 [isolate](/config/test/isolate) |
| `--exclude <exclude>` | 排除指定文件,详见 [exclude](/config/test/exclude) |
| `-u, --update` | 更新快照文件,详见 [update](/config/test/update) |
| `--coverage` | 启用代码覆盖率收集,详见 [coverage](/config/test/coverage) |
| `--passWithNoTests` | 当未找到测试文件时允许测试通过,详见 [passWithNoTests](/config/test/passWithNoTests) |
| `--printConsoleTrace` | 调用 console 方法时打印调用栈,详见 [printConsoleTrace](/config/test/printConsoleTrace) |
| `--project <name>` | 仅运行指定项目的测试,详见 [根据项目名称过滤](/guide/basic/test-filter#根据项目名称过滤) |
Expand Down
6 changes: 6 additions & 0 deletions website/docs/zh/guide/start/features.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ Rstest 支持使用 jsdom 与 happy-dom 模拟 DOM 与浏览器 API,并对 Rea

了解更多关于 [DOM 测试](/config/test/testEnvironment#dom-测试)。

## 代码覆盖率

Rstest 支持使用 Istanbul 收集代码覆盖率。你可以通过设置 `coverage.enabled` 为 `true` 来启用代码覆盖率收集。

了解更多关于 [代码覆盖率](/config/test/coverage)。

## 更多能力

- 与 Jest 兼容的断言与快照测试
Expand Down
Loading