8000 docs: mention RuleTester static properties (#9874) · abrahamguo/typescript-eslint@cef0912 · GitHub
[go: up one dir, main page]

Skip to content

Commit cef0912

Browse files
docs: mention RuleTester static properties (typescript-eslint#9874)
* docs: mention RuleTester static properties * Apply suggestions from code review Co-authored-by: Kirk Waiblinger <kirk.waiblinger@gmail.com> --------- Co-authored-by: Kirk Waiblinger <kirk.waiblinger@gmail.com>
1 parent 2421575 commit cef0912

File tree

1 file changed

+70
-10
lines changed

1 file changed

+70
-10
lines changed

docs/packages/Rule_Tester.mdx

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,27 @@ import { RuleTester } from '@typescript-eslint/rule-tester';
225225
RuleTester.afterAll = mocha.after;
226226
```
227227
228+
#### Node.js (`node:test`)
229+
230+
Consider setting up `RuleTester`'s static properties in a preloaded module using the [`--import`](https://nodejs.org/api/cli.html#--importmodule< 8000 /span>) or [`--require`](https://nodejs.org/api/cli.html#-r---require-module) flag:
231+
232+
```ts
233+
// setup.js
234+
import * as test from 'node:test';
235+
import { RuleTester } from '@typescript-eslint/rule-tester';
236+
237+
RuleTester.afterAll = test.after;
238+
RuleTester.describe = test.describe;
239+
RuleTester.it = test.it;
240+
RuleTester.itOnly = test.it.only;
241+
```
242+
243+
Tests can then be [run from the command line](https://nodejs.org/api/test.html#running-tests-from-the-command-line) like so:
244+
245+
```sh
246+
node --import setup.js --test
247+
```
248+
228249
#### Vitest
229250

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

244-
#### Node built-in test runner
265+
#### Other Frameworks
245266

246-
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:
267+
In general, `RuleTester` can support any test framework that exposes hooks for running code before or after rules.
268+
From `RuleTester`'s [Static Properties](#static-properties), assign any of the following that the running test framework supports.
269+
270+
- `afterAll`
271+
- `describe`
272+
- `it`
273+
- `itOnly`
274+
275+
I.e.:
247276

248277
```ts
249-
// setup.js
250-
import * as test from 'node:test';
278+
import * as test from '...';
251279
import { RuleTester } from '@typescript-eslint/rule-tester';
252280

253281
RuleTester.afterAll = test.after;
@@ -256,12 +284,6 @@ RuleTester.it = test.it;
256284
RuleTester.itOnly = test.it.only;
257285
```
258286

259-
Tests can then be [run from the command line](https://nodejs.org/api/test.html#running-tests-from-the-command-line) like so:
260-
261-
```sh
262-
node --import setup.js --test
263-
```
264-
265287
## Options
266288

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

283305
<CodeBlock language="ts">{InvalidTestCase}</CodeBlock>
306+
307+
## Static Properties
308+
309+
Each of the following properties may be assigned to as static members of the `RuleTester` class.
310+
311+
For example, to assign `afterAll`:
312+
313+
```ts
314+
import { RuleTester } from '@typescript-eslint/rule-tester';
315+
316+
RuleTester.afterAll = () => {
317+
// ...
318+
};
319+
```
320+
321+
### `afterAll`
322+
323+
Runs after all the tests in this file have completed.
324+
325+
### `describe`
326+
327+
Creates a test grouping.
328+
329+
### `describeSkip`
330+
331+
Skips running the tests inside this `describe`.
332+
333+
### `it`
334+
335+
Creates a test closure.
336+
337+
### `itOnly`
338+
339+
Skips all other tests in the current file.
340+
341+
### `itSkip`
342+
343+
Skips running this test.

0 commit comments

Comments
 (0)
0