From 328f3179f062c0ea340e6e8297fd6cc10df0aa74 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Fri, 8 Nov 2024 15:42:17 +0100 Subject: [PATCH] test: Ensure all rules are exported --- tests/package/exports.js | 36 ++++++++++++++++++++++++++++++++++++ tests/plugin/eslint.test.js | 19 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/tests/package/exports.js b/tests/package/exports.js index 69a8878..ba7484c 100644 --- a/tests/package/exports.js +++ b/tests/package/exports.js @@ -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 @@ -24,6 +35,31 @@ 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)); + const exportedRules = exports.default.rules; + + assert.deepStrictEqual( + Object.keys(exportedRules), + 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); }); diff --git a/tests/plugin/eslint.test.js b/tests/plugin/eslint.test.js index 3ad4bd7..d9f5cba 100644 --- a/tests/plugin/eslint.test.js +++ b/tests/plugin/eslint.test.js @@ -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`, () => {