Skip to content

Commit a8625f1

Browse files
hainenberG-Rath
andauthored
feat: create new valid-mock-module-path rule (#1845)
* feat(rule): add new rule to validate `jest.mock` path existence Signed-off-by: hainenber <[email protected]> * chore: update rules.test.ts due to newly added rule Signed-off-by: hainenber <[email protected]> * chore: rewrite implementation to return early when linting over unwanted LoCs Signed-off-by: hainenber <[email protected]> * fix(ci): verify broken npmjs link in MD file by checking its `registry.npmjs.org` URI instead Signed-off-by: hainenber <[email protected]> * build(dev-deps): update `markdown-link-check` Signed-off-by: hainenber <[email protected]> * feat: apply suggestions from @G-Rath code review Co-authored-by: Gareth Jones <[email protected]> * Revert "fix(ci): verify broken npmjs link in MD file by checking its `registry.npmjs.org` URI instead" This reverts commit c7d1901. * Revert "build(dev-deps): update `markdown-link-check`" This reverts commit fee7e7a. * feat: change rule name + add tests + configurable module file extensions + faster return branches Signed-off-by: hainenber <[email protected]> * Apply suggestions from @G-Rath's code review Co-authored-by: Gareth Jones <[email protected]> * Apply 2nd suggestion from @G-Rath Co-authored-by: Gareth Jones <[email protected]> * feat: recast err to `unknown` + refactor to return happy path asap Signed-off-by: hainenber <[email protected]> * chore: rerun `yarn regenerate-docs` Signed-off-by: hainenber <[email protected]> * Update src/rules/valid-mock-module-path.ts Co-authored-by: Gareth Jones <[email protected]> * feat(rule/valid-mock-module-path): add test to cover code branch that throws unexpected OS error Signed-off-by: hainenber <[email protected]> * chore: fix correct config to test `valid-mock-module-path` on eslint v9 Signed-off-by: hainenber <[email protected]> --------- Signed-off-by: hainenber <[email protected]> Co-authored-by: Gareth Jones <[email protected]>
1 parent 59927d9 commit a8625f1

File tree

15 files changed

+406
-11
lines changed

15 files changed

+406
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ Manually fixable by
386386
| [valid-describe-callback](docs/rules/valid-describe-callback.md) | Enforce valid `describe()` callback || | | |
387387
| [valid-expect](docs/rules/valid-expect.md) | Enforce valid `expect()` usage || | 🔧 | |
388388
| [valid-expect-in-promise](docs/rules/valid-expect-in-promise.md) | Require promises that have expectations in their chain to be valid || | | |
389+
| [valid-mock-module-path](docs/rules/valid-mock-module-path.md) | Disallow mocking of non-existing module paths | | | | |
389390
| [valid-title](docs/rules/valid-title.md) | Enforce valid titles || | 🔧 | |
390391

391392
### Requires Type Checking
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Disallow mocking of non-existing module paths (`valid-mock-module-path`)
2+
3+
<!-- end auto-generated rule header -->
4+
5+
This rule raises an error when using `jest.mock` and `jest.doMock` and the first
6+
argument for mocked object (module/local file) do not exist.
7+
8+
## Rule details
9+
10+
This rule checks existence of the supplied path for `jest.mock` or `jest.doMock`
11+
in the first argument.
12+
13+
The following patterns are considered errors:
14+
15+
```js
16+
// Module(s) that cannot be found
17+
jest.mock('@org/some-module-not-in-package-json');
18+
jest.mock('some-module-not-in-package-json');
19+
20+
// Local module (directory) that cannot be found
21+
jest.mock('../../this/module/does/not/exist');
22+
23+
// Local file that cannot be found
24+
jest.mock('../../this/path/does/not/exist.js');
25+
```
26+
27+
The following patterns are **not** considered errors:
28+
29+
```js
30+
// Module(s) that can be found
31+
jest.mock('@org/some-module-in-package-json');
32+
jest.mock('some-module-in-package-json');
33+
34+
// Local module that cannot be found
35+
jest.mock('../../this/module/really/does/exist');
36+
37+
// Local file that cannot be found
38+
jest.mock('../../this/path/really/does/exist.js');
39+
```
40+
41+
## Options
42+
43+
```json
44+
{
45+
"jest/valid-mock-module-path": [
46+
"error",
47+
{
48+
"moduleFileExtensions": [".js", ".ts", ".jsx", ".tsx", ".json"]
49+
}
50+
]
51+
}
52+
```
53+
54+
### `moduleFileExtensions`
55+
56+
This array option controls which file extensions the plugin checks for
57+
existence. The default extensions are:
58+
59+
- `".js"`
60+
- `".ts"`
61+
- `".jsx"`
62+
- `".tsx"`
63+
- `".json"`
64+
65+
For any custom extension, a preceding dot **must** be present before the file
66+
extension for desired effect.
67+
68+
## When Not To Use It
69+
70+
Don't use this rule on non-jest test files.

src/__tests__/__snapshots__/rules.test.ts.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ exports[`rules should export configs that refer to actual rules 1`] = `
7272
"jest/valid-describe-callback": "error",
7373
"jest/valid-expect": "error",
7474
"jest/valid-expect-in-promise": "error",
75+
"jest/valid-mock-module-path": "error",
7576
"jest/valid-title": "error",
7677
},
7778
},
@@ -164,6 +165,7 @@ exports[`rules should export configs that refer to actual rules 1`] = `
164165
"jest/valid-describe-callback": "error",
165166
"jest/valid-expect": "error",
166167
"jest/valid-expect-in-promise": "error",
168+
"jest/valid-mock-module-path": "error",
167169
"jest/valid-title": "error",
168170
},
169171
},

src/__tests__/rules.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { existsSync } from 'fs';
22
import { resolve } from 'path';
33
import plugin from '../';
44

5-
const numberOfRules = 63;
5+
const numberOfRules = 64;
66
const ruleNames = Object.keys(plugin.rules);
77
const deprecatedRules = Object.entries(plugin.rules)
88
.filter(([, rule]) => rule.meta.deprecated)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.bar-container {
2+
background-color: #f5f5f5;
3+
padding: 16px;
4+
border-radius: 8px;
5+
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const foo = 'foo_js';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const foo = 'foo_ts';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './foo';

src/rules/__tests__/fixtures/module/jsx/foo.jsx

Whitespace-only changes.

0 commit comments

Comments
 (0)