For AI agents: the complete documentation index is available at https://rstest.rs/zh/llms.txt, the full documentation bundle is available at https://rstest.rs/zh/llms-full.txt, and this page is available as Markdown at https://rstest.rs/zh/config/test/include.md.
close
  • 简体中文
  • include

    • 类型: string[]
    • 默认值: ['**/*.{test,spec}.?(c|m)[jt]s?(x)']
    • CLI: --include '**/*.test.ts' --include '**/*.spec.ts'

    匹配测试文件的 glob 规则列表,相对于 root(默认为 process.cwd())解析。设置 include 会覆盖默认匹配规则。

    CLI
    rstest.config.ts
    npx rstest --include '**/*.{test,spec}.?(c|m)[jt]s?(x)'

    运行 npx rstest list --filesOnly 可以查看实际匹配到的测试文件:

    $ npx rstest list --filesOnly
    
    a.test.ts
    b.test.ts

    默认行为

    默认情况下,Rstest 会匹配文件名中包含 .test. 或 .spec. 的 JavaScript / TypeScript 文件:

    ├── __tests__
    │   └── index.test.js # test
    │   └── App.spec.tsx # test
    │   └── helper.ts # not test
    ├── src
    │   └── component.ts # not test
    │   └── component.test.tsx # test
    ├── bar.spec.jsx # test
    ├── index.test.js # test
    └── index.js # not test

    不含目录前缀的模式(如 index.test.ts)只会匹配根目录下的文件。需要匹配所有子目录时,使用 **/index.test.ts:

    import { defineConfig } from '@rstest/core';
    
    export default defineConfig({
    -  include: ['index.test.ts'],
    +  include: ['**/index.test.ts'],
    });

    匹配单个扩展名时应写成 **/*.ts,不要写成 **/*.{ts}。底层 glob 库会把单元素花括号按字面量处理,**/*.{ts} 不会匹配 .ts 文件。