it("should use strict equality to compare output", () => {
const replaceProgramWith5Rule = {
+ meta: {
+ fixable: "code"
+ },
+
create: context => ({
Program(node) {
context.report({ node, message: "bad", fix: fixer => fixer.replaceText(node, "5") });
}, expectedErrorMessage);
});
+ it("should throw error for empty error array", () => {
+ assert.throws(() => {
+ ruleTester.run("suggestions-messageIds", require("../../fixtures/testers/rule-tester/suggestions").withMessageIds, {
+ valid: [],
+ invalid: [{
+ code: "var foo;",
+ errors: []
+ }]
+ });
+ }, /Invalid cases must have at least one error/u);
+ });
+
+ it("should throw error for errors : 0", () => {
+ assert.throws(() => {
+ ruleTester.run(
+ "suggestions-messageIds",
+ require("../../fixtures/testers/rule-tester/suggestions")
+ .withMessageIds,
+ {
+ valid: [],
+ invalid: [
+ {
+ code: "var foo;",
+ errors: 0
+ }
+ ]
+ }
+ );
+ }, /Invalid cases must have 'error' value greater than 0/u);
+ });
+
it("should not skip column assertion if column is a falsy value", () => {
assert.throws(() => {
ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
{
code: "eval(foo)",
parser: require.resolve("esprima"),
- errors: [{}]
+ errors: [{ line: 1 }]
}
]
});
}, "Error must specify 'messageId' if 'data' is used.");
});
+ // fixable rules with or without `meta` property
+ it("should not throw an error if a rule that has `meta.fixable` produces fixes", () => {
+ const replaceProgramWith5Rule = {
+ meta: {
+ fixable: "code"
+ },
+ create(context) {
+ return {
+ Program(node) {
+ context.report({ node, message: "bad", fix: fixer => fixer.replaceText(node, "5") });
+ }
+ };
+ }
+ };
+
+ ruleTester.run("replaceProgramWith5", replaceProgramWith5Rule, {
+ valid: [],
+ invalid: [
+ { code: "var foo = bar;", output: "5", errors: 1 }
+ ]
+ });
+ });
+ it("should throw an error if a new-format rule that doesn't have `meta` produces fixes", () => {
+ const replaceProgramWith5Rule = {
+ create(context) {
+ return {
+ Program(node) {
+ context.report({ node, message: "bad", fix: fixer => fixer.replaceText(node, "5") });
+ }
+ };
+ }
+ };
+
+ assert.throws(() => {
+ ruleTester.run("replaceProgramWith5", replaceProgramWith5Rule, {
+ valid: [],
+ invalid: [
+ { code: "var foo = bar;", output: "5", errors: 1 }
+ ]
+ });
+ }, "Fixable rules should export a `meta.fixable` property.");
+ });
+ it("should throw an error if a legacy-format rule produces fixes", () => {
+
+ /**
+ * Legacy-format rule (a function instead of an object with `create` method).
+ * @param {RuleContext} context The ESLint rule context object.
+ * @returns {Object} Listeners.
+ */
+ function replaceProgramWith5Rule(context) {
+ return {
+ Program(node) {
+ context.report({ node, message: "bad", fix: fixer => fixer.replaceText(node, "5") });
+ }
+ };
+ }
+
+ assert.throws(() => {
+ ruleTester.run("replaceProgramWith5", replaceProgramWith5Rule, {
+ valid: [],
+ invalid: [
+ { code: "var foo = bar;", output: "5", errors: 1 }
+ ]
+ });
+ }, "Fixable rules should export a `meta.fixable` property.");
+ });
+
describe("suggestions", () => {
it("should pass with valid suggestions (tested using desc)", () => {
ruleTester.run("suggestions-basic", require("../../fixtures/testers/rule-tester/suggestions").basic, {
});
});
+
});