Skip to content

Commit 8357800

Browse files
committed
fix(valid-mock-module-path): report on ERR_PACKAGE_PATH_NOT_EXPORTED errors
1 parent ba20c35 commit 8357800

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/rules/__tests__/valid-mock-module-path.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,34 @@ ruleTester.run('valid-mock-module-path', rule, {
126126
},
127127
],
128128
},
129+
// the imported file does not exist, but since it's not in `exports`
130+
// a ERR_PACKAGE_PATH_NOT_EXPORTED error will be thrown instead
131+
{
132+
filename: __filename,
133+
code: 'jest.mock("jest-util/build/isInteractive")',
134+
errors: [
135+
{
136+
messageId: 'invalidMockModulePath',
137+
data: { moduleName: '"jest-util/build/isInteractive"' },
138+
column: 1,
139+
line: 1,
140+
},
141+
],
142+
},
143+
// the imported file does exist, but since it's not in `exports`
144+
// a ERR_PACKAGE_PATH_NOT_EXPORTED error will be thrown instead
145+
{
146+
filename: __filename,
147+
code: 'jest.mock("jackspeak/dist/commonjs/parse-args.js")',
148+
errors: [
149+
{
150+
messageId: 'invalidMockModulePath',
151+
data: { moduleName: '"jackspeak/dist/commonjs/parse-args.js"' },
152+
column: 1,
153+
line: 1,
154+
},
155+
],
156+
},
129157
],
130158
});
131159

src/rules/valid-mock-module-path.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export default createRule<
2424
description: 'Disallow mocking of non-existing module paths',
2525
},
2626
messages: {
27-
invalidMockModulePath: 'Module path {{ moduleName }} does not exist',
27+
invalidMockModulePath:
28+
'Module path {{ moduleName }} does not exist or is not exported',
2829
},
2930
schema: [
3031
{
@@ -104,7 +105,10 @@ export default createRule<
104105

105106
// Reports unexpected issues when attempt to verify mocked module path.
106107
// The list of possible errors is non-exhaustive.
107-
if (castedErr.code !== 'MODULE_NOT_FOUND') {
108+
if (
109+
castedErr.code !== 'MODULE_NOT_FOUND' &&
110+
castedErr.code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED'
111+
) {
108112
throw new Error(
109113
`Error when trying to validate mock module path from \`jest.mock\`: ${err}`,
110114
);

0 commit comments

Comments
 (0)