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

Code before:
        const ruleName = "no-var";
        const rule = require("../../fixtures/testers/rule-tester/no-var");

        it("should be called when a function is assigned", () => {
            const setup = sinon.stub();

            ruleTester = new RuleTester();
            ruleTester.run(ruleName, rule, {
                valid: [{
                    code: "const onlyValid = 42;",
                    setup
                }],
                invalid: []
            });
            sinon.assert.calledOnce(setup);
        });

        it("should throw when not a function is assigned", () => {
            ruleTester = new RuleTester();
            assert.throws(() => ruleTester.run(ruleName, rule, {
                valid: [{
                    code: "const onlyValid = 42;",
                    setup: 42
                }],
                invalid: []
            }), "Optional test case property 'setup' must be a function");
        });
    });

    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" }] }
                ]
            });

Review comment (suggestion):
```suggestion
            }), "Optional test case property 'before' must be a function");
```

Code after:
        const ruleName = "no-var";
        const rule = require("../../fixtures/testers/rule-tester/no-var");

        ["before", "after"].forEach(hookName => {
            it(`${hookName} should be called when a function is assigned`, () => {
                const hookForValid = sinon.stub();
                const hookForInvalid = sinon.stub();

                ruleTester = new RuleTester();
                ruleTester.run(ruleName, rule, {
                    valid: [{
                        code: "const onlyValid = 42;",
                        [hookName]: hookForValid
                    }],
                    invalid: [{
                        code: "var onlyValid = 42;",
                        errors: [/^Bad var/u],
                        output: " onlyValid = 42;",
                        [hookName]: hookForInvalid
                    }]
                });
                sinon.assert.calledOnce(hookForValid);
                sinon.assert.calledOnce(hookForInvalid);
            });

            it(`${hookName} should cause test to fail when it throws error`, () => {
                const hook = sinon.stub().throws(new Error("Something happened"));

                ruleTester = new RuleTester();
                assert.throws(() => ruleTester.run(ruleName, rule, {
                    valid: [{
                        code: "const onlyValid = 42;",
                        [hookName]: hook
                    }],
                    invalid: []
                }), "Something happened");
                assert.throws(() => ruleTester.run(ruleName, rule, {
                    valid: [],
                    invalid: [{
                        code: "var onlyValid = 42;",
                        errors: [/^Bad var/u],
                        output: " onlyValid = 42;",
                        [hookName]: hook
                    }]
                }), "Something happened");
            });

            it(`${hookName} should throw when not a function is assigned`, () => {
                ruleTester = new RuleTester();
                assert.throws(() => ruleTester.run(ruleName, rule, {
                    valid: [{