Skip to content

Commit dd431b9

Browse files
authored
Prevent committing tests with .only. (#1883)
Adds an eslint rule for mocha tests and suites marked with .only, including a version for our tests/suites wrapped with tag functionality. Adds eslint in to the precommit hook in addition to running the formatter.
1 parent 7d196b2 commit dd431b9

File tree

4 files changed

+105
-1
lines changed

4 files changed

+105
-1
lines changed

.eslintrc.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,22 @@
66
"sourceType": "module",
77
"project": true
88
},
9-
"plugins": ["@typescript-eslint"],
9+
"plugins": ["@typescript-eslint", "mocha"],
1010
"rules": {
1111
"curly": "error",
1212
"eqeqeq": "warn",
1313
"no-throw-literal": "warn",
1414
// TODO "@typescript-eslint/semi" rule moved to https://eslint.style
1515
"semi": "error",
1616
"no-console": "warn",
17+
"mocha/no-exclusive-tests": "error",
18+
"no-restricted-syntax": [
19+
"error",
20+
{
21+
"selector": "CallExpression[callee.object.object.callee.name='tag'][callee.property.name='only']",
22+
"message": "Unexpected exclusive mocha test with tag().suite.only() or tag().test.only()"
23+
}
24+
],
1725
"@typescript-eslint/no-floating-promises": ["warn", { "checkThenables": true }],
1826
"@typescript-eslint/await-thenable": "warn",
1927
// Mostly fails tests, ex. expect(...).to.be.true returns a Chai.Assertion

package-lock.json

Lines changed: 88 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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,6 +2009,10 @@
20092009
"prepare": "husky"
20102010
},
20112011
"lint-staged": {
2012+
"**/*.ts": [
2013+
"eslint --max-warnings=0",
2014+
"prettier --write"
2015+
],
20122016
"**/*": "prettier --write --ignore-unknown"
20132017
},
20142018
"devDependencies": {
@@ -2051,6 +2055,7 @@
20512055
"esbuild": "^0.25.10",
20522056
"eslint": "^8.57.0",
20532057
"eslint-config-prettier": "^10.1.8",
2058+
"eslint-plugin-mocha": "^10.5.0",
20542059
"fantasticon": "^1.2.3",
20552060
"husky": "^9.1.7",
20562061
"lint-staged": "^16.2.3",

test/tags.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,13 @@ export function tag(size: TestSize): MochaFunctions {
144144
};
145145
wrappedSuite.only = (title: string, fn?: (this: Suite) => void): Suite => {
146146
if (fn) {
147+
// eslint-disable-next-line mocha/no-exclusive-tests
147148
return suite.only(title, function () {
148149
applyTags(this);
149150
fn.call(this);
150151
});
151152
}
153+
// eslint-disable-next-line mocha/no-exclusive-tests
152154
return suite.only(title);
153155
};
154156
wrappedSuite.skip = (title: string, fn: (this: Suite) => void): Suite | void => {
@@ -164,6 +166,7 @@ export function tag(size: TestSize): MochaFunctions {
164166
};
165167
wrappedTest.only = (titleOrFn: string | AsyncFunc | Func, fn?: AsyncFunc | Func): Test => {
166168
return applyTags(
169+
// eslint-disable-next-line mocha/no-exclusive-tests
167170
typeof titleOrFn === "string" ? test.only(titleOrFn, fn) : test.only(titleOrFn)
168171
);
169172
};

0 commit comments

Comments
 (0)