Skip to content

Commit

Permalink
Chore: add eslint-plugin-mocha (#184)
Browse files Browse the repository at this point in the history
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`)
  • Loading branch information
bmish authored May 6, 2021
1 parent ce4803f commit de7ca73
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 29 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@
"quotes": "off",
"space-before-function-paren": "off"
}
},
{
"files": ["tests/**/*.js"],
"extends": ["plugin:mocha/recommended"]
}
]
}
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
46 changes: 18 additions & 28 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//------------------------------------------------------------------------------

const assert = require("chai").assert,
index = require("../index"),
{ rules, configs } = require("../index"),
fs = require("fs"),
path = require("path");

Expand All @@ -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");
Expand All @@ -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"]);
Expand Down

0 comments on commit de7ca73

Please sign in to comment.