From 01808b472994ff81ceb7ce53b0e41d5c9e62238b Mon Sep 17 00:00:00 2001 From: Bryan Mishkin <698306+bmish@users.noreply.github.com> Date: Thu, 6 May 2021 09:30:11 -0700 Subject: [PATCH] chore(dev-deps): add eslint-plugin-mocha I refactored the index.js test structure a bit to simplify things and help address these two rules: * [mocha/no-nested-tests](https://github.com/lo1tuma/eslint-plugin-mocha/blob/master/docs/rules/no-nested-tests.md) (we had `describe` nested inside `it`) * [mocha/no-setup-in-describe](https://github.com/lo1tuma/eslint-plugin-mocha/blob/master/docs/rules/no-setup-in-describe.md) (doesn't like function calls or member expressions inside `describe`) --- .eslintrc.json | 4 ++++ .github/workflows/ci.yml | 2 +- package-lock.json | 39 ++++++++++++++++++++++++++++++++++ package.json | 1 + tests/index.js | 46 ++++++++++++++++------------------------ 5 files changed, 63 insertions(+), 29 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 6180fa4d..6dee8023 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -217,6 +217,10 @@ "quotes": "off", "space-before-function-paren": "off" } + }, + { + "files": ["tests/**/*.js"], + "extends": ["plugin:mocha/recommended"] } ] } diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3eae9d8f..078db806 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,7 +28,7 @@ jobs: - run: npm ci - run: npm test - run: npm run update && git diff --exit-code - - run: npm install --save-dev eslint@6 --save-dev eslint-plugin-unicorn@19 && npm run unit-test + - run: npm install --save-dev eslint@6 --save-dev eslint-plugin-unicorn@19 --save-dev eslint-plugin-mocha@6 && npm run unit-test - name: Coveralls uses: coverallsapp/github-action@master diff --git a/package-lock.json b/package-lock.json index 2342fd0b..3b2991df 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,6 +20,7 @@ "eslint-plugin-eslint-comments": "^3.2.0", "eslint-plugin-eslint-plugin": "^2.3.0", "eslint-plugin-markdown": "^2.0.1", + "eslint-plugin-mocha": "^8.1.0", "eslint-plugin-node": "^11.1.0", "eslint-plugin-unicorn": "^30.0.0", "markdownlint-cli": "^0.27.1", @@ -1755,6 +1756,22 @@ "eslint": ">=6.0.0" } }, + "node_modules/eslint-plugin-mocha": { + "version": "8.1.0", + "resolved": "https://registry.npmjs.org/eslint-plugin-mocha/-/eslint-plugin-mocha-8.1.0.tgz", + "integrity": "sha512-1EgHvXKRl7W3mq3sntZAi5T24agRMyiTPL4bSXe+B4GksYOjAPEWYx+J3eJg4It1l2NMNZJtk0gQyQ6mfiPhQg==", + "dev": true, + "dependencies": { + "eslint-utils": "^2.1.0", + "ramda": "^0.27.1" + }, + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "eslint": ">=7.0.0" + } + }, "node_modules/eslint-plugin-node": { "version": "11.1.0", "resolved": "https://registry.npmjs.org/eslint-plugin-node/-/eslint-plugin-node-11.1.0.tgz", @@ -4529,6 +4546,12 @@ } ] }, + "node_modules/ramda": { + "version": "0.27.1", + "resolved": "https://registry.npmjs.org/ramda/-/ramda-0.27.1.tgz", + "integrity": "sha512-PgIdVpn5y5Yns8vqb8FzBUEYn98V3xcPgawAkkgj0YJ0qDsnHCiNmZYfOGMgOvoB0eWFLpYbhxUR3mxfDIMvpw==", + "dev": true + }, "node_modules/randombytes": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz", @@ -7441,6 +7464,16 @@ "unified": "^6.1.2" } }, + "eslint-plugin-mocha": { + "version": "8.1.0", + "resolved": "https://registry.npmjs.org/eslint-plugin-mocha/-/eslint-plugin-mocha-8.1.0.tgz", + "integrity": "sha512-1EgHvXKRl7W3mq3sntZAi5T24agRMyiTPL4bSXe+B4GksYOjAPEWYx+J3eJg4It1l2NMNZJtk0gQyQ6mfiPhQg==", + "dev": true, + "requires": { + "eslint-utils": "^2.1.0", + "ramda": "^0.27.1" + } + }, "eslint-plugin-node": { "version": "11.1.0", "resolved": "https://registry.npmjs.org/eslint-plugin-node/-/eslint-plugin-node-11.1.0.tgz", @@ -9538,6 +9571,12 @@ "integrity": "sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==", "dev": true }, + "ramda": { + "version": "0.27.1", + "resolved": "https://registry.npmjs.org/ramda/-/ramda-0.27.1.tgz", + "integrity": "sha512-PgIdVpn5y5Yns8vqb8FzBUEYn98V3xcPgawAkkgj0YJ0qDsnHCiNmZYfOGMgOvoB0eWFLpYbhxUR3mxfDIMvpw==", + "dev": true + }, "randombytes": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz", diff --git a/package.json b/package.json index da25d7c5..4156b33f 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "eslint-plugin-eslint-comments": "^3.2.0", "eslint-plugin-eslint-plugin": "^2.3.0", "eslint-plugin-markdown": "^2.0.1", + "eslint-plugin-mocha": "^8.1.0", "eslint-plugin-node": "^11.1.0", "eslint-plugin-unicorn": "^30.0.0", "markdownlint-cli": "^0.27.1", diff --git a/tests/index.js b/tests/index.js index 0cc3c1ae..3ee0fef3 100644 --- a/tests/index.js +++ b/tests/index.js @@ -9,7 +9,7 @@ //------------------------------------------------------------------------------ const assert = require("chai").assert, - index = require("../index"), + { rules, configs } = require("../index"), fs = require("fs"), path = require("path"); @@ -31,63 +31,53 @@ function toSentenceCase(str) { ); } -describe("index.js", function () { - let ruleFileNames; +const ruleNames = fs.readdirSync("./lib/rules").map(rawFileName => path.basename(rawFileName, ".js")); - before(function (done) { - fs.readdir("./lib/rules", function (err, files) { - if (err) throw err; - ruleFileNames = files.map(function (rawFileName) { - return path.basename(rawFileName, ".js"); - }); - done(); - }); - }); - - it("rules", function () { - for (const fileName of ruleFileNames) { - describe(fileName, function () { +describe("index.js", function () { + describe("rules", function () { + for (const ruleName of ruleNames) { + describe(ruleName, function () { it("should appear in rule exports", function () { - assert.property(index.rules, fileName, `Rule export for ${fileName} not present`); + assert.property(rules, ruleName, `Rule export for ${ruleName} not present`); }); it("should appear in tests", function (done) { - const path = `./tests/lib/rules/${fileName}.js`; + const path = `./tests/lib/rules/${ruleName}.js`; fs.access(path, function (err) { - assert.notOk(err, `tests/lib/rules/${fileName}.js should exist`); + assert.notOk(err, `tests/lib/rules/${ruleName}.js should exist`); done(); }); }); it("should appear in docs", function (done) { - const path = `./docs/rules/${fileName}.md`; + const path = `./docs/rules/${ruleName}.md`; fs.access(path, function (err) { - assert.notOk(err, `docs/rules/${fileName}.md should exist`); + assert.notOk(err, `docs/rules/${ruleName}.md should exist`); done(); }); }); it("should have the right doc contents", function () { - const path = `./docs/rules/${fileName}.md`; + const path = `./docs/rules/${ruleName}.md`; const fileContents = fs.readFileSync(path, "utf8"); const lines = fileContents.split("\n"); // First content should be title. - const description = index.rules[fileName].meta.docs.description; - const expectedTitle = `# ${toSentenceCase(description)} (${fileName})`; + const description = rules[ruleName].meta.docs.description; + const expectedTitle = `# ${toSentenceCase(description)} (${ruleName})`; assert.equal(lines[0], expectedTitle, "includes the rule description and name in title"); // Decide which notices should be shown at the top of the doc. const expectedNotices = []; const unexpectedNotices = []; - if (index.configs.recommended.rules[`qunit/${fileName}`]) { + if (configs.recommended.rules[`qunit/${ruleName}`]) { expectedNotices.push("configRecommended"); } else { unexpectedNotices.push("configRecommended"); } - if (index.rules[fileName].meta.fixable) { + if (rules[ruleName].meta.fixable) { expectedNotices.push("fixable"); } else { unexpectedNotices.push("fixable"); @@ -114,8 +104,8 @@ describe("index.js", function () { }); describe("configs", function () { - for (const configName of Object.keys(index.configs)) { - const config = index.configs[configName]; + // eslint-disable-next-line mocha/no-setup-in-describe -- rule doesn't like function calls like `Object.entries()` + for (const [configName, config] of Object.entries(configs)) { describe(configName, function () { it("has the right plugins", function () { assert.deepStrictEqual(config.plugins, ["qunit"]);