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

Code before:
                        [hookName]: 42
                    }],
                    invalid: []
                }), `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);
        });

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

Review comment (suggestion):
```suggestion
            const hookBefore = sinon.stub().throws(new Error("Something happened in before()"));
```

Code after:
                    }]
                }), "Something happened");
            });

            it(`${hookName} should throw when not a function is assigned`, () => {
                ruleTester = new RuleTester();
                assert.throws(() => ruleTester.run(ruleName, rule, {
                    valid: [{
                        code: "const onlyValid = 42;",
                        [hookName]: 42
                    }],
                    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);