Skip to content

Commit cb5947e

Browse files
committed
feat(rule/valid-mock-module-path): add test to cover code branch that throws unexpected OS error
Signed-off-by: hainenber <[email protected]>
1 parent 36d37a3 commit cb5947e

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

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

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
import { TSESLint } from '@typescript-eslint/utils';
12
import dedent from 'dedent';
23
import rule from '../valid-mock-module-path';
3-
import { FlatCompatRuleTester as RuleTester, espreeParser } from './test-utils';
4+
import {
5+
FlatCompatRuleTester as RuleTester,
6+
espreeParser,
7+
usingFlatConfig,
8+
} from './test-utils';
49

510
const ruleTester = new RuleTester({
611
parser: espreeParser,
@@ -123,3 +128,64 @@ ruleTester.run('valid-mock-module-path', rule, {
123128
},
124129
],
125130
});
131+
132+
const mockUnexpectedError = () => {
133+
jest.resetModules();
134+
135+
jest.doMock('path', () => ({
136+
...jest.requireActual('path'),
137+
resolve() {
138+
throw new (class extends Error {
139+
public code;
140+
141+
constructor(message?: string) {
142+
super(message);
143+
this.code = 'VERY_UNEXPECTED_OS_ERROR';
144+
}
145+
})();
146+
},
147+
}));
148+
149+
// eslint-disable-next-line @typescript-eslint/no-require-imports
150+
return require('../valid-mock-module-path').default;
151+
};
152+
153+
describe('valid-mock-module-path', () => {
154+
it('throws if encountered unexpected OS errors', () => {
155+
expect(() => {
156+
const linter = new TSESLint.Linter();
157+
158+
/* istanbul ignore if */
159+
if (usingFlatConfig) {
160+
linter.verify(
161+
'jest.mock("./fixtures/module")',
162+
[
163+
{
164+
files: [__filename],
165+
plugins: {
166+
jest: {
167+
rules: { 'valid-mock-module-path': mockUnexpectedError() },
168+
},
169+
},
170+
},
171+
],
172+
__filename,
173+
);
174+
175+
return;
176+
}
177+
178+
linter.defineRule('valid-mock-module-path', mockUnexpectedError());
179+
180+
linter.verify(
181+
'jest.mock("./fixtures/module")',
182+
{
183+
rules: { 'valid-mock-module-path': 'error' },
184+
},
185+
__filename,
186+
);
187+
}).toThrow(
188+
'Error when trying to validate mock module path from `jest.mock`',
189+
);
190+
});
191+
});

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ export default createRule<
104104

105105
// Reports unexpected issues when attempt to verify mocked module path.
106106
// The list of possible errors is non-exhaustive.
107-
/* istanbul ignore if */
108107
if (castedErr.code !== 'MODULE_NOT_FOUND') {
109108
throw new Error(
110109
`Error when trying to validate mock module path from \`jest.mock\`: ${err}`,

0 commit comments

Comments
 (0)