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

Code before:

            assert.strictEqual(messages.length, 1);
            assert.strictEqual(messages[0].ruleId, "semi");
            assert.strictEqual(messages[0].messageId, "missingSemi");
            assert.strictEqual(messages[0].line, 1);

            await sleep(100);
            await fsp.writeFile(path.join(cwd, "eslint.config.js"), configFileContent.replace("always", "never"));

            eslint = new FlatESLint({ cwd });
            [{ messages }] = await eslint.lintFiles(["a.js"]);

            assert.strictEqual(messages.length, 1);
            assert.strictEqual(messages[0].ruleId, "semi");
            assert.strictEqual(messages[0].messageId, "extraSemi");
            assert.strictEqual(messages[0].line, 2);
        });
    });

});

describe("shouldUseFlatConfig", () => {
    describe("when the env variable `ESLINT_USE_FLAT_CONFIG` is `'true'`", () => {
        beforeEach(() => {
            process.env.ESLINT_USE_FLAT_CONFIG = true;
        });

        afterEach(() => {
            delete process.env.ESLINT_USE_FLAT_CONFIG;
        });

        it("is `true`", async () => {
            assert.strictEqual(await shouldUseFlatConfig(true), true);
        });
    });

    describe("when the env variable `ESLINT_USE_FLAT_CONFIG` is `'false'`", () => {
        beforeEach(() => {
            process.env.ESLINT_USE_FLAT_CONFIG = false;
        });

        afterEach(() => {
            delete process.env.ESLINT_USE_FLAT_CONFIG;
        });

        it("is `false`", async () => {
            assert.strictEqual(await shouldUseFlatConfig(true), false);
        });
    });

    describe("when the env variable `ESLINT_USE_FLAT_CONFIG` is unset", () => {

Review comment (suggestion):
I think this test would be better with a setup where the is no config file present (e.g., with `process.chdir(os.tmpdir())`).

Code after:

            assert.strictEqual(messages.length, 1);
            assert.strictEqual(messages[0].ruleId, "semi");
            assert.strictEqual(messages[0].messageId, "missingSemi");
            assert.strictEqual(messages[0].line, 1);

            await sleep(100);
            await fsp.writeFile(path.join(cwd, "eslint.config.js"), configFileContent.replace("always", "never"));

            eslint = new FlatESLint({ cwd });
            [{ messages }] = await eslint.lintFiles(["a.js"]);

            assert.strictEqual(messages.length, 1);
            assert.strictEqual(messages[0].ruleId, "semi");
            assert.strictEqual(messages[0].messageId, "extraSemi");
            assert.strictEqual(messages[0].line, 2);
        });
    });

});

describe("shouldUseFlatConfig", () => {

    /**
     * Check that `shouldUseFlatConfig` returns the expected value from a CWD
     * with a flat config and one without a flat config.
     * @param {boolean} expectedValueWithConfig the expected return value of
     * `shouldUseFlatConfig` when in a directory with a flat config present
     * @param {boolean} expectedValueWithoutConfig the expected return value of
     * `shouldUseFlatConfig` when in a directory without any flat config present
     * @returns {void}
     */
    function testShouldUseFlatConfig(expectedValueWithConfig, expectedValueWithoutConfig) {
        describe("when there is a flat config file present", () => {
            const originalDir = process.cwd();

            beforeEach(() => {
                process.chdir(__dirname);
            });

            afterEach(() => {
                process.chdir(originalDir);
            });

            it(`is \`${expectedValueWithConfig}\``, async () => {
                assert.strictEqual(await shouldUseFlatConfig(), expectedValueWithConfig);
            });
        });

        describe("when there is no flat config file present", () => {
            const originalDir = process.cwd();