Skip to content

docs: mention RuleTester static properties #9874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 70 additions & 10 deletions docs/packages/Rule_Tester.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,27 @@ import { RuleTester } from '@typescript-eslint/rule-tester';
RuleTester.afterAll = mocha.after;
```

#### Node.js (`node:test`)

Consider setting up `RuleTester`'s static properties in a preloaded module using the [`--import`](https://nodejs.org/api/cli.html#--importmodule) or [`--require`](https://nodejs.org/api/cli.html#-r---require-module) flag:

```ts
// setup.js
import * as test from 'node:test';
import { RuleTester } from '@typescript-eslint/rule-tester';

RuleTester.afterAll = test.after;
RuleTester.describe = test.describe;
RuleTester.it = test.it;
RuleTester.itOnly = test.it.only;
```

Tests can then be [run from the command line](https://nodejs.org/api/test.html#running-tests-from-the-command-line) like so:

```sh
node --import setup.js --test
```

#### Vitest

Consider setting up `RuleTester`'s static properties in a [`setupFiles` script](https://vitest.dev/config/#setupfiles):
Expand All @@ -241,13 +262,20 @@ RuleTester.itOnly = vitest.it.only;
RuleTester.describe = vitest.describe;
```

#### Node built-in test runner
#### Other Frameworks

Consider setting up `RuleTester`'s static properties in a preloaded module using the [`--import`](https://nodejs.org/api/cli.html#--importmodule) or [`--require`](https://nodejs.org/api/cli.html#-r---require-module) flag:
In general, `RuleTester` can support any test framework that exposes hooks for running code before or after rules.
From `RuleTester`'s [Static Properties](#static-properties), assign any of the following that the running test framework supports.

- `afterAll`
- `describe`
- `it`
- `itOnly`

I.e.:

```ts
// setup.js
import * as test from 'node:test';
import * as test from '...';
import { RuleTester } from '@typescript-eslint/rule-tester';

RuleTester.afterAll = test.after;
Expand All @@ -256,12 +284,6 @@ RuleTester.it = test.it;
RuleTester.itOnly = test.it.only;
```

Tests can then be [run from the command line](https://nodejs.org/api/test.html#running-tests-from-the-command-line) like so:

```sh
node --import setup.js --test
```

## Options

### `RuleTester` constructor options
Expand All @@ -281,3 +303,41 @@ import ValidTestCase from '!!raw-loader!../../packages/rule-tester/src/types/Val
import InvalidTestCase from '!!raw-loader!../../packages/rule-tester/src/types/InvalidTestCase.ts';

<CodeBlock language="ts">{InvalidTestCase}</CodeBlock>

## Static Properties

Each of the following properties may be assigned to as static members of the `RuleTester` class.

For example, to assign `afterAll`:

```ts
import { RuleTester } from '@typescript-eslint/rule-tester';

RuleTester.afterAll = () => {
// ...
};
```

### `afterAll`

Runs after all the tests in this file have completed.

### `describe`

Creates a test grouping.

### `describeSkip`

Skips running the tests inside this `describe`.

### `it`

Creates a test closure.

### `itOnly`

Skips all other tests in the current file.

### `itSkip`

Skips running this test.
Loading