Skip to content
This repository was archived by the owner on Mar 7, 2019. It is now read-only.

Commit 87fd555

Browse files
feat(rule): add eslint-plugin-mocha ☕️
1 parent 9a3165f commit 87fd555

File tree

9 files changed

+156
-41
lines changed

9 files changed

+156
-41
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
[flow-home]: https://flow.org
77
[react-home]: http://reactjs.com
88
[nodejs-home]: https://nodejs.org
9+
[mocha-home]: https://mochajs.org
910

1011

1112
# JavaScript Coding Standards
@@ -62,6 +63,10 @@ See the [tutorial](tutorial) directory for lots of example config files.
6263

6364
- @strv/javascript/environments/flow/recommended
6465

66+
#### For [Mocha][mocha-home]
67+
68+
- @strv/javascript/environments/mocha/recommended
69+
6570
#### Coding styles
6671

6772
These rulesets include rules which deal with how the code looks like and not how it works. They help keep the code clean and consistent.

environments/mocha/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Mocha
2+
3+
These configuration files are suitable to lint Mocha test files.
4+
5+
## Configurations
6+
7+
### @strv/javascript/environments/mocha/recommended
8+
9+
Use this ruleset to configure ESLint to lint your Mocha test files. Mocha test files are by default identified by `*.test.*` or `*.spec.*` filenames or by being in the _test/_ directory in your project root.
10+
11+
> ⚠️ You can use this environment ruleset in combination with any of the existing environment rulesets. Just make sure this one comes in as the last one.

environments/mocha/recommended.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Js-coding-standards
3+
*
4+
* @author Robert Rossmann <[email protected]>
5+
* @copyright 2018 STRV
6+
* @license http://choosealicense.com/licenses/bsd-3-clause BSD-3-Clause License
7+
*/
8+
9+
'use strict'
10+
11+
const globs = require('../../globs')
12+
13+
module.exports = {
14+
15+
overrides: [{
16+
files: globs.test,
17+
18+
plugins: [
19+
'mocha',
20+
],
21+
22+
env: {
23+
mocha: true,
24+
},
25+
26+
rules: {
27+
// Enforces handling of callbacks for async tests
28+
// Mocha allows you to write asynchronous tests by adding a done callback to the parameters of
29+
// your test function. It is easy to forget calling this callback after the asynchronous
30+
// operation is done.
31+
// @TODO (semver-major): Raise to `error`
32+
'mocha/handle-done-callback': 'warn',
33+
34+
// Limit the number of top-level suites in a single file
35+
// This rule enforces having a limited amount of top-level suites in a file.
36+
'mocha/max-top-level-suites': ['warn', { limit: 1 }],
37+
38+
// Disallow exclusive tests
39+
// This rule reminds you to remove .only from your tests by raising a warning whenever you are
40+
// using the exclusivity feature.
41+
'mocha/no-exclusive-tests': 'warn',
42+
43+
// Disallow global tests
44+
// This rule checks each mocha test function to not be located directly in the global scope.
45+
'mocha/no-global-tests': 'warn',
46+
47+
// Disallow identical titles
48+
// This rule looks at the title of every test and test suites. It will report when two test
49+
// suites or two test cases at the same level of a test suite have the same title.
50+
'mocha/no-identical-title': 'warn',
51+
52+
// Disallow tests to be nested within other tests
53+
// This rule looks for all test cases (it, specify and test) or suites (describe, context and
54+
// suite) which are nested within another test case.
55+
// @TODO (semver-major): Raise to `error`
56+
'mocha/no-nested-tests': 'warn',
57+
58+
// Disallow returning in a test or hook function that uses a callback
59+
// Mocha's tests or hooks (like before) may be asynchronous by either returning a Promise or
60+
// specifying a callback parameter for the function. It can be confusing to have both methods
61+
// used in a test or hook, and from Mocha v3 on, causes the test to fail in order to force
62+
// developers to remove this source of confusion.
63+
// @TODO (semver-major): Raise to `error`
64+
'mocha/no-return-and-callback': 'warn',
65+
66+
// Disallow setup in describe blocks
67+
// Any setup directly in a describe is run before all tests execute. This rule reports all
68+
// function calls and use of the dot operator (due to getters and setters) directly in
69+
// describe blocks.
70+
'mocha/no-setup-in-describe': 'warn',
71+
},
72+
}],
73+
}

environments/shared/recommended.js

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -223,43 +223,6 @@ module.exports = {
223223
// class, it will match to either ❇ (U+2747) or VARIATION SELECTOR-16 (U+FE0F) rather than ❇️.
224224
'no-misleading-character-class': 'error',
225225

226-
// Disallow certain object properties
227-
// Currently configured for the following (more can be added as necessary):
228-
// - Mocha test isolation features (skipping tests, running only certain tests)
229-
'no-restricted-properties': ['warn', {
230-
object: 'describe',
231-
property: 'only',
232-
message: 'Isolated test suite',
233-
}, {
234-
object: 'describe',
235-
property: 'skip',
236-
message: 'Skipped test suite. Use xdescribe() to write a pending test suite.',
237-
}, {
238-
object: 'it',
239-
property: 'only',
240-
message: 'Isolated test case',
241-
}, {
242-
object: 'it',
243-
property: 'skip',
244-
message: 'Skipped test case. Use xit() to write a pending test case.',
245-
}, {
246-
object: 'context',
247-
property: 'only',
248-
message: 'Isolated test suite',
249-
}, {
250-
object: 'context',
251-
property: 'skip',
252-
message: 'Skipped test suite. Use xcontext() to write a pending test suite.',
253-
}, {
254-
object: 'specify',
255-
property: 'only',
256-
message: 'Isolated test case',
257-
}, {
258-
object: 'specify',
259-
property: 'skip',
260-
message: 'Skipped test case. Use xspecify() to write a pending test case.',
261-
}],
262-
263226
// Disallow unnecessary `return await`
264227
// Inside an async function, return await is useless. Since the return value of an async
265228
// function is always wrapped in Promise.resolve, return await doesn’t actually do anything

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"eslint-plugin-flowtype": "^2.39.1",
1919
"eslint-plugin-import": "^2.14.0",
2020
"eslint-plugin-jsx-a11y": "^6.1.1",
21+
"eslint-plugin-mocha": "^5.2.0",
2122
"eslint-plugin-react": "^7.4.0"
2223
},
2324
"devDependencies": {

tutorial/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ When configuring js-coding-standards, please keep the following in mind:
88
- Always extend only from **one environment configuration** per _.eslintrc.js_
99
> Some environment configuration files in this ruleset have conflicting rule configurations - you **will** experience weird issues if you include ie. both nodejs and react environments into a single _.eslintrc.js_ file.
1010
>
11-
> If your project consists of both React and Node.js code, put them into separate folders and create _.eslintrc.js_ file for each of those folders.
11+
> If your project consists of both React and Node.js code, you should utilise the `overrides` functionality in ESLint to only enable one environment ruleset for each type of code.
1212
1313
- Do not include extensions when extending
1414
> You should not include the file extension in your `extends:` properties - the format in which this ruleset is written should be an implementation detail for ESLint to figure out.

tutorial/nodejs-eslintrc.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module.exports = {
66
extends: [
77
'@strv/javascript/environments/nodejs/v10',
88
'@strv/javascript/environments/nodejs/optional',
9+
'@strv/javascript/environments/mocha/recommended',
910
'@strv/javascript/coding-styles/recommended',
1011
],
1112

@@ -18,9 +19,6 @@ module.exports = {
1819
files: [
1920
'test/**',
2021
],
21-
env: {
22-
mocha: true,
23-
},
2422

2523
rules: {
2624
'func-names': 'off',

unused.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ module.exports = {
7979
// them.
8080
'no-plusplus': 0,
8181

82+
// Disallow certain object properties
83+
// Unused, no use case.
84+
'no-restricted-properties': 0,
85+
8286
// Disallow use of the void operator
8387
// void is quite useful with IIFEs and for discarding return values when using return for
8488
// short-circuiting.
@@ -179,5 +183,52 @@ module.exports = {
179183
// Unused, too restrictive, although I do agree about imorting stuff from parent modules'
180184
// subdirectories is generally a bad idea... ⚠️
181185
'import/no-relative-parent-imports': 0,
186+
187+
// Disallow hooks
188+
// Unused, hooks are quite useful if they are used responsibly.
189+
'mocha/no-hooks': 0,
190+
191+
// Disallow hooks for a single test or test suite
192+
// Unused, usually you start with the setup hooks and write the first test, and once everything
193+
// is working you continue writing new tests.
194+
'mocha/no-hooks-for-single-case': 0,
195+
196+
// Disallow arrow functions as arguments to mocha functions
197+
// Unused, arrow functions are preferred unless you need to access `this` inside of them.
198+
'mocha/no-mocha-arrows': 0,
199+
200+
// Disallow pending tests
201+
// Unused, Mocha already displays pending tests quite nicely, no need to bother the developer
202+
// with extra warnings from the linter.
203+
'mocha/no-pending-tests': 0,
204+
205+
// Disallow duplicate uses of a hook at the same level inside a describe
206+
// Unused, too restrictive. Sometimes it is useful to have the preparaction code separated, to
207+
// "stand out" to make it more obvious.
208+
'mocha/no-sibling-hooks': 0,
209+
210+
// Disallow skipped tests
211+
// Unused, too restrictive. Developers should be able to author and commit pending tests.
212+
'mocha/no-skipped-tests': 0,
213+
214+
// Disallow synchronous tests
215+
// Unused, too restrictive.
216+
'mocha/no-synchronous-tests': 0,
217+
218+
// Disallow top-level hooks
219+
// Unused, too restrictive.
220+
'mocha/no-top-level-hooks': 0,
221+
222+
// Require using arrow functions for callbacks
223+
// Unused, the ESLint's original implementation works just fine for Mocha tests as well.
224+
'mocha/prefer-arrow-callback': 0,
225+
226+
// Match suite descriptions against a pre-configured regular expression
227+
// Unused, too restrictive.
228+
'mocha/valid-suite-description': 0,
229+
230+
// Match test descriptions against a pre-configured regular expression
231+
// Unused, too restrictive.
232+
'mocha/valid-test-description': 0,
182233
},
183234
}

0 commit comments

Comments
 (0)