Skip to content

Commit

Permalink
test: Ensure all rules are exported (#53)
Browse files Browse the repository at this point in the history
* test: Ensure all rules are exported

* sort rules
  • Loading branch information
mdjermanovic authored Nov 12, 2024
1 parent 72273f5 commit e29e94c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
37 changes: 37 additions & 0 deletions tests/package/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@

import * as exports from "../../src/index.js";
import assert from "node:assert";
import path from "node:path";
import fs from "node:fs/promises";
import { fileURLToPath, pathToFileURL } from "node:url";

//-----------------------------------------------------------------------------
// Helpers
//-----------------------------------------------------------------------------

const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);
const rulesDir = path.resolve(dirname, "../../src/rules");

//------------------------------------------------------------------------------
// Tests
Expand All @@ -24,6 +35,32 @@ describe("Package exports", () => {
]);
});

it("has all available rules exported in the ESLint plugin", async () => {
const allRules = (await fs.readdir(rulesDir))
.filter(name => name.endsWith(".js"))
.map(name => name.slice(0, -".js".length))
.sort();
const exportedRules = exports.default.rules;

assert.deepStrictEqual(
Object.keys(exportedRules).sort(),
allRules,
"Expected all rules to be exported in the ESLint plugin (`plugin.rules` in `src/index.js`)",
);

for (const [ruleName, rule] of Object.entries(exportedRules)) {
assert.strictEqual(
rule,
(
await import(
pathToFileURL(path.resolve(rulesDir, `${ruleName}.js`))
)
).default,
`Expected ${ruleName}.js to be exported under key "${ruleName}" in the ESLint plugin (\`plugin.rules\` in \`src/index.js\`)`,
);
}
});

it("has a JSONLanguage export", () => {
assert.ok(exports.JSONLanguage);
});
Expand Down
19 changes: 19 additions & 0 deletions tests/plugin/eslint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@ import dedent from "dedent";
//-----------------------------------------------------------------------------

describe("when the plugin is used with ESLint", () => {
describe("plugin configs", () => {
Object.keys(json.configs).forEach(configName => {
it(`Using "${configName}" config should not throw`, async () => {
const config = {
files: ["**/*.json"],
language: "json/json",
...json.configs[configName],
};

const eslint = new ESLint({
overrideConfigFile: true,
overrideConfig: config,
});

await eslint.lintText("{}", { filePath: "test.json" });
});
});
});

describe("config comments", () => {
["jsonc", "json5"].forEach(language => {
describe(`with ${language} language`, () => {
Expand Down

0 comments on commit e29e94c

Please sign in to comment.