Testing
TLDR
run just test-update to run all tests and update snapshots automatically
We have two groups of test suites: one for Rust, and one for Node.js.
Test principle you should respect
- When adding new feature with options, always make sure adding related tests in JavaScript side if possible.
Here is some details about how to choose test technique details\
Summary
just testfor running all tests.just test-updaterunning all tests and update snapshots automaticallyjust test-rustfor running all Rust tests.just test-nodefor running all Node.js tests.just test-node-rolldownfor running only Rolldown's Node.js tests.just test-node-rollupfor running only Rollup's tests.
Rust Tests
Rust tests cases are stored in
/crates/rolldown/tests/esbuild/crates/rolldown/tests/fixtures
How test cases are defined
A test case is a folder that contains _config.json.
_config.json contains the configuration for the test suite. If everything works right, you should be able to have auto-completion while editing _config.json due to the config.
For all available options, you could refer to
https://github.com/rolldown/rolldown/blob/main/crates/rolldown_testing_config/src/lib.rs
https://github.com/rolldown/rolldown/blob/main/crates/rolldown_testing/_config.schema.json
main.jsis the default entry of the test case, ifconfig.inputis not specified in_config.json.Rolldown will bundle the input into
/dist, and execute every entry file in/distorderly. You might thinking it as runningnode --import ./dist/entry1.mjs --import ./dist/entry2.mjs --import ./dist/entry3.mjs --eval "".- If there is a
_test.mjs/_test.cjsin the test case folder, only_test.mjs/_test.cjswill be executed. If you want to execute compiled entries, you need to import them manually in_test.mjs/_test.cjs.
- If there is a
Function-complete tests in Rust
_config.json has it's limitations, so we also support writing tests with Rust directly. You could refer to
Snapshot testing
Rolldown uses insta for Rust snapshot testing. You can use:
cargo insta reviewto review the new snapshot one by one.cargo insta acceptto accept all new snapshots at once.
HMR tests
If a test case folder contains any files named *.hmr-*.js, the test will run in HMR enabled mode.
HMR edit files
- Files that match the pattern
*.hmr-*.jsare called HMR edit files. - These files represent changes to existing source files.
- The part after
hmr-indicates the step number of the change. For example,main.hmr-1.jsmeans a change applied in step 1.
How the test works
- All non-HMR files are copied to a temporary directory.
- An initial build is generated from these files.
- Then, HMR step 1 begins: files with
.hmr-1.jsare used to overwrite the corresponding files in the temporary directory, and an HMR patch is generated. - This process repeats for step 2, 3, and so on. Files like
*.hmr-2.js,*.hmr-3.js, etc., are applied step by step.
Example
If the test folder has these files:
main.jssub.jsmain.hmr-1.jssub.hmr-1.jssub2.hmr-2.js
The test will go through these steps:
- Initial build:
main.js,sub.js - Step 1:
main.jsis replaced withmain.hmr-1.jssub.jsis replaced withsub.hmr-1.js
- Step 2:
main.jsandsub.jsremain as in Step 1sub2.jsis added using the contents ofsub2.hmr-2.js
Node.js Tests
TIP
Make sure to build Node.js bindings before running these tests.
Node.js API tests
Tests located in packages/rolldown/tests are used to test Rolldown's Node.js API (i.e. the API of the rolldown package published on NPM).
It is our goal to align Rolldown's Node.js API with that of Rollup's as much as possible, and the tests are used to verify API alignment and track the progress. Currently, there are many Rollup options that are not yet supported. If you implemented support for additional options from rollup, please add corresponding test cases for them.
just test-node-rolldownwill run rolldown tests.just test-node-rolldown --updatewill run tests and update snapshots.
Run tests of the specific file
To run tests of the specific file, you could use
just test-node-rolldown test-file-nameFor example, to run tests in fixture.test.ts, you could use just test-node-rolldown fixture.
Run the specific test
To run specific test, you could use
just test-node-rolldown -t test-nameNames of tests in fixture.test.ts are defined with their folder names. tests/fixtures/resolve/alias will has test name resolve/alias.
To run the tests/fixtures/resolve/alias test, you could use just test-node-rolldown -t resolve/alias.
INFO
just test-node-rolldown -t aaa bbbis different fromjust test-node-rolldown -t "aaa bbb". The former will run tests that either containsaaaorbbb, while the latter will run tests, whose name containaaa bbb.For more advanced usage, please refer to https://vitest.dev/guide/filtering.
Rollup behavior alignment tests
We also aim for behavior alignment with Rollup by running Rollup's own tests against Rolldown.
To achieve this, each test case in packages/rollup-tests/test proxies to the corresponding test in the rollup git submodule in project root.
The git submodule should have been initialized after running just setup when setting up the project, but you should also run just update-submodule to update the submodule before running the Rollup tests.
In /packages/rollup-tests:
just test-node-rollupwill run rollup tests.just test-node-rollup --updatewill run and update the tests' status.
How to choose test technique
Our Rust test infra is powerful enough to cover most of the case of JavaScript (plugin, passing function inside config). But since JavaScript side user is still our first class user, try to put tests in JavaScript side if possible. Here are some experience about what test technique you should use.
TLDR
Add test in JavaScript side if you don't want to wasting time on deciding which way to use.
Prefer Rust
- Test warning or error emitted by rolldown core.
- Matrix testing, assume you want to test a suite different format, with
configVariantsyou could do that with only one test. - Tests related to linking algorithm(tree shaking, chunk splitting) Those may require a lot of debugging, add test on Rust side could reduce the time of coding-debug-coding work loop.
Prefer JavaScript
Any category not mentioned above should put in JavaScript side.