|
| 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. |
0 commit comments