> For AI agents: the complete documentation index is available at /zh/llms.txt, the full documentation bundle is available at /zh/llms-full.txt.

# Rslint

[Rslint](https://rslint.rs/) 是一个兼容 ESLint 的 JavaScript 和 TypeScript linter。它内置的 `rstestPlugin` 提供了针对 Rstest 测试定义、断言、mock 和 snapshot 的 rules，可以帮助你发现常见问题。

在项目中安装 [@rslint/core](https://github.com/web-infra-dev/rslint)，然后在 `rslint.config.ts` 中启用 recommended preset：


```sh [npm]
npm add @rslint/core -D
```

```sh [yarn]
yarn add @rslint/core -D
```

```sh [pnpm]
pnpm add @rslint/core -D
```

```sh [bun]
bun add @rslint/core -D
```

```sh [deno]
deno add npm:@rslint/core -D
```

## 独立的测试文件

如果测试文件使用 `*.test.ts` 或 `*.spec.ts` 这样的文件名，可以将 preset 限定到 test 和 spec 文件：

```ts title="rslint.config.ts"
import { defineConfig, rstestPlugin } from '@rslint/core';

export default defineConfig([
  {
    ...rstestPlugin.configs.recommended,
    files: ['**/*.{test,spec}.{js,jsx,ts,tsx,cjs,cts,mjs,mts}'],
  },
]);
```

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

## In-source tests

使用 [in-source testing](/zh/config/test/include-source.md) 时，测试代码与生产代码写在同一个文件中，并通过 `import.meta.rstest` 使用测试 API。将 preset 应用到所有需要 lint 的文件，才能同时检查 in-source tests 和独立的测试文件：

```ts title="rslint.config.ts"
import { defineConfig, rstestPlugin } from '@rslint/core';

export default defineConfig([rstestPlugin.configs.recommended]);
```

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

## 运行 Rslint

在项目根目录运行 Rslint：


```sh [npm]
npm exec rslint
```

```sh [yarn]
yarn exec rslint
```

```sh [pnpm]
pnpm exec rslint
```

```sh [bun]
bun exec rslint
```

```sh [deno]
deno exec npm:rslint
```

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

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

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

```text
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.json` 的 `scripts`，再通过 package manager 的 lint script 运行。

## 查看 rules

[Rslint 的 `rstestPlugin` preset](https://rslint.rs/api/presets/rstest-plugin) 介绍了配置方式和使用场景。要查看 recommended preset 包含哪些 rules，请打开 [Rstest rules 列表](https://rslint.rs/rules/?preset=rstestPlugin.configs.recommended)。
