From 83578005749b3817dc1186d91f3fc04b80d5971f Mon Sep 17 00:00:00 2001 From: Gareth Jones <3151613+G-Rath@users.noreply.github.com> Date: Mon, 24 Nov 2025 07:22:14 +1300 Subject: [PATCH] fix(valid-mock-module-path): report on `ERR_PACKAGE_PATH_NOT_EXPORTED` errors --- .../__tests__/valid-mock-module-path.test.ts | 28 +++++++++++++++++++ src/rules/valid-mock-module-path.ts | 8 ++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/rules/__tests__/valid-mock-module-path.test.ts b/src/rules/__tests__/valid-mock-module-path.test.ts index 5442a4278..8151a9961 100644 --- a/src/rules/__tests__/valid-mock-module-path.test.ts +++ b/src/rules/__tests__/valid-mock-module-path.test.ts @@ -126,6 +126,34 @@ ruleTester.run('valid-mock-module-path', rule, { }, ], }, + // the imported file does not exist, but since it's not in `exports` + // a ERR_PACKAGE_PATH_NOT_EXPORTED error will be thrown instead + { + filename: __filename, + code: 'jest.mock("jest-util/build/isInteractive")', + errors: [ + { + messageId: 'invalidMockModulePath', + data: { moduleName: '"jest-util/build/isInteractive"' }, + column: 1, + line: 1, + }, + ], + }, + // the imported file does exist, but since it's not in `exports` + // a ERR_PACKAGE_PATH_NOT_EXPORTED error will be thrown instead + { + filename: __filename, + code: 'jest.mock("jackspeak/dist/commonjs/parse-args.js")', + errors: [ + { + messageId: 'invalidMockModulePath', + data: { moduleName: '"jackspeak/dist/commonjs/parse-args.js"' }, + column: 1, + line: 1, + }, + ], + }, ], }); diff --git a/src/rules/valid-mock-module-path.ts b/src/rules/valid-mock-module-path.ts index 6478bd8f3..3954f4db2 100644 --- a/src/rules/valid-mock-module-path.ts +++ b/src/rules/valid-mock-module-path.ts @@ -24,7 +24,8 @@ export default createRule< description: 'Disallow mocking of non-existing module paths', }, messages: { - invalidMockModulePath: 'Module path {{ moduleName }} does not exist', + invalidMockModulePath: + 'Module path {{ moduleName }} does not exist or is not exported', }, schema: [ { @@ -104,7 +105,10 @@ export default createRule< // Reports unexpected issues when attempt to verify mocked module path. // The list of possible errors is non-exhaustive. - if (castedErr.code !== 'MODULE_NOT_FOUND') { + if ( + castedErr.code !== 'MODULE_NOT_FOUND' && + castedErr.code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED' + ) { throw new Error( `Error when trying to validate mock module path from \`jest.mock\`: ${err}`, );