Skip to content

Commit 4ee0483

Browse files
authored
Feat/eslint tests setup (#15)
* feat: add tests config * chore: add peer deps for tests config * feat: add examples breaking tests rules * docs: update README
1 parent 49c1333 commit 4ee0483

10 files changed

Lines changed: 944 additions & 37 deletions

File tree

packages/eslint-plugin/README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This project is an ESLint plugin that gathers all the rules, plugins and parsers that should be used in any new BAM project.
44

5-
## How to use?
5+
## How to use the recommended config?
66

77
In your app, run
88

@@ -19,6 +19,23 @@ In your `.eslintrc` config file, extend the exported recommended configuration:
1919
}
2020
```
2121

22+
## How to use the config for tests ?
23+
24+
```bash
25+
yarn add --dev eslint-plugin-jest eslint-plugin-jest-formatting eslint-plugin-testing-library
26+
```
27+
28+
In your `.eslintrc` config file, extend the exported recommended configuration (don't forget to add the overrides to check only the test files)
29+
30+
```json
31+
"overrides": [
32+
{
33+
"files": ["*.test.tsx"],
34+
"extends": "plugin:@bam.tech/tests"
35+
}
36+
]
37+
```
38+
2239
## How to customize?
2340

2441
You can still customize your ESLint config by adding configurations, plugins and rules to your `.eslintrc` config file.
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
{
2-
"extends": "plugin:@bam.tech/recommended"
2+
"extends": "plugin:@bam.tech/recommended",
3+
"overrides": [
4+
{
5+
"files": ["*.test.tsx"],
6+
"extends": "plugin:@bam.tech/tests"
7+
}
8+
]
39
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// This should trigger an error breaking eslint-jest-formatting rule:
2+
// jest-formatting/padding-around-all
3+
4+
it("a test", () => {
5+
const abc = 123;
6+
expect(abc).toEqual(123);
7+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// This should trigger an error breaking eslint-jest rules:
2+
// jest/valid-title
3+
// jest/no-identical-title
4+
5+
it("should not fail", () => {
6+
expect(1).toBe(1);
7+
});
8+
9+
it("should not fail", () => {
10+
expect(1).toBe(1);
11+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// This should trigger an error breaking eslint-testing-library rule:
2+
// testing-library/no-await-sync-events
3+
4+
it("a test", () => {
5+
await fireEvent();
6+
});

packages/eslint-plugin/example-app/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111
"@types/react": "^18.2.14",
1212
"@typescript-eslint/eslint-plugin": "^5.48.0",
1313
"eslint": "^8.31.0",
14+
"eslint-plugin-jest": "^27.2.2",
15+
"eslint-plugin-jest-formatting": "^3.1.0",
1416
"eslint-plugin-prettier": "^4.2.1",
1517
"eslint-plugin-react": "^7.31.11",
1618
"eslint-plugin-react-hooks": "^4.6.0",
1719
"eslint-plugin-react-native": "^4.0.0",
20+
"eslint-plugin-testing-library": "^5.11.0",
21+
"jest": "^29.5.0",
1822
"prettier": "^2.8.8"
1923
},
2024
"private": "true",

packages/eslint-plugin/lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
"use strict";
33

44
const recommended = require("./recommended");
5+
const tests = require("./tests");
56

67
module.exports = {
78
configs: {
89
recommended,
10+
tests,
911
},
1012
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const { defineConfig } = require("eslint-define-config");
2+
3+
module.exports = defineConfig({
4+
env: {
5+
"jest/globals": true,
6+
},
7+
plugins: ["jest", "jest-formatting", "testing-library"],
8+
rules: {
9+
// Setting the recommended rules manually, because we don't want warnings, only errors
10+
"jest/no-alias-methods": "error", // STYLE
11+
"jest/no-commented-out-tests": "error",
12+
"jest/no-conditional-expect": "error",
13+
"jest/no-conditional-in-test": "error",
14+
"jest/no-deprecated-functions": "error",
15+
"jest/no-disabled-tests": "error",
16+
"jest/no-done-callback": "error",
17+
"jest/no-export": "error",
18+
"jest/no-focused-tests": "error",
19+
"jest/no-identical-title": "error",
20+
"jest/no-mocks-import": "error",
21+
"jest/no-standalone-expect": "error",
22+
"jest/no-test-prefixes": "error", // STYLE - force it.skip over xtest
23+
"jest/no-test-return-statement": "error",
24+
"jest/prefer-to-have-length": "error", // STYLE
25+
"jest/valid-describe-callback": "error",
26+
"jest/valid-expect": "error",
27+
"jest/valid-expect-in-promise": "error",
28+
"jest/valid-title": ["error", { disallowedWords: ["should"] }], // STYLE
29+
"jest-formatting/padding-around-all": "error", // STYLE
30+
"testing-library/await-async-query": "error",
31+
"testing-library/no-manual-cleanup": "error",
32+
"testing-library/no-container": "error",
33+
"testing-library/no-await-sync-query": "error",
34+
"testing-library/no-await-sync-events": "error",
35+
"testing-library/no-debugging-utils": "error",
36+
"testing-library/await-async-utils": "error",
37+
"testing-library/no-promise-in-fire-event": "error",
38+
"testing-library/no-render-in-setup": "error",
39+
"testing-library/no-unnecessary-act": "error",
40+
"testing-library/no-wait-for-multiple-assertions": "error",
41+
"testing-library/prefer-explicit-assert": "error",
42+
"testing-library/prefer-presence-queries": "error",
43+
"testing-library/no-wait-for-side-effects": "error",
44+
"testing-library/prefer-screen-queries": "error",
45+
},
46+
});

packages/eslint-plugin/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
"peerDependencies": {
1717
"@typescript-eslint/eslint-plugin": ">= 5",
1818
"eslint": ">= 8",
19+
"eslint-plugin-jest": "^27.2.2",
20+
"eslint-plugin-jest-formatting": "^3.1.0",
1921
"eslint-plugin-prettier": ">=4.2.1",
2022
"eslint-plugin-react": ">=7.31.11",
2123
"eslint-plugin-react-hooks": ">=4.6.0",
2224
"eslint-plugin-react-native": ">=4.0.0",
25+
"eslint-plugin-testing-library": "^5.11.0",
2326
"prettier": ">=2.8.8",
2427
"react": ">=17"
2528
},

0 commit comments

Comments
 (0)