You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
If you don't specify any options to the `RuleTester` constructor, then it uses the ESLint defaults (`languageOptions: { ecmaVersion: "latest", sourceType: "module" }`).
797
+
:::
798
+
795
799
The `RuleTester#run()` method is used to run the tests. It should be passed the following arguments:
796
800
797
801
* The name of the rule (string)
@@ -822,12 +826,12 @@ In addition to the properties above, invalid test cases can also have the follow
822
826
If a string is provided as an error instead of an object, the string is used to assert the `message` of the error.
823
827
*`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.
824
828
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:
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`.
909
913
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.
911
915
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.
912
916
913
917
`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`.)
## <aname="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
+
construleTester=newRuleTester({
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.
## <a name="flat-eslint"></a> `FlatESLint` is now `ESLint`
162
213
163
214
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`.
0 commit comments