File tree Expand file tree Collapse file tree 6 files changed +74
-8
lines changed Expand file tree Collapse file tree 6 files changed +74
-8
lines changed Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change @@ -33,7 +33,8 @@ export async function listTests(
33
33
const entries = await getTestEntries ( {
34
34
include,
35
35
exclude,
36
- root,
36
+ rootPath,
37
+ projectRoot : root ,
37
38
fileFilters : context . fileFilters || [ ] ,
38
39
includeSource,
39
40
} ) ;
Original file line number Diff line number Diff line change @@ -32,7 +32,8 @@ export async function runTests(context: Rstest): Promise<void> {
32
32
include,
33
33
exclude,
34
34
includeSource,
35
- root,
35
+ rootPath,
36
+ projectRoot : root ,
36
37
fileFilters : context . fileFilters || [ ] ,
37
38
} ) ;
38
39
Original file line number Diff line number Diff line change @@ -48,20 +48,22 @@ export const formatTestEntryName = (name: string): string =>
48
48
export const getTestEntries = async ( {
49
49
include,
50
50
exclude,
51
- root,
51
+ rootPath,
52
+ projectRoot,
52
53
fileFilters,
53
54
includeSource,
54
55
} : {
56
+ rootPath : string ;
55
57
include : string [ ] ;
56
58
exclude : string [ ] ;
57
59
includeSource : string [ ] ;
58
60
fileFilters : string [ ] ;
59
- root : string ;
61
+ projectRoot : string ;
60
62
} ) : Promise < {
61
63
[ name : string ] : string ;
62
64
} > => {
63
65
const testFiles = await glob ( include , {
64
- cwd : root ,
66
+ cwd : projectRoot ,
65
67
absolute : true ,
66
68
ignore : exclude ,
67
69
dot : true ,
@@ -70,7 +72,7 @@ export const getTestEntries = async ({
70
72
71
73
if ( includeSource ?. length ) {
72
74
const sourceFiles = await glob ( includeSource , {
73
- cwd : root ,
75
+ cwd : projectRoot ,
74
76
absolute : true ,
75
77
ignore : exclude ,
76
78
dot : true ,
@@ -92,8 +94,8 @@ export const getTestEntries = async ({
92
94
}
93
95
94
96
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 ) ;
97
99
return [ formatTestEntryName ( relativePath ) , entry ] ;
98
100
} ) ,
99
101
) ;
Original file line number Diff line number Diff line change @@ -34,6 +34,23 @@ You can also specify multiple test files or glob patterns:
34
34
rstest test/foo.test.ts test/bar.test.ts
35
35
```
36
36
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
+
37
54
### include/exclude
38
55
39
56
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.
Original file line number Diff line number Diff line change @@ -34,6 +34,23 @@ rstest foo
34
34
rstest test/foo.test.ts test/bar.test.ts
35
35
```
36
36
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
+
37
54
### include/exclude
38
55
39
56
当你直接通过 ` rstest **/*.test.ts ` 过滤文件时,Rstest 会在 [ include] ( /config/test/include ) 和 [ exclude] ( /config/test/exclude ) 配置的基础上进一步筛选文件。
You can’t perform that action at this time.
0 commit comments