Skip to content

Commit 43e4bbf

Browse files
authored
feat: filter file based on workspace's root (#521)
1 parent 6ca6025 commit 43e4bbf

File tree

6 files changed

+74
-8
lines changed

6 files changed

+74
-8
lines changed

e2e/projects/filter.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { dirname, join } from 'node:path';
2+
import { fileURLToPath } from 'node:url';
3+
import { describe, expect, it } from '@rstest/core';
4+
import { runRstestCli } from '../scripts';
5+
6+
const __filename = fileURLToPath(import.meta.url);
7+
const __dirname = dirname(__filename);
8+
9+
describe('test projects filter', () => {
10+
it('should run test success with test file filter', async () => {
11+
const { cli, expectExecSuccess } = await runRstestCli({
12+
command: 'rstest',
13+
args: ['run', 'client', '--globals'],
14+
options: {
15+
nodeOptions: {
16+
cwd: join(__dirname, 'fixtures'),
17+
},
18+
},
19+
});
20+
21+
await expectExecSuccess();
22+
const logs = cli.stdout.split('\n').filter(Boolean);
23+
24+
// test log print
25+
expect(logs.find((log) => log.includes('node/test/index'))).toBeFalsy();
26+
expect(logs.find((log) => log.includes('client/test/index'))).toBeTruthy();
27+
});
28+
});

packages/core/src/core/listTests.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export async function listTests(
3333
const entries = await getTestEntries({
3434
include,
3535
exclude,
36-
root,
36+
rootPath,
37+
projectRoot: root,
3738
fileFilters: context.fileFilters || [],
3839
includeSource,
3940
});

packages/core/src/core/runTests.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export async function runTests(context: Rstest): Promise<void> {
3232
include,
3333
exclude,
3434
includeSource,
35-
root,
35+
rootPath,
36+
projectRoot: root,
3637
fileFilters: context.fileFilters || [],
3738
});
3839

packages/core/src/utils/testFiles.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,22 @@ export const formatTestEntryName = (name: string): string =>
4848
export const getTestEntries = async ({
4949
include,
5050
exclude,
51-
root,
51+
rootPath,
52+
projectRoot,
5253
fileFilters,
5354
includeSource,
5455
}: {
56+
rootPath: string;
5557
include: string[];
5658
exclude: string[];
5759
includeSource: string[];
5860
fileFilters: string[];
59-
root: string;
61+
projectRoot: string;
6062
}): Promise<{
6163
[name: string]: string;
6264
}> => {
6365
const testFiles = await glob(include, {
64-
cwd: root,
66+
cwd: projectRoot,
6567
absolute: true,
6668
ignore: exclude,
6769
dot: true,
@@ -70,7 +72,7 @@ export const getTestEntries = async ({
7072

7173
if (includeSource?.length) {
7274
const sourceFiles = await glob(includeSource, {
73-
cwd: root,
75+
cwd: projectRoot,
7476
absolute: true,
7577
ignore: exclude,
7678
dot: true,
@@ -92,8 +94,8 @@ export const getTestEntries = async ({
9294
}
9395

9496
return Object.fromEntries(
95-
filterFiles(testFiles, fileFilters, root).map((entry) => {
96-
const relativePath = pathe.relative(root, entry);
97+
filterFiles(testFiles, fileFilters, rootPath).map((entry) => {
98+
const relativePath = pathe.relative(rootPath, entry);
9799
return [formatTestEntryName(relativePath), entry];
98100
}),
99101
);

website/docs/en/guide/basic/test-filter.mdx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,23 @@ You can also specify multiple test files or glob patterns:
3434
rstest test/foo.test.ts test/bar.test.ts
3535
```
3636

37+
### Filter by file path
38+
39+
This method also applies to filtering by file path.
40+
41+
When filtering by file path, it can be either an absolute path or a relative path based on the current working directory (also called the project root directory or workspace root directory)
42+
43+
```bash
44+
# Filtering by absolute path
45+
rstest /Users/userName/Desktop/projects/rstest/packages/core/tests/index.test.ts
46+
47+
# Filtering by relative path
48+
rstest packages/core/tests/index.test.ts
49+
50+
# Filtering by project directory
51+
rstest packages/core
52+
```
53+
3754
### include/exclude
3855

3956
When you filter files directly with `rstest **/*.test.ts`, rstest will further filter files based on the [include](/config/test/include) and [exclude](/config/test/exclude) configuration.

website/docs/zh/guide/basic/test-filter.mdx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,23 @@ rstest foo
3434
rstest test/foo.test.ts test/bar.test.ts
3535
```
3636

37+
### 根据文件路径过滤
38+
39+
此种方式也适用于根据文件路径过滤。
40+
41+
当你根据文件路径过滤,它可以是一个绝对路径或者基于当前工作目录(也称为项目根目录或 workspace 根目录)的相对路径。
42+
43+
```bash
44+
# 根据绝对路径过滤
45+
rstest /Users/userName/Desktop/projects/rstest/packages/core/tests/index.test.ts
46+
47+
# 根据相对路径过滤
48+
rstest packages/core/tests/index.test.ts
49+
50+
# 根据 project 目录过滤
51+
rstest packages/core
52+
```
53+
3754
### include/exclude
3855

3956
当你直接通过 `rstest **/*.test.ts` 过滤文件时,Rstest 会在 [include](/config/test/include)[exclude](/config/test/exclude) 配置的基础上进一步筛选文件。

0 commit comments

Comments
 (0)