string[]
[]
源码内联测试(In-source testing)指的是测试代码与源代码写在同一个文件中,类似于 Rust 的模块测试。
你可以通过 includeSource
配置,定义一组用于匹配内联测试文件的 glob 模式列表。
源码内联测试通常适用于小型功能函数和工具方法,能够方便地进行快速验证和调试。对于更复杂的功能和模块,建议使用独立的测试文件。
当定义了 includeSource
后,Rstest 会运行所有通过 glob 匹配且包含 import.meta.rstest
的文件。
你可以通过 import.meta.rstest
获取 Rstest 的测试 API。
将测试代码写在 if (import.meta.rstest)
代码块内,并在你的构建配置(如 rsbuild.config.ts
)中将 import.meta.rstest
定义为 undefined
,这样将有助于打包工具消除无用代码。
如需让 TypeScript 支持 import.meta.rstest
,可在 tsconfig.json
中添加 @rstest/core/importMeta
: