For AI agents: the complete documentation index is available at /zh/llms.txt, the full documentation bundle is available at /zh/llms-full.txt, and this page is available as Markdown at /zh/guide/integration/rslint.md.
close
  • 简体中文
  • Rslint

    Rslint 是一个兼容 ESLint 的 JavaScript 和 TypeScript linter。它内置的 rstestPlugin 提供了针对 Rstest 测试定义、断言、mock 和 snapshot 的 rules,可以帮助你发现常见问题。

    在项目中安装 @rslint/core,然后在 rslint.config.ts 中启用 recommended preset:

    npm
    yarn
    pnpm
    bun
    deno
    npm add @rslint/core -D

    独立的测试文件

    如果测试文件使用 *.test.ts*.spec.ts 这样的文件名,可以将 preset 限定到与 Rstest 默认发现范围一致的文件模式:

    rslint.config.ts
    import { defineConfig, rstestPlugin } from '@rslint/core';
    
    export default defineConfig([
      {
        ...rstestPlugin.configs.recommended,
        files: [
          '**/*.{test,spec}.{js,jsx,ts,tsx,cjs,cjsx,cts,ctsx,mjs,mjsx,mts,mtsx}',
        ],
      },
    ]);

    这个模式覆盖 .js.jsx.ts.tsx.cjs.cjsx.cts.ctsx.mjs.mjsx.mts.mtsx 文件。recommended preset 本身没有声明 files 匹配模式。加上这个模式后,Rstest 专用 rules 只会检查测试文件,生产代码仍然可以由其他 Rslint 配置项负责。

    In-source tests

    使用 in-source testing 时,测试代码与生产代码写在同一个文件中,并通过 import.meta.rstest 使用测试 API。将 preset 应用到所有需要 lint 的文件,才能同时检查 in-source tests 和独立的测试文件:

    rslint.config.ts
    import { defineConfig, rstestPlugin } from '@rslint/core';
    
    export default defineConfig([rstestPlugin.configs.recommended]);

    对于 import.meta.rstest,不需要额外声明 Rslint globals。

    运行 Rslint

    在项目根目录运行 Rslint:

    npm
    yarn
    pnpm
    bun
    deno
    npm exec rslint

    运行成功后,会输出类似下面的汇总信息:

    start   Linting...
    success Lint passed in 92ms (1 file, 14 rules, 14 threads)

    如果 Rslint 发现问题,会输出 rule 名称、源码位置、代码片段和失败汇总信息:

    start   Linting...
    
      rstest/no-identical-title  — [error] Test title is used multiple times in the same describe block
      ╭─┴──────────( invalid.test.ts:8:8 )─────
      │ 7 │
      │ 8 │  test('same title', () => {
      │ 9 │    expect(false).toBeFalsy();
      ╰────────────────────────────────
    
    error   Lint failed with 1 error in 31ms (1 file, 14 rules, 14 threads)

    也可以把这条命令加入 package.jsonscripts,再通过 package manager 的 lint script 运行。

    查看 rules

    Rslint 的 rstestPlugin preset 介绍了配置方式和使用场景。要查看 recommended preset 包含哪些 rules,请打开 Rstest rules 列表