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

Code before:
            ruleTester = new RuleTester();
            assert.throws(() => ruleTester.run(ruleName, rule, {
                valid: [{
                    code: "var onlyValid = 42;",
                    before: hookBefore,
                    after: hookAfter
                }],
                invalid: []
            }));
            sinon.assert.calledOnce(hookBefore);
            sinon.assert.calledOnce(hookAfter);
        });

        it("should call after() hook even when before() throws", () => {
            const hookBefore = sinon.stub().throws(new Error("Something happended in before()"));
            const hookAfter = sinon.stub();

            ruleTester = new RuleTester();
            assert.throws(() => ruleTester.run(ruleName, rule, {
                valid: [{
                    code: "const onlyValid = 42;",
                    before: hookBefore,
                    after: hookAfter
                }],
                invalid: []
            }), "Something happended in before()");
            sinon.assert.calledOnce(hookBefore);
            sinon.assert.calledOnce(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" }] }

Review comment (suggestion):
```suggestion
            }), "Something happened in before()");
```

Code after:
                    invalid: []
                }), `Optional test case property '${hookName}' must be a function`);
                assert.throws(() => ruleTester.run(ruleName, rule, {
                    valid: [],
                    invalid: [{
                        code: "var onlyValid = 42;",
                        errors: [/^Bad var/u],
                        output: " onlyValid = 42;",
                        [hookName]: 42
                    }]
                }), `Optional test case property '${hookName}' must be a function`);
            });
        });

        it("should call both before() and after() hooks even when the case failed", () => {
            const hookBefore = sinon.stub();
            const hookAfter = sinon.stub();

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

        it("should call both before() and after() hooks regardless syntax errors", () => {
            const hookBefore = sinon.stub();
            const hookAfter = sinon.stub();

            ruleTester = new RuleTester();
            assert.throws(() => ruleTester.run(ruleName, rule, {
                valid: [{
                    code: "invalid javascript code",