From f0c4b0a75eb4a3157be4c0d4940411e8b727c86b Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Fri, 23 Aug 2024 03:17:08 -0400 Subject: [PATCH 1/2] docs: mention RuleTester static properties --- docs/packages/Rule_Tester.mdx | 80 ++++++++++++++++++++++++++++++----- 1 file changed, 70 insertions(+), 10 deletions(-) diff --git a/docs/packages/Rule_Tester.mdx b/docs/packages/Rule_Tester.mdx index f9f288511ae4..7149977bf66d 100644 --- a/docs/packages/Rule_Tester.mdx +++ b/docs/packages/Rule_Tester.mdx @@ -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): @@ -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; @@ -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 @@ -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'; {InvalidTestCase} + +## 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` for the current file. + +### `it` + +Creates a test closure. + +### `itOnly` + +Only runs this test in the current file. + +### `itSkip` + +Skips running this test in the current file. From 019aa3b83bd0aa132973798069e1acc030f825bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josh=20Goldberg=20=E2=9C=A8?= Date: Wed, 28 Aug 2024 09:21:59 -0400 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Kirk Waiblinger --- docs/packages/Rule_Tester.mdx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/packages/Rule_Tester.mdx b/docs/packages/Rule_Tester.mdx index 7149977bf66d..3eb90fb46d78 100644 --- a/docs/packages/Rule_Tester.mdx +++ b/docs/packages/Rule_Tester.mdx @@ -328,7 +328,7 @@ Creates a test grouping. ### `describeSkip` -Skips running the tests inside this `describe` for the current file. +Skips running the tests inside this `describe`. ### `it` @@ -336,8 +336,8 @@ Creates a test closure. ### `itOnly` -Only runs this test in the current file. +Skips all other tests in the current file. ### `itSkip` -Skips running this test in the current file. +Skips running this test.