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

Code before:
                    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 happended"));

                ruleTester = new RuleTester();
                assert.throws(() => ruleTester.run(ruleName, rule, {
                    valid: [{
                        code: "const onlyValid = 42;",
                        [hookName]: hook
                    }],
                    invalid: []
                }), "Something happended");
            });

            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`);
            });
        });

        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
                }],

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

Code after:
                    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: [{
                        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],