Skip to content

Commit e92eb10

Browse files
committed
feat: recast err to unknown + refactor to return happy path asap
Signed-off-by: hainenber <[email protected]>
1 parent 09da117 commit e92eb10

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed
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+
{}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ ruleTester.run('valid-mock-module-path', rule, {
4747
code: 'jest.mock("./fixtures/module/tsx/foo")',
4848
options: [{ moduleFileExtensions: ['.jsx'] }],
4949
},
50+
{
51+
filename: __filename,
52+
code: 'jest.mock("./fixtures/module/bar")',
53+
options: [{ moduleFileExtensions: ['.json'] }],
54+
},
55+
{
56+
filename: __filename,
57+
code: 'jest.mock("./fixtures/module/bar")',
58+
options: [{ moduleFileExtensions: ['.css'] }],
59+
},
5060
],
5161
invalid: [
5262
{

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,27 @@ export default createRule<
9696
},
9797
);
9898

99-
if (!hasPossiblyModulePaths) {
100-
throw { code: 'MODULE_NOT_FOUND' };
99+
if (hasPossiblyModulePaths) {
100+
return;
101101
}
102-
} catch (err: any) {
102+
} catch (err: unknown) {
103+
const castedErr = err as NodeJS.ErrnoException;
104+
103105
// Reports unexpected issues when attempt to verify mocked module path.
104106
// The list of possible errors is non-exhaustive.
105107
/* istanbul ignore if */
106-
if (!['MODULE_NOT_FOUND', 'ENOENT'].includes(err.code)) {
108+
if (castedErr.code !== 'MODULE_NOT_FOUND') {
107109
throw new Error(
108110
`Error when trying to validate mock module path from \`jest.mock\`: ${err}`,
109111
);
110112
}
111-
112-
context.report({
113-
messageId: 'invalidMockModulePath',
114-
data: { moduleName: moduleName.raw },
115-
node,
116-
});
117113
}
114+
115+
context.report({
116+
messageId: 'invalidMockModulePath',
117+
data: { moduleName: moduleName.raw },
118+
node,
119+
});
118120
},
119121
};
120122
},

0 commit comments

Comments
 (0)