diff --git a/packages/rspack-test-tools/etc/test-tools.api.md b/packages/rspack-test-tools/etc/test-tools.api.md index da6cd4b9138a..2b682c08fea1 100644 --- a/packages/rspack-test-tools/etc/test-tools.api.md +++ b/packages/rspack-test-tools/etc/test-tools.api.md @@ -138,7 +138,8 @@ export function createWatchCase(name: string, src: string, dist: string, temp: s export function createWatchIncrementalCase(name: string, src: string, dist: string, temp: string, options?: WatchIncrementalOptions): void; // @public (undocumented) -export function describeByWalk(testFile: string, createCase: (name: string, src: string, dist: string) => void, options?: { +export function describeByWalk( +testFile: string, createCase: (name: string, src: string, dist: string) => void, options?: { type?: 'file' | 'directory'; level?: number; source?: string; diff --git a/packages/rspack-test-tools/src/helper/directory.ts b/packages/rspack-test-tools/src/helper/directory.ts index b06349d5faf2..be56a34c49d3 100644 --- a/packages/rspack-test-tools/src/helper/directory.ts +++ b/packages/rspack-test-tools/src/helper/directory.ts @@ -9,6 +9,9 @@ export const isValidCaseDirectory = (name: string) => !name.startsWith('_') && !name.startsWith('.') && name !== 'node_modules'; export function describeByWalk( + /** + * The test file absolute path. + */ testFile: string, createCase: (name: string, src: string, dist: string) => void, options: { @@ -30,6 +33,7 @@ export function describeByWalk( options.source || path.join(path.dirname(testFile), `${testId}Cases`); const testSourceId = path.basename(sourceBase); + const absoluteTestDir = path.dirname(testFile); const distBase = options.dist || path.join(path.dirname(testFile), 'js', testId); @@ -75,8 +79,21 @@ export function describeByWalk( .split('.') .shift()!, ); + let suiteName = name; - describeFn(name, () => { + // support filter test by absolute path + if (process.env.testFilter?.includes(absoluteTestDir)) { + const fullCasePath = path.join( + absoluteTestDir, + testSourceId, + caseName, + ); + if (fullCasePath.includes(process.env.testFilter!)) { + suiteName = fullCasePath; + } + } + + describeFn(suiteName, () => { const source = path.join(sourceBase, caseName); let dist = ''; if (absoluteDist) { @@ -89,7 +106,7 @@ export function describeByWalk( dist = path.join(sourceBase, caseName, relativeDist); } } - createCase(name, source, dist); + createCase(suiteName, source, dist); }); } }); diff --git a/tests/rspack-test/Cache.test.js b/tests/rspack-test/Cache.test.js index 8e891bd0f57d..6d70cf7193df 100644 --- a/tests/rspack-test/Cache.test.js +++ b/tests/rspack-test/Cache.test.js @@ -4,7 +4,7 @@ const tempDir = path.resolve(__dirname, `./js/temp`); // Run tests rspack-test/tests/cacheCases in target async-node describeByWalk( - "cache", + __filename, (name, src, dist) => { createCacheCase(name, src, dist, "async-node", path.join(tempDir, name)); }, diff --git a/tests/rspack-test/rstest.config.ts b/tests/rspack-test/rstest.config.ts index aa8c2ead2b58..9deb8c0beb0b 100644 --- a/tests/rspack-test/rstest.config.ts +++ b/tests/rspack-test/rstest.config.ts @@ -34,6 +34,14 @@ const wasmConfig = process.env.WASM && defineProject({ maxConcurrency: 1, }); +const testFilter = process.argv.includes("--test") || process.argv.includes("-t") + ? process.argv[ + (process.argv.includes("-t") + ? process.argv.indexOf("-t") + : process.argv.indexOf("--test")) + 1 + ] + : undefined; + const sharedConfig = defineProject({ setupFiles: setupFilesAfterEnv, testTimeout: process.env.CI ? 60000 : 30000, @@ -75,14 +83,7 @@ const sharedConfig = defineProject({ RSPACK_DEV: 'false', RSPACK_EXPERIMENTAL: 'true', RSPACK_CONFIG_VALIDATE: "strict", - testFilter: - process.argv.includes("--test") || process.argv.includes("-t") - ? process.argv[ - (process.argv.includes("-t") - ? process.argv.indexOf("-t") - : process.argv.indexOf("--test")) + 1 - ] - : undefined, + testFilter, printLogger: process.env.DEBUG === "test" ? 'true' : 'false', __TEST_PATH__: __dirname, __TEST_FIXTURES_PATH__: path.resolve(__dirname, "fixtures"), @@ -112,7 +113,7 @@ export default defineConfig({ RSPACK_HOT_TEST: 'true', }, }], - reporters: ['default'], + reporters: testFilter ? ['verbose'] : ['default'], hideSkippedTests: true, pool: { maxWorkers: process.env.WASM ? 1 : "80%", diff --git a/website/docs/en/contribute/development/testing.mdx b/website/docs/en/contribute/development/testing.mdx index 9c4c2c838660..e94a9e1398d4 100644 --- a/website/docs/en/contribute/development/testing.mdx +++ b/website/docs/en/contribute/development/testing.mdx @@ -41,7 +41,7 @@ You can run Rspack tests by running `./x test unit` or `pnpm run test:unit` at t You can also go to the `tests/rspack-test` folder and run `npm run test` to run test cases and add some arguments: - **When refreshing test snapshots is needed**: Add `-u`, like `npm run test -- -u` -- **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. +- **When filtering test cases is needed**: Add `-t`. Pattern matching supports regex, see [rstest](https://rstest.rs/config/test/testNamePattern) for details. ### Running tests @@ -51,8 +51,11 @@ You can run these test cases in the following ways: - Or run `npm run test` from the `tests/rspack-test` directory. - To update snapshots, run `npm run test -- -u` from the `tests/rspack-test` directory. - To pass specific rstest cli arguments, run `npm run test -- {args}` from the `tests/rspack-test` directory. -- To filter specific test cases, run `npm run test -- -t path-of-spec` from the `tests/rspack-test` directory. - - 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). +- To filter specific test cases, run `npm run test -- -t ${testPath}` where `testPath` can be either an absolute or relative path. + - For example: + - `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. + - `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. + - `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. - To use Rspack Wasm for running test cases, you need to additionally configure the following environment variables: 1. `NAPI_RS_FORCE_WASI=1`: Forces the use of Rspack Wasm instead of native binding 2. `WASM=1`: Enables Wasm-specific test configurations diff --git a/website/docs/zh/contribute/development/testing.mdx b/website/docs/zh/contribute/development/testing.mdx index 2c20865796bd..cf5f85274736 100644 --- a/website/docs/zh/contribute/development/testing.mdx +++ b/website/docs/zh/contribute/development/testing.mdx @@ -41,7 +41,7 @@ Rspack 的测试用例包括如下: 也可以进入 `tests/rspack-test` 文件夹并运行 `npm run test` 来运行测试用例,并且对测试流程进行更精细的控制: - **需要刷新测试快照时**:添加 `-u` 参数,如 `npm run test -- -u` -- **需要过滤测试用例时**:添加 `-t` 参数,如 `npm run test -- -t configCases/asset` 即可仅运行 `tests/rspack-test/configCases/asset` 文件夹下的用例。匹配支持正则,详见 [rstest](https://rstest.rs/config/test/testNamePattern) +- **需要过滤测试用例时**:添加 `-t` 参数。匹配支持正则,详见 [rstest](https://rstest.rs/config/test/testNamePattern) ### 运行测试 @@ -51,8 +51,11 @@ Rspack 的测试用例包括如下: - 或在 `tests/rspack-test` 目录下运行 `npm run test`。 - 如需更新 snapshot,在 `tests/rspack-test` 目录下运行 `npm run test -- -u`。 - 如需传入特定 rstest cli 参数,在 `tests/rspack-test` 目录下运行 `npm run test -- {args}`。 -- 如需过滤特定测试用例,在 `tests/rspack-test` 目录下运行 `npm run test -- -t path-of-spec`。 - - 如 `npm run test -- -t configCases/asset` 即可仅运行 `tests/rspack-test/configCases/asset` 文件夹下的用例(config 会自动映射到 configCases,其他文件夹类似)。 +- 如需过滤特定测试用例,运行 `npm run test -- -t ${testPath}`,其中 `${testPath}` 可以是绝对路径或相对路径。 + - 例如: + - `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` 测试用例中的所有测试。 + - `npm run test -- -t hotCases/json` (或 `npm run test -- -t /Users/rspack/tests/rspack-test/hotCases/json`) 将运行 `json` 目录下的所有测试用例。 + - `npm run test -- -t hotCases` (或 `npm run test -- -t /Users/rspack/tests/rspack-test/hotCases`) 将运行 `hotCases` 目录下的所有测试用例。 - 如需使用 Rspack Wasm 运行测试用例,需要额外配置以下环境变量: 1. `NAPI_RS_FORCE_WASI=1`: 强制使用 Rspack Wasm 而不是原生绑定 2. `WASM=1`:启用 Wasm 专用的测试配置