Repository: eslint/eslint
File: tests/lib/rules/no-console.js (JavaScript)

Code before:

const ruleTester = new RuleTester();

ruleTester.run("no-console", rule, {
    valid: [
        "Console.info(foo)",

        // single array item
        { code: "console.info(foo)", options: [{ allow: ["info"] }] },
        { code: "console.warn(foo)", options: [{ allow: ["warn"] }] },
        { code: "console.error(foo)", options: [{ allow: ["error"] }] },
        { code: "console.log(foo)", options: [{ allow: ["log"] }] },

        // multiple array items
        { code: "console.info(foo)", options: [{ allow: ["warn", "info"] }] },
        { code: "console.warn(foo)", options: [{ allow: ["error", "warn"] }] },
        { code: "console.error(foo)", options: [{ allow: ["log", "error"] }] },
        { code: "console.log(foo)", options: [{ allow: ["info", "log", "warn"] }] },

        // https://github.com/eslint/eslint/issues/7010
        "var console = require('myconsole'); console.log(foo)"
    ],
    invalid: [

        // no options
        { code: "console.log(foo)", errors: [{ messageId: "unexpected", type: "MemberExpression", suggestions: [{ messageId: "removeConsole", data: { propertyName: "log" }, output: "" }] }] },
        { code: "console.error(foo)", errors: [{ messageId: "unexpected", type: "MemberExpression", suggestions: [{ messageId: "removeConsole", data: { propertyName: "error" }, output: "" }] }] },
        { code: "console.info(foo)", errors: [{ messageId: "unexpected", type: "MemberExpression", suggestions: [{ messageId: "removeConsole", data: { propertyName: "info" }, output: "" }] }] },
        { code: "console.warn(foo)", errors: [{ messageId: "unexpected", type: "MemberExpression", suggestions: [{ messageId: "removeConsole", data: { propertyName: "warn" }, output: "" }] }] },
        { code: "switch (a) { case 1: console.log(foo) }", errors: [{ messageId: "unexpected", type: "MemberExpression", suggestions: [{ messageId: "removeConsole", data: { propertyName: "log" }, output: "switch (a) { case 1:  }" }] }] },
        { code: "if (a) { console.warn(foo) }", errors: [{ messageId: "unexpected", type: "MemberExpression", suggestions: [{ messageId: "removeConsole", data: { propertyName: "warn" }, output: "if (a) {  }" }] }] },
        { code: "if (a) console.warn(foo)", errors: [{ messageId: "unexpected", type: "MemberExpression" }] },
        { code: "foo(console.log)", errors: [{ messageId: "unexpected", type: "MemberExpression" }] },
        { code: "class A { static { console.info(foo) } }", parserOptions: { ecmaVersion: "latest" }, errors: [{ messageId: "unexpected", type: "MemberExpression", suggestions: [{ messageId: "removeConsole", data: { propertyName: "info" }, output: "class A { static {  } }" }] }] },

        //  one option
        { code: "console.log(foo)", options: [{ allow: ["error"] }], errors: [{ messageId: "unexpected", type: "MemberExpression", suggestions: [{ messageId: "removeConsole", data: { propertyName: "log" }, output: "" }] }] },
        { code: "console.error(foo)", options: [{ allow: ["warn"] }], errors: [{ messageId: "unexpected", type: "MemberExpression", suggestions: [{ messageId: "removeConsole", data: { propertyName: "error" }, output: "" }] }] },
        { code: "console.info(foo)", options: [{ allow: ["log"] }], errors: [{ messageId: "unexpected", type: "MemberExpression", suggestions: [{ messageId: "removeConsole", data: { propertyName: "info" }, output: "" }] }] },
        { code: "console.warn(foo)", options: [{ allow: ["error"] }], errors: [{ messageId: "unexpected", type: "MemberExpression", suggestions: [{ messageId: "removeConsole", data: { propertyName: "warn" }, output: "" }] }] },
        { code: "switch (a) { case 1: console.log(foo) }", options: [{ allow: ["error"] }], errors: [{ messageId: "unexpected", type: "MemberExpression", suggestions: [{ messageId: "removeConsole", data: { propertyName: "log" }, output: "switch (a) { case 1:  }" }] }] },
        { code: "if (a) { console.info(foo) }", options: [{ allow: ["warn"] }], errors: [{ messageId: "unexpected", type: "MemberExpression", suggestions: [{ messageId: "removeConsole", data: { propertyName: "info" }, output: "if (a) {  }" }] }] },
        { code: "if (a) console.info(foo)", options: [{ allow: ["warn"] }], errors: [{ messageId: "unexpected", type: "MemberExpression" }] },
        { code: "foo(console.warn)", options: [{ allow: ["log"] }], errors: [{ messageId: "unexpected", type: "MemberExpression" }] },
        { code: "class A { static { console.error(foo) } }", options: [{ allow: ["log"] }], parserOptions: { ecmaVersion: "latest" }, errors: [{ messageId: "unexpected", type: "MemberExpression", suggestions: [{ messageId: "removeConsole", data: { propertyName: "error" }, output: "class A { static {  } }" }] }] },

        // multiple options
        { code: "console.log(foo)", options: [{ allow: ["warn", "info"] }], errors: [{ messageId: "unexpected", type: "MemberExpression", suggestions: [{ messageId: "removeConsole", data: { propertyName: "log" }, output: "" }] }] },
        { code: "console.error(foo)", options: [{ allow: ["warn", "info", "log"] }], errors: [{ messageId: "unexpected", type: "MemberExpression", suggestions: [{ messageId: "removeConsole", data: { propertyName: "error" }, output: "" }] }] },
        { code: "console.info(foo)", options: [{ allow: ["warn", "error", "log"] }], errors: [{ messageId: "unexpected", type: "MemberExpression", suggestions: [{ messageId: "removeConsole", data: { propertyName: "info" }, output: "" }] }] },
        { code: "console.warn(foo)", options: [{ allow: ["info", "log"] }], errors: [{ messageId: "unexpected", type: "MemberExpression", suggestions: [{ messageId: "removeConsole", data: { propertyName: "warn" }, output: "" }] }] },

Review comment (suggestion):
Can you break these out into multiple lines to make them easier to review? I know the original had these on single lines, but adding in suggestions makes these even harder to read on a single line.

Code after:

const ruleTester = new RuleTester();

ruleTester.run("no-console", rule, {
    valid: [
        "Console.info(foo)",

        // single array item
        { code: "console.info(foo)", options: [{ allow: ["info"] }] },
        { code: "console.warn(foo)", options: [{ allow: ["warn"] }] },
        { code: "console.error(foo)", options: [{ allow: ["error"] }] },
        { code: "console.log(foo)", options: [{ allow: ["log"] }] },

        // multiple array items
        { code: "console.info(foo)", options: [{ allow: ["warn", "info"] }] },
        { code: "console.warn(foo)", options: [{ allow: ["error", "warn"] }] },
        { code: "console.error(foo)", options: [{ allow: ["log", "error"] }] },
        { code: "console.log(foo)", options: [{ allow: ["info", "log", "warn"] }] },

        // https://github.com/eslint/eslint/issues/7010
        "var console = require('myconsole'); console.log(foo)"
    ],
    invalid: [

        // no options
        {
            code: "if (a) console.warn(foo)",
            errors: [{
                messageId: "unexpected",
                type: "MemberExpression",
                suggestions: null
            }]
        },
        {
            code: "foo(console.log)",
            errors: [{
                messageId: "unexpected",
                type: "MemberExpression",
                suggestions: null
            }]
        },
        {
            code: "console.log(foo)",
            errors: [{
                messageId: "unexpected",
                type: "MemberExpression",
                suggestions: [{
                    messageId: "removeConsole",
                    data: { propertyName: "log" },
                    output: ""
                }]