-
Notifications
You must be signed in to change notification settings - Fork 247
feat: create new valid-mock-module-path rule
#1845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
1d32972
9ce0a40
621b8da
c7d1901
fee7e7a
bf6127f
172594c
4fb2681
80063e6
a6782da
4fab92e
09da117
e92eb10
d60997c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # Disallow mocking of non-existing module path (`valid-mocked-module-path`) | ||
|
|
||
| <!-- end auto-generated rule header --> | ||
|
|
||
| This rule raises an error when using `jest.mock` and `jest.doMock` and the first | ||
| argument for mocked object (module/local file) do not exist. | ||
|
|
||
| ## Rule details | ||
|
|
||
| This rule checks existence of the supplied path for `jest.mock` or `jest.doMock` | ||
| in the first argument. | ||
|
|
||
| The following patterns are considered errors: | ||
|
|
||
| ```js | ||
| // Module(s) that cannot be found | ||
| jest.mock('@org/some-module-not-in-package-json'); | ||
| jest.mock('some-module-not-in-package-json'); | ||
|
|
||
| // Local module (directory) that cannot be found | ||
| jest.mock('../../this/module/does/not/exist'); | ||
|
|
||
| // Local file that cannot be found | ||
| jest.mock('../../this/path/does/not/exist.js'); | ||
| ``` | ||
|
|
||
| The following patterns are **not** considered errors: | ||
|
|
||
| ```js | ||
| // Module(s) that can be found | ||
| jest.mock('@org/some-module-in-package-json'); | ||
| jest.mock('some-module-in-package-json'); | ||
|
|
||
| // Local module that cannot be found | ||
| jest.mock('../../this/module/really/does/exist'); | ||
|
|
||
| // Local file that cannot be found | ||
| jest.mock('../../this/path/really/does/exist.js'); | ||
| ``` | ||
|
|
||
| ## When Not To Use It | ||
|
|
||
| Don't use this rule on non-jest test files. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const foo = 'foo_js'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const foo = 'foo_ts'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './foo'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import dedent from 'dedent'; | ||
| import rule from '../valid-mocked-module-path'; | ||
| import { FlatCompatRuleTester as RuleTester, espreeParser } from './test-utils'; | ||
|
|
||
| const ruleTester = new RuleTester({ | ||
| parser: espreeParser, | ||
| parserOptions: { | ||
| ecmaVersion: 2015, | ||
| }, | ||
| }); | ||
|
|
||
| ruleTester.run('valid-mocked-module-path', rule, { | ||
| valid: [ | ||
| { filename: __filename, code: 'jest.mock("./fixtures/module")' }, | ||
| { filename: __filename, code: 'jest.mock("./fixtures/module", () => {})' }, | ||
| { filename: __filename, code: 'jest.mock()' }, | ||
| { | ||
| filename: __filename, | ||
| code: 'jest.doMock("./fixtures/module", () => {})', | ||
| }, | ||
| { | ||
| filename: __filename, | ||
| code: dedent` | ||
| describe("foo", () => {}); | ||
| `, | ||
| }, | ||
| { filename: __filename, code: 'jest.doMock("./fixtures/module")' }, | ||
| { filename: __filename, code: 'jest.mock("./fixtures/module/foo.ts")' }, | ||
| { filename: __filename, code: 'jest.doMock("./fixtures/module/foo.ts")' }, | ||
| { filename: __filename, code: 'jest.mock("./fixtures/module/foo.js")' }, | ||
| { filename: __filename, code: 'jest.doMock("./fixtures/module/foo.js")' }, | ||
| 'jest.mock("eslint")', | ||
| 'jest.doMock("eslint")', | ||
| 'jest.mock("child_process")', | ||
| ], | ||
| invalid: [ | ||
| { | ||
| filename: __filename, | ||
| code: "jest.mock('../module/does/not/exist')", | ||
| errors: [ | ||
| { | ||
| messageId: 'invalidMockModulePath', | ||
| data: { moduleName: "'../module/does/not/exist'" }, | ||
| column: 1, | ||
| line: 1, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| filename: __filename, | ||
| code: 'jest.mock("../file/does/not/exist.ts")', | ||
| errors: [ | ||
| { | ||
| messageId: 'invalidMockModulePath', | ||
| data: { moduleName: '"../file/does/not/exist.ts"' }, | ||
| column: 1, | ||
| line: 1, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| filename: __filename, | ||
| code: 'jest.mock("@doesnotexist/module")', | ||
| errors: [ | ||
| { | ||
| messageId: 'invalidMockModulePath', | ||
| data: { moduleName: '"@doesnotexist/module"' }, | ||
| column: 1, | ||
| line: 1, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import { statSync } from 'fs'; | ||
| import path from 'path'; | ||
| import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils'; | ||
| import { | ||
| createRule, | ||
| findModuleName, | ||
| getAccessorValue, | ||
| isSupportedAccessor, | ||
| isTypeOfJestFnCall, | ||
| } from './utils'; | ||
|
|
||
| export default createRule({ | ||
| name: __filename, | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: 'Disallow mocking of non-existing module path', | ||
| }, | ||
| messages: { | ||
| invalidMockModulePath: 'Mocked module path {{moduleName}} does not exist', | ||
| }, | ||
| schema: [], | ||
| }, | ||
| defaultOptions: [], | ||
| create(context) { | ||
| return { | ||
| CallExpression(node: TSESTree.CallExpression): void { | ||
| const { callee } = node; | ||
|
||
|
|
||
| if (callee.type !== AST_NODE_TYPES.MemberExpression) { | ||
| return; | ||
| } | ||
|
|
||
| const { property } = callee; | ||
|
|
||
| if ( | ||
| !node.arguments.length || | ||
| !isTypeOfJestFnCall(node, context, ['jest']) || | ||
| !( | ||
| isSupportedAccessor(property) && | ||
| ['mock', 'doMock'].includes(getAccessorValue(property)) | ||
| ) | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| const [nameNode] = node.arguments; | ||
| const moduleName = findModuleName(nameNode); | ||
hainenber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /* istanbul ignore if */ | ||
| if (!moduleName) { | ||
| throw new Error( | ||
| 'Cannot parse mocked module name from `jest.mock` - - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`', | ||
| ); | ||
| } | ||
|
||
|
|
||
| try { | ||
| if (moduleName.value.startsWith('.')) { | ||
| const resolvedModulePath = path.resolve( | ||
| path.dirname(context.filename), | ||
| moduleName.value, | ||
| ); | ||
|
|
||
| const hasPossiblyModulePaths = ['', '.js', '.ts'] | ||
|
||
| .map(ext => `${resolvedModulePath}${ext}`) | ||
| .some(modPath => { | ||
| try { | ||
| statSync(modPath); | ||
|
|
||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| }); | ||
|
|
||
| if (!hasPossiblyModulePaths) { | ||
| throw { code: 'MODULE_NOT_FOUND' }; | ||
| } | ||
| } else { | ||
| require.resolve(moduleName.value); | ||
|
||
| } | ||
| } catch (err: any) { | ||
| // Skip over any unexpected issues when attempt to verify mocked module path. | ||
| // The list of possible errors is non-exhaustive. | ||
| /* istanbul ignore if */ | ||
| if (!['MODULE_NOT_FOUND', 'ENOENT'].includes(err.code)) { | ||
|
||
| return; | ||
| } | ||
|
|
||
| context.report({ | ||
| messageId: 'invalidMockModulePath', | ||
| data: { moduleName: moduleName.raw }, | ||
| node, | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.