Skip to content

Commit 946ae00

Browse files
nzakasmdjermanovic
andauthored
feat!: FlatRuleTester -> RuleTester (#17922)
* feat!: FlatRuleTester -> RuleTester refs #13481 * Update docs/src/use/migrate-to-9.0.0.md Co-authored-by: Milos Djermanovic <[email protected]> * Update docs/src/use/migrate-to-9.0.0.md Co-authored-by: Milos Djermanovic <[email protected]> * Update docs/src/use/migrate-to-9.0.0.md Co-authored-by: Milos Djermanovic <[email protected]> --------- Co-authored-by: Milos Djermanovic <[email protected]>
1 parent f182114 commit 946ae00

File tree

299 files changed

+992
-4933
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

299 files changed

+992
-4933
lines changed

docs/src/integrate/nodejs-api.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -789,9 +789,13 @@ ruleTester.run("my-rule", rule, {
789789
The `RuleTester` constructor accepts an optional object argument, which can be used to specify defaults for your test cases. For example, if all of your test cases use ES2015, you can set it as a default:
790790

791791
```js
792-
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015 } });
792+
const ruleTester = new RuleTester({ languageOptions: { ecmaVersion: 2015 } });
793793
```
794794

795+
::: tip
796+
If you don't specify any options to the `RuleTester` constructor, then it uses the ESLint defaults (`languageOptions: { ecmaVersion: "latest", sourceType: "module" }`).
797+
:::
798+
795799
The `RuleTester#run()` method is used to run the tests. It should be passed the following arguments:
796800

797801
* The name of the rule (string)
@@ -822,12 +826,12 @@ In addition to the properties above, invalid test cases can also have the follow
822826
If a string is provided as an error instead of an object, the string is used to assert the `message` of the error.
823827
* `output` (string, required if the rule fixes code): Asserts the output that will be produced when using this rule for a single pass of autofixing (e.g. with the `--fix` command line flag). If this is `null`, asserts that none of the reported problems suggest autofixes.
824828

825-
Any additional properties of a test case will be passed directly to the linter as config options. For example, a test case can have a `parserOptions` property to configure parser behavior:
829+
Any additional properties of a test case will be passed directly to the linter as config options. For example, a test case can have a `languageOptions` property to configure parser behavior:
826830

827831
```js
828832
{
829833
code: "let foo;",
830-
parserOptions: { ecmaVersion: 2015 }
834+
languageOptions: { ecmaVersion: 2015 }
831835
}
832836
```
833837

@@ -907,7 +911,7 @@ ruleTester.run("my-rule-for-no-foo", rule, {
907911

908912
If `RuleTester.itOnly` has been set to a function value, `RuleTester` will call `RuleTester.itOnly` instead of `RuleTester.it` to run cases with `only: true`. If `RuleTester.itOnly` is not set but `RuleTester.it` has an `only` function property, `RuleTester` will fall back to `RuleTester.it.only`.
909913

910-
2. Otherwise, if `describe` and `it` are present as globals, `RuleTester` will use `global.describe` and `global.it` to run tests and `global.it.only` to run cases with `only: true`. This allows `RuleTester` to work when using frameworks like [Mocha](https://mochajs.org/) without any additional configuration.
914+
2. Otherwise, if `describe` and `it` are present as globals, `RuleTester` will use `globalThis.describe` and `globalThis.it` to run tests and `globalThis.it.only` to run cases with `only: true`. This allows `RuleTester` to work when using frameworks like [Mocha](https://mochajs.org/) without any additional configuration.
911915
3. Otherwise, `RuleTester#run` will simply execute all of the tests in sequence, and will throw an error if one of them fails. This means you can simply execute a test file that calls `RuleTester.run` using `Node.js`, without needing a testing framework.
912916

913917
`RuleTester#run` calls the `describe` function with two arguments: a string describing the rule, and a callback function. The callback calls the `it` function with a string describing the test case, and a test function. The test function will return successfully if the test passes, and throw an error if the test fails. The signature for `only` is the same as `it`. `RuleTester` calls either `it` or `only` for every case even when some cases have `only: true`, and the test framework is responsible for implementing test case exclusivity. (Note that this is the standard behavior for test suites when using frameworks like [Mocha](https://mochajs.org/); this information is only relevant if you plan to customize `RuleTester.describe`, `RuleTester.it`, or `RuleTester.itOnly`.)

docs/src/use/migrate-to-9.0.0.md

+51
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ The lists below are ordered roughly by the number of users each change is expect
2828
* [Removed `sourceCode.getComments()`](#removed-sourcecode-getcomments)
2929
* [Function-style rules are no longer supported](#drop-function-style-rules)
3030
* [`meta.schema` is required for rules with options](#meta-schema-required)
31+
* [`FlatRuleTester` is now `RuleTester`](#flat-rule-tester)
3132

3233
### Breaking changes for integration developers
3334

@@ -158,6 +159,56 @@ The [eslint-plugin/require-meta-schema](https://github.com/eslint-community/esli
158159

159160
**Related Issues(s):** [#14709](https://github.com/eslint/eslint/issues/14709)
160161

162+
## <a name="flat-rule-tester"></a> `FlatRuleTester` is now `RuleTester`
163+
164+
As announced in our [blog post](/blog/2023/10/flat-config-rollout-plans/), the temporary `FlatRuleTester` class has been renamed to `RuleTester`, while the `RuleTester` class from v8.x has been removed. Additionally, the `FlatRuleTester` export from `eslint/use-at-your-own-risk` has been removed.
165+
166+
**To address:** Update your rule tests to use the new `RuleTester`. To do so, here are some of the common changes you'll need to make:
167+
168+
* **Be aware of new defaults for `ecmaVersion` and `sourceType`.** By default, `RuleTester` uses the flat config default of `ecmaVersion: "latest"` and `sourceType: "module"`. This may cause some tests to break if they were expecting the old default of `ecmaVersion: 5` and `sourceType: "script"`. If you'd like to use the old default, you'll need to manually specify that in your `RuleTester` like this:
169+
170+
```js
171+
// use eslintrc defaults
172+
const ruleTester = new RuleTester({
173+
languageOptions: {
174+
ecmaVersion: 5,
175+
sourceType: "script"
176+
}
177+
});
178+
```
179+
180+
* **Change `parserOptions` to `languageOptions`.** If you're setting `ecmaVersion` or `sourceType` on your tests, move those from `parserOptions` to `languageOptions`, like this:
181+
182+
```js
183+
ruleTester.run("my-rule", myRule, {
184+
valid: [
185+
{
186+
code: "foo",
187+
parserOptions: {
188+
ecmaVersion: 6
189+
}
190+
}
191+
]
192+
});
193+
194+
// becomes
195+
196+
ruleTester.run("my-rule", myRule, {
197+
valid: [
198+
{
199+
code: "foo",
200+
languageOptions: {
201+
ecmaVersion: 6
202+
}
203+
}
204+
]
205+
});
206+
```
207+
208+
* **Translate other config keys.** Keys such as `env` and `parser` that used to run on the eslintrc config system must be translated into the flat config system. Please refer to the [Configuration Migration Guide](configure/migration-guide) for details on translating other keys you may be using.
209+
210+
**Related Issues(s):** [#13481](https://github.com/eslint/eslint/issues/13481)
211+
161212
## <a name="flat-eslint"></a> `FlatESLint` is now `ESLint`
162213
163214
As announced in our [blog post](/blog/2023/10/flat-config-rollout-plans/), the temporary `FlatESLint` class has been renamed to `ESLint`, while the `ESLint` class from v8.x has been renamed to `LegacyESLint`.

lib/api.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
const { ESLint } = require("./eslint/eslint");
1313
const { Linter } = require("./linter");
14-
const { FlatRuleTester } = require("./rule-tester");
14+
const { RuleTester } = require("./rule-tester");
1515
const { SourceCode } = require("./source-code");
1616

1717
//-----------------------------------------------------------------------------
@@ -21,6 +21,6 @@ const { SourceCode } = require("./source-code");
2121
module.exports = {
2222
Linter,
2323
ESLint,
24-
RuleTester: FlatRuleTester,
24+
RuleTester,
2525
SourceCode
2626
};

0 commit comments

Comments
 (0)