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

Code before:
        describe("static helper wrapper", () => {
            it("adds `only` to string test cases", () => {
                const test = RuleTester.only("const valid = 42;");

                assert.deepStrictEqual(test, {
                    code: "const valid = 42;",
                    only: true
                });
            });

            it("adds `only` to object test cases", () => {
                const test = RuleTester.only({ code: "const valid = 42;" });

                assert.deepStrictEqual(test, {
                    code: "const valid = 42;",
                    only: true
                });
            });
        });
    });

    describe("before", () => {
        const ruleName = "no-var";
        const rule = require("../../fixtures/testers/rule-tester/no-var");

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

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

        it("should cause test to fail when it throws error", () => {
            const before = sinon.stub().throws(new Error("Something happended"));

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

Review comment (suggestion):
three tests are similar for both hooks, so that I'd like to use something similar to `.each()` approach, but looks like Mocha does not have it, so will attempt to use `for..of` or `Array::forEach()`

Code after:
        describe("static helper wrapper", () => {
            it("adds `only` to string test cases", () => {
                const test = RuleTester.only("const valid = 42;");

                assert.deepStrictEqual(test, {
                    code: "const valid = 42;",
                    only: true
                });
            });

            it("adds `only` to object test cases", () => {
                const test = RuleTester.only({ code: "const valid = 42;" });

                assert.deepStrictEqual(test, {
                    code: "const valid = 42;",
                    only: true
                });
            });
        });
    });

    describe("hooks", () => {
        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();