Skip to content

Commit 47e60f8

Browse files
authored
feat!: Stricter rule test validations (#17654)
* feat!: rule tester require suggestion matchers * feat!: rule tester require message or messageId * feat!: rule tester require output for suggestions * feat!: rule tester only allow desc or messageId in suggestion matchers * feat!: rule tester require suggestion output differs from original source code * feat!: rule tester typecheck only and filename test case properties * feat!: rule tester check whether code and output is equal * docs: update rule tester documentation to reflect additional assertions * fix: fix invalid test cases of builtin rules * fix: remove unnecessary space for the suggestion prefix * chore: fixing merge diversions * fix: remove incorrect claim that data is required if messageId with placeholders is used * chore: remove potentially confusing differentation between missing and failing fixer * fix: better explanation for missing suggestions * feat: support specifying an suggestion amount * fix: ecmaVersion should be a languageOptions property (not parserOptions) * chore: tweak message for omitting output if there is no autofix * chore: fixup using old error message for the output property and update its docs * fix: fixup reported error messages * fix: refine more assertion messages * docs: document suggestion can also be a number * chore: simplify suggestion existence checks * feat: check string and regexp error matchers for missing suggestion matchers * chore: cleanup message for missing suggestions when testing only the message
1 parent 1a94589 commit 47e60f8

File tree

10 files changed

+404
-181
lines changed

10 files changed

+404
-181
lines changed

docs/src/integrate/nodejs-api.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -735,19 +735,19 @@ A test case is an object with the following properties:
735735

736736
In addition to the properties above, invalid test cases can also have the following properties:
737737

738-
* `errors` (number or array, required): Asserts some properties of the errors that the rule is expected to produce when run on this code. If this is a number, asserts the number of errors produced. Otherwise, this should be a list of objects, each containing information about a single reported error. The following properties can be used for an error (all are optional):
739-
* `message` (string/regexp): The message for the error
740-
* `messageId` (string): The Id for the error. See [testing errors with messageId](#testing-errors-with-messageid) for details
738+
* `errors` (number or array, required): Asserts some properties of the errors that the rule is expected to produce when run on this code. If this is a number, asserts the number of errors produced. Otherwise, this should be a list of objects, each containing information about a single reported error. The following properties can be used for an error (all are optional unless otherwise noted):
739+
* `message` (string/regexp): The message for the error. Must provide this or `messageId`
740+
* `messageId` (string): The Id for the error. Must provide this or `message`. See [testing errors with messageId](#testing-errors-with-messageid) for details
741741
* `data` (object): Placeholder data which can be used in combination with `messageId`
742742
* `type` (string): The type of the reported AST node
743743
* `line` (number): The 1-based line number of the reported location
744744
* `column` (number): The 1-based column number of the reported location
745745
* `endLine` (number): The 1-based line number of the end of the reported location
746746
* `endColumn` (number): The 1-based column number of the end of the reported location
747-
* `suggestions` (array): An array of objects with suggestion details to check. See [Testing Suggestions](#testing-suggestions) for details
747+
* `suggestions` (array): An array of objects with suggestion details to check. Required if the rule produces suggestions. See [Testing Suggestions](#testing-suggestions) for details
748748

749749
If a string is provided as an error instead of an object, the string is used to assert the `message` of the error.
750-
* `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.
750+
* `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` or omitted, asserts that none of the reported problems suggest autofixes.
751751

752752
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:
753753

@@ -784,12 +784,12 @@ Please note that `data` in a test case does not assert `data` passed to `context
784784

785785
### Testing Suggestions
786786

787-
Suggestions can be tested by defining a `suggestions` key on an errors object. The options to check for the suggestions are the following (all are optional):
787+
Suggestions can be tested by defining a `suggestions` key on an errors object. If this is a number, it asserts the number of suggestions provided for the error. Otherwise, this should be an array of objects, each containing information about a single provided suggestion. The following properties can be used:
788788

789-
* `desc` (string): The suggestion `desc` value
790-
* `messageId` (string): The suggestion `messageId` value for suggestions that use `messageId`s
791-
* `data` (object): Placeholder data which can be used in combination with `messageId`
792-
* `output` (string): A code string representing the result of applying the suggestion fix to the input code
789+
* `desc` (string): The suggestion `desc` value. Must provide this or `messageId`
790+
* `messageId` (string): The suggestion `messageId` value for suggestions that use `messageId`s. Must provide this or `desc`
791+
* `data` (object): Placeholder data which can be used in combination with `messageId`.
792+
* `output` (string, required): A code string representing the result of applying the suggestion fix to the input code
793793

794794
Example:
795795

lib/rule-tester/rule-tester.js

Lines changed: 83 additions & 67 deletions
Large diffs are not rendered by default.

tests/fixtures/testers/rule-tester/suggestions.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,37 @@ module.exports.withoutHasSuggestionsProperty = {
164164
};
165165
}
166166
};
167+
168+
module.exports.withFixerWithoutChanges = {
169+
meta: { hasSuggestions: true },
170+
create(context) {
171+
return {
172+
Identifier(node) {
173+
if (node.name === "foo") {
174+
context.report({
175+
node,
176+
message: "Avoid using identifiers named 'foo'.",
177+
suggest: [{
178+
desc: "Rename identifier 'foo' to 'bar'",
179+
fix: fixer => fixer.replaceText(node, 'foo')
180+
}]
181+
});
182+
}
183+
}
184+
};
185+
}
186+
};
187+
188+
module.exports.withFailingFixer = {
189+
create(context) {
190+
return {
191+
Identifier(node) {
192+
context.report({
193+
node,
194+
message: "some message",
195+
suggest: [{ desc: "some suggestion", fix: fixer => null }]
196+
});
197+
}
198+
};
199+
}
200+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy