Skip to content

Commit 476c9a0

Browse files
9aoyCopilot
andauthored
test: support filter test by absolute path (#12749)
* test: support filter test by absolute path * Update packages/rspack-test-tools/src/helper/directory.ts Co-authored-by: Copilot <[email protected]> * Update packages/rspack-test-tools/src/helper/directory.ts Co-authored-by: Copilot <[email protected]> * Update tests/rspack-test/Cache.test.js Co-authored-by: Copilot <[email protected]> * fix: lint * docs: update -t --------- Co-authored-by: Copilot <[email protected]>
1 parent cd15818 commit 476c9a0

File tree

6 files changed

+44
-19
lines changed

6 files changed

+44
-19
lines changed

packages/rspack-test-tools/etc/test-tools.api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ export function createWatchCase(name: string, src: string, dist: string, temp: s
138138
export function createWatchIncrementalCase(name: string, src: string, dist: string, temp: string, options?: WatchIncrementalOptions): void;
139139

140140
// @public (undocumented)
141-
export function describeByWalk(testFile: string, createCase: (name: string, src: string, dist: string) => void, options?: {
141+
export function describeByWalk(
142+
testFile: string, createCase: (name: string, src: string, dist: string) => void, options?: {
142143
type?: 'file' | 'directory';
143144
level?: number;
144145
source?: string;

packages/rspack-test-tools/src/helper/directory.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ export const isValidCaseDirectory = (name: string) =>
99
!name.startsWith('_') && !name.startsWith('.') && name !== 'node_modules';
1010

1111
export function describeByWalk(
12+
/**
13+
* The test file absolute path.
14+
*/
1215
testFile: string,
1316
createCase: (name: string, src: string, dist: string) => void,
1417
options: {
@@ -30,6 +33,7 @@ export function describeByWalk(
3033
options.source || path.join(path.dirname(testFile), `${testId}Cases`);
3134

3235
const testSourceId = path.basename(sourceBase);
36+
const absoluteTestDir = path.dirname(testFile);
3337

3438
const distBase =
3539
options.dist || path.join(path.dirname(testFile), 'js', testId);
@@ -75,8 +79,21 @@ export function describeByWalk(
7579
.split('.')
7680
.shift()!,
7781
);
82+
let suiteName = name;
7883

79-
describeFn(name, () => {
84+
// support filter test by absolute path
85+
if (process.env.testFilter?.includes(absoluteTestDir)) {
86+
const fullCasePath = path.join(
87+
absoluteTestDir,
88+
testSourceId,
89+
caseName,
90+
);
91+
if (fullCasePath.includes(process.env.testFilter!)) {
92+
suiteName = fullCasePath;
93+
}
94+
}
95+
96+
describeFn(suiteName, () => {
8097
const source = path.join(sourceBase, caseName);
8198
let dist = '';
8299
if (absoluteDist) {
@@ -89,7 +106,7 @@ export function describeByWalk(
89106
dist = path.join(sourceBase, caseName, relativeDist);
90107
}
91108
}
92-
createCase(name, source, dist);
109+
createCase(suiteName, source, dist);
93110
});
94111
}
95112
});

tests/rspack-test/Cache.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const tempDir = path.resolve(__dirname, `./js/temp`);
44

55
// Run tests rspack-test/tests/cacheCases in target async-node
66
describeByWalk(
7-
"cache",
7+
__filename,
88
(name, src, dist) => {
99
createCacheCase(name, src, dist, "async-node", path.join(tempDir, name));
1010
},

tests/rspack-test/rstest.config.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ const wasmConfig = process.env.WASM && defineProject({
3434
maxConcurrency: 1,
3535
});
3636

37+
const testFilter = process.argv.includes("--test") || process.argv.includes("-t")
38+
? process.argv[
39+
(process.argv.includes("-t")
40+
? process.argv.indexOf("-t")
41+
: process.argv.indexOf("--test")) + 1
42+
]
43+
: undefined;
44+
3745
const sharedConfig = defineProject({
3846
setupFiles: setupFilesAfterEnv,
3947
testTimeout: process.env.CI ? 60000 : 30000,
@@ -75,14 +83,7 @@ const sharedConfig = defineProject({
7583
RSPACK_DEV: 'false',
7684
RSPACK_EXPERIMENTAL: 'true',
7785
RSPACK_CONFIG_VALIDATE: "strict",
78-
testFilter:
79-
process.argv.includes("--test") || process.argv.includes("-t")
80-
? process.argv[
81-
(process.argv.includes("-t")
82-
? process.argv.indexOf("-t")
83-
: process.argv.indexOf("--test")) + 1
84-
]
85-
: undefined,
86+
testFilter,
8687
printLogger: process.env.DEBUG === "test" ? 'true' : 'false',
8788
__TEST_PATH__: __dirname,
8889
__TEST_FIXTURES_PATH__: path.resolve(__dirname, "fixtures"),
@@ -112,7 +113,7 @@ export default defineConfig({
112113
RSPACK_HOT_TEST: 'true',
113114
},
114115
}],
115-
reporters: ['default'],
116+
reporters: testFilter ? ['verbose'] : ['default'],
116117
hideSkippedTests: true,
117118
pool: {
118119
maxWorkers: process.env.WASM ? 1 : "80%",

website/docs/en/contribute/development/testing.mdx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ You can run Rspack tests by running `./x test unit` or `pnpm run test:unit` at t
4141
You can also go to the `tests/rspack-test` folder and run `npm run test` to run test cases and add some arguments:
4242

4343
- **When refreshing test snapshots is needed**: Add `-u`, like `npm run test -- -u`
44-
- **When filtering test cases is needed**: Add `-t`, like `npm run test -- -t configCases/asset` to only run test cases from the `tests/rspack-test/configCases/asset` folder. Pattern matching supports regex, see [rstest](https://rstest.rs/config/test/testNamePattern) for details.
44+
- **When filtering test cases is needed**: Add `-t`. Pattern matching supports regex, see [rstest](https://rstest.rs/config/test/testNamePattern) for details.
4545

4646
### Running tests
4747

@@ -51,8 +51,11 @@ You can run these test cases in the following ways:
5151
- Or run `npm run test` from the `tests/rspack-test` directory.
5252
- To update snapshots, run `npm run test -- -u` from the `tests/rspack-test` directory.
5353
- To pass specific rstest cli arguments, run `npm run test -- {args}` from the `tests/rspack-test` directory.
54-
- To filter specific test cases, run `npm run test -- -t path-of-spec` from the `tests/rspack-test` directory.
55-
- Like `npm run test -- -t configCases/asset` to only run test cases from the `tests/rspack-test/configCases/asset` folder (config will be automatically mapped to configCases, and other folders will work in a similar way).
54+
- To filter specific test cases, run `npm run test -- -t ${testPath}` where `testPath` can be either an absolute or relative path.
55+
- For example:
56+
- `npm run test -- -t hotCases/json/error-in-json` (or `npm run test -- -t /Users/rspack/tests/rspack-test/hotCases/json/error-in-json`) will run all tests in the `error-in-json` test case.
57+
- `npm run test -- -t hotCases/json` (or `npm run test -- -t /Users/rspack/tests/rspack-test/hotCases/json`) will run all test cases in the `json` directory.
58+
- `npm run test -- -t hotCases` (or `npm run test -- -t /Users/rspack/tests/rspack-test/hotCases`) will run all test cases in the `hotCases` directory.
5659
- To use Rspack Wasm for running test cases, you need to additionally configure the following environment variables:
5760
1. `NAPI_RS_FORCE_WASI=1`: Forces the use of Rspack Wasm instead of native binding
5861
2. `WASM=1`: Enables Wasm-specific test configurations

website/docs/zh/contribute/development/testing.mdx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Rspack 的测试用例包括如下:
4141
也可以进入 `tests/rspack-test` 文件夹并运行 `npm run test` 来运行测试用例,并且对测试流程进行更精细的控制:
4242

4343
- **需要刷新测试快照时**:添加 `-u` 参数,如 `npm run test -- -u`
44-
- **需要过滤测试用例时**:添加 `-t` 参数,如 `npm run test -- -t configCases/asset` 即可仅运行 `tests/rspack-test/configCases/asset` 文件夹下的用例。匹配支持正则,详见 [rstest](https://rstest.rs/config/test/testNamePattern)
44+
- **需要过滤测试用例时**:添加 `-t` 参数。匹配支持正则,详见 [rstest](https://rstest.rs/config/test/testNamePattern)
4545

4646
### 运行测试
4747

@@ -51,8 +51,11 @@ Rspack 的测试用例包括如下:
5151
- 或在 `tests/rspack-test` 目录下运行 `npm run test`
5252
- 如需更新 snapshot,在 `tests/rspack-test` 目录下运行 `npm run test -- -u`
5353
- 如需传入特定 rstest cli 参数,在 `tests/rspack-test` 目录下运行 `npm run test -- {args}`
54-
- 如需过滤特定测试用例,在 `tests/rspack-test` 目录下运行 `npm run test -- -t path-of-spec`
55-
-`npm run test -- -t configCases/asset` 即可仅运行 `tests/rspack-test/configCases/asset` 文件夹下的用例(config 会自动映射到 configCases,其他文件夹类似)。
54+
- 如需过滤特定测试用例,运行 `npm run test -- -t ${testPath}`,其中 `${testPath}` 可以是绝对路径或相对路径。
55+
- 例如:
56+
- `npm run test -- -t hotCases/json/error-in-json` (或 `npm run test -- -t /Users/rspack/tests/rspack-test/hotCases/json/error-in-json`) 将运行 `error-in-json` 测试用例中的所有测试。
57+
- `npm run test -- -t hotCases/json` (或 `npm run test -- -t /Users/rspack/tests/rspack-test/hotCases/json`) 将运行 `json` 目录下的所有测试用例。
58+
- `npm run test -- -t hotCases` (或 `npm run test -- -t /Users/rspack/tests/rspack-test/hotCases`) 将运行 `hotCases` 目录下的所有测试用例。
5659
- 如需使用 Rspack Wasm 运行测试用例,需要额外配置以下环境变量:
5760
1. `NAPI_RS_FORCE_WASI=1`: 强制使用 Rspack Wasm 而不是原生绑定
5861
2. `WASM=1`:启用 Wasm 专用的测试配置

0 commit comments

Comments
 (0)