Repository: eslint/eslint
File: tests/lib/rule-tester/rule-tester.js (JavaScript)

Code before:
                ],
                invalid: [
                    { code: "eval(foo)", errors: [{ message: "eval sucks.", type: "CallExpression" }] }
                ]
            });
        }, /Should have no errors but had 1/u);
    });

    it("should throw an error if invalid code is valid", () => {

        assert.throws(() => {
            ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
                valid: [
                    "Eval(foo)"
                ],
                invalid: [
                    { code: "Eval(foo)", errors: [{ message: "eval sucks.", type: "CallExpression" }] }
                ]
            });
        }, /Should have 1 error but had 0/u);
    });

    it("should throw an error when the error message is wrong", () => {
        assert.throws(() => {
            ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {

                // Only the invalid test matters here
                valid: [
                    "bar = baz;"
                ],
                invalid: [
                    { code: "var foo = bar;", errors: [{ message: "Bad error message." }] }
                ]
            });
        }, assertErrorMatches("Bad var.", "Bad error message."));
    });

    it("should throw an error when the error message regex does not match", () => {
        assert.throws(() => {
            ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
                valid: [],
                invalid: [
                    { code: "var foo = bar;", errors: [{ message: /Bad error message/u }] }
                ]
            });
        }, /Expected 'Bad var.' to match \/Bad error message\//u);
    });

    it("should throw an error when the error is not a supported type", () => {
        assert.throws(() => {
            ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {

Review comment (suggestion):
To cover ourselves, can you please add some tests that:

1. Validate `after` is called in invalid cases
2. Validate `after` is called when there's a syntax error in `code`.

Code after:
            ruleTester = new RuleTester();
            assert.throws(() => ruleTester.run(ruleName, rule, {
                valid: [{
                    code: "const onlyValid = 42;",
                    before: hookBefore,
                    after: hookAfter
                }],
                invalid: []
            }), "Something happened in before()");
            sinon.assert.calledOnce(hookBefore);
            sinon.assert.calledOnce(hookAfter);
            assert.throws(() => ruleTester.run(ruleName, rule, {
                valid: [],
                invalid: [{
                    code: "var onlyValid = 42;",
                    errors: [/^Bad var/u],
                    output: " onlyValid = 42;",
                    before: hookBefore,
                    after: hookAfter
                }]
            }), "Something happened in before()");
            sinon.assert.calledTwice(hookBefore);
            sinon.assert.calledTwice(hookAfter);
        });
    });

    it("should not throw an error when everything passes", () => {
        ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
            valid: [
                "Eval(foo)"
            ],
            invalid: [
                { code: "eval(foo)", errors: [{ message: "eval sucks.", type: "CallExpression" }] }
            ]
        });
    });

    it("should throw correct error when valid code is invalid and enables other core rule", () => {

        assert.throws(() => {
            ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
                valid: [
                    "/*eslint semi: 2*/ eval(foo);"
                ],
                invalid: [
                    { code: "eval(foo)", errors: [{ message: "eval sucks.", type: "CallExpression" }] }
                ]
            });
        }, /Should have no errors but had 1/u);
    });