Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add FlatESLint tests with missing config files #17164

Merged
merged 2 commits into from May 29, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 62 additions & 0 deletions tests/lib/eslint/flat-eslint.js
Expand Up @@ -648,6 +648,37 @@ describe("FlatESLint", () => {
);
});

it("should throw if eslint.config.js file is not present", async () => {
eslint = new FlatESLint({
cwd: getFixturePath("..")
});
await assert.rejects(() => eslint.lintText("var foo = 'bar';"), /Could not find config file/u);
});

it("should not throw if eslint.config.js file is not present and overrideConfigFile is `true`", async () => {
eslint = new FlatESLint({
cwd: getFixturePath(".."),
overrideConfigFile: true
});
await eslint.lintText("var foo = 'bar';");
});

it("should not throw if eslint.config.js file is not present and overrideConfigFile is path to a config file", async () => {
eslint = new FlatESLint({
cwd: getFixturePath(".."),
overrideConfigFile: "fixtures/configurations/quotes-error.js"
});
await eslint.lintText("var foo = 'bar';");
});

it("should throw if overrideConfigFile is path to a file that doesn't exist", async () => {
eslint = new FlatESLint({
cwd: getFixturePath(""),
overrideConfigFile: "does-not-exist.js"
});
await assert.rejects(() => eslint.lintText("var foo = 'bar';"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add the message check here too?

Suggested change
await assert.rejects(() => eslint.lintText("var foo = 'bar';"));
await assert.rejects(() => eslint.lintText("var foo = 'bar';"), /Could not find config file/u);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a system error thrown by fs.stat():

Error: ENOENT: no such file or directory, stat 'path/to/temp/eslint/fixtures/does-not-exist.js'.

I wanted to avoid checking error messages that we don't generate.

Perhaps /ENOENT/u?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe {code: "ENOENT"}? Looks more accurate to me. We can even add a checker function that asserts the code and uses a regex on the message property. (docs: https://nodejs.org/api/assert.html#assertrejectsasyncfn-error-message)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think anything that checks the expected error message is fine. I just want to make sure we are getting the error we're expecting. I'm agnostic as to the approach.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added { code: "ENOENT" }, thanks for the suggestion!

});

it("should throw if non-string value is given to 'code' parameter", async () => {
eslint = new FlatESLint();
await assert.rejects(() => eslint.lintText(100), /'code' must be a string/u);
Expand Down Expand Up @@ -763,6 +794,37 @@ describe("FlatESLint", () => {
assert.strictEqual(results[0].suppressedMessages.length, 0);
});

it("should throw if eslint.config.js file is not present", async () => {
eslint = new FlatESLint({
cwd: getFixturePath("..")
});
await assert.rejects(() => eslint.lintFiles("fixtures/undef*.js"), /Could not find config file/u);
});

it("should not throw if eslint.config.js file is not present and overrideConfigFile is `true`", async () => {
eslint = new FlatESLint({
cwd: getFixturePath(".."),
overrideConfigFile: true
});
await eslint.lintFiles("fixtures/undef*.js");
});

it("should not throw if eslint.config.js file is not present and overrideConfigFile is path to a config file", async () => {
eslint = new FlatESLint({
cwd: getFixturePath(".."),
overrideConfigFile: "fixtures/configurations/quotes-error.js"
});
await eslint.lintFiles("fixtures/undef*.js");
});

it("should throw if overrideConfigFile is path to a file that doesn't exist", async () => {
eslint = new FlatESLint({
cwd: getFixturePath(),
overrideConfigFile: "does-not-exist.js"
});
await assert.rejects(() => eslint.lintFiles("undef*.js"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, do we want to check the message?

Suggested change
await assert.rejects(() => eslint.lintFiles("undef*.js"));
await assert.rejects(() => eslint.lintFiles("undef*.js"), /Could not find config file/u);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I concur. We should check the error code and message when viable as any error can mask actual errors code (working or test).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added { code: "ENOENT" }

});

it("should throw an error when given a config file and a valid file and invalid parser", async () => {
eslint = new FlatESLint({
overrideConfig: {
Expand Down