Testing
Because Rspack uses a mix of Rust and Node.js code, different testing strategies are used for each.
Rust testing
Rust test cases are only suitable for unit testing. To test the complete build process, please add Node.js test cases.
Running Rust tests
You can run the Rust code's test cases using ./x test rust or cargo test.
Writing Rust tests
Test cases are written within the Rust code. For example:
For more information, please refer to: Rust: How to Write Tests
Node.js testing
Rspack's test cases include the following:
- Rspack core test cases are stored in the
tests/rspack-testfolder and will run the test cases by simulating the build process. In general, test cases should be added in this folder. - Test cases for other Rspack packages are stored in the
packages/{name}/testsfolder and should only be added or modified when modifying that package.
You can run Rspack tests by running ./x test unit or pnpm run test:unit at the root folder.
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, likenpm run test -- -u - When filtering test cases is needed: Add
-t, likenpm run test -- -t configCases/assetto only run test cases from thetests/rspack-test/configCases/assetfolder. Pattern matching supports regex, see rstest for details.
Running tests
You can run these test cases in the following ways:
- Run
./x test unitorpnpm run test:unitfrom the root directory. - Or run
npm run testfrom thetests/rspack-testdirectory. - To update snapshots, run
npm run test -- -ufrom thetests/rspack-testdirectory. - To pass specific rstest cli arguments, run
npm run test -- {args}from thetests/rspack-testdirectory. - To filter specific test cases, run
npm run test -- -t path-of-specfrom thetests/rspack-testdirectory.- Like
npm run test -- -t configCases/assetto only run test cases from thetests/rspack-test/configCases/assetfolder (config will be automatically mapped to configCases, and other folders will work in a similar way).
- Like
- To use Rspack Wasm for running test cases, you need to additionally configure the following environment variables:
NAPI_RS_FORCE_WASI=1: Forces the use of Rspack Wasm instead of native bindingWASM=1: Enables Wasm-specific test configurationsNODE_NO_WARNINGS=1: Disables Node.js Wasm warnings.
Directory structure
The structure of the tests/rspack-test folder is as follows:
The {Name}.test.js is the entry file for tests, which will walk the {name}Cases folder and run cases in it. Therefore, when you need to add or modify test cases, add them to the relevant {name}Cases folder based on the type of testing.
Test types
The existing test types are:
- Normal: Used to test core build processes without configuration changes. This type is used when testing does not require adding
rspack.config.js. - Config: Used to test build configuration options. If your test needs specific configuration added through
rspack.config.jsto run and does not fit other scenarios, use this test type. - Hot: Used to test whether HMR runs correctly. This type includes HotNode with a fixed
target=async-node, HotWeb with a fixedtarget=web, and HotWorker with a fixedtarget=webworker. - HotSnapshot: Used to test whether HMR can generate correct intermediate artifacts. This test type shares test cases with the Hot type and generates snapshots for incremental artifacts for each HMR.
- Watch: Used to test incremental compilation after modifying files in Watch mode.
- StatsOutput: Used to test the console output log after the build ends.
- StatsAPI: Used to test the Stats object generated after the build ends.
- Diagnostic: Used to test the formatted output information for warnings/errors generated during the build process.
- Hash: Used to test whether hash generation works correctly.
- Compiler: Used to test Compiler/Compilation object APIs.
- Defaults: Used to test the interaction between configuration options.
- Error: Used to test the interaction between
compilation.errorsandcompilation.warnings. - Hook: Used to test various hook functionalities.
- TreeShaking: Used to test Tree Shaking-related features.
- Builtin: Used to test plugins with built-in native implementations.
Please prioritize adding test cases within the above test types.
Normal
The writing of the case is the same as a regular rspack project, but it does not include the rspack.config.js file and will use the provided configuration for building.
Config
This test case is similar to a regular rspack project. You can specify the build configuration by adding a rspack.config.js and control various behaviors during testing by adding a test.config.js. The structure of the test.config.js file is as follows:
Hot
This test case is similar to a regular rspack project. You can specify the build configuration by adding a rspack.config.js.
And also, within the file that has changed, use --- to separate the code before and after the change:
In the test case code, use the NEXT_HMR method to control the timing of file changes and add test code within it:
HotSnapshot
Uses the same test cases as Hot{Target}, and generates a __snapshots__/{target}/{step}.snap.txt file in the case folder to perform snapshot testing on the incremental artifacts of each HMR.
The snapshot structure is as follows:
- Changed Files: Source code files that trigger this HMR build
- Asset Files: Artifact files of this HMR build
- Manifest: Contents of the
hot-update.jsonmetadata file for this HMR build, where:"c": Id of the chunks to be updated in this HMR"r": Id of the chunks to be removed in this HMR"m": Id of the modules to be removed in this HMR
- Update: Information about the
hot-update.jspatch file for this HMR build, including:- Changed Modules: List of modules included in the patch
- Changed Runtime Modules: List of runtime modules included in the patch
- Changed Content: Snapshot of the patch code
Watch
As the Watch build needs to be performed in multiple steps, you can specify the build configuration by adding a rspack.config.js. The directory structure of its cases is special and will use incrementing numbers to represent change batches:
In the test code, you can use the WATCH_STEP variable to get the current batch number of changes.
StatsOutput
The writing of the cases is the same as in a regular rspack project. After running, the console output information will be captured in snapshots and stored in tests/rspack-test/__snapshots__/StatsOutput.test.js.snap.
As some StatsOutput test cases contain hashes, when you modify the output code, please use the -u parameter to update the snapshots for these cases.
Stats API
This test uses tests/rspack-test/fixtures as the source code for the build, so the test case is written as a single file. Its structure is as follows:
Diagnostic
This test case is similar to a typical rspack project and can specify build configurations by adding a rspack.config.js. Additionally, it will add a stats.err file in the case directory to store snapshots of warnings/errors. To refresh, use the -u parameter.
Hash
This test case is similar to a typical rspack project, but it will add a test.config.js file in the case directory and specify a validate() method to check the hash information in the stats object after the build is complete:
Compiler
This test uses tests/rspack-test/fixtures as the source code for the build, so the test case is written as a single file. Its structure is as follows:
Defaults
This test does not execute real builds; it only generates build configurations and observes the differences from the default configuration. The basic default configuration will be snapshot and stored in tests/rspack-test/__snapshots__/Defaults.test.js.snap.
This test uses tests/rspack-test/fixtures as the source code for the build, so the test case is written as a single file. Its structure is as follows:
The details for the Error test are as follows:
Error
This test uses tests/rspack-test/fixtures as the source code for the build, so the test case is written as a single file. Its structure is as follows:
Hook
This test records the input and output of the hook and stores it in the snapshot hooks.snap.txt. The snapshot of the final product code is stored in output.snap.txt.
This test uses tests/rspack-test/fixtures as the source code for the build, so the test case is written as a single file. Its structure is as follows:
TreeShaking
In this test case, the configuration is similar to a regular rspack project. You can specify the build configuration by adding a rspack.config.js, but the final product is snapshot and stored in __snapshots__/treeshaking.snap.txt.
Builtin
This test case is similar to a regular rspack project, and you can specify the build configuration by adding a rspack.config.js. However, depending on the directory, different snapshots of the products will be generated and stored in __snapshots__/output.snap.txt:
- plugin-css: Snapshots of files with a
.cssextension - plugin-css-modules: Snapshots of files with
.cssand.jsextensions - plugin-html: Snapshots of files with
.htmlextension - Other: Snapshots of files with
.jsextension

