includeSource
- Type:
string[] - Default:
[]
In-source testing is where the test code lives within the same file as the source code, similar to Rust's module tests.
You can define a list of glob patterns that match your in-source test files via includeSource configuration.
In-source testing is usually suitable for small functional functions and utilities, allowing for easy and rapid verification and debugging. For more complex functions and modules, independent test files are recommended.
includeSource works in both Node mode and browser mode projects.
Writing in-source tests
When includeSource defined, Rstest will run all matched files with import.meta.rstest inside.
You can get the Rstest test API via import.meta.rstest.
For production
Put the test code inside the if (import.meta.rstest) block. When generating non-test output, define import.meta.rstest as false in your project's build configuration so the bundler can eliminate the test code as dead code.
- Use Rspack's DefinePlugin:
- Use Rsbuild's source.define option:
TypeScript
To get TypeScript support for import.meta.rstest, we recommend adding @rstest/core/importMeta to compilerOptions.types in the tsconfig.json for your test files:
If your
tsconfig.jsonalready configurescompilerOptions.types, append this type to the existing list.
Alternatively, reference the type in src/rstestEnv.d.ts: