Skip to content

Commit 232ea36

Browse files
committed
refactor(unbound-method): extend base rule
1 parent 137b525 commit 232ea36

File tree

2 files changed

+104
-368
lines changed

2 files changed

+104
-368
lines changed

src/rules/__tests__/unbound-method.test.ts

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'path';
22
import { ESLintUtils, TSESLint } from '@typescript-eslint/experimental-utils';
33
import dedent from 'dedent';
4-
import rule, { MessageIds, Options } from '../unbound-method';
4+
import type { MessageIds, Options } from '../unbound-method';
55

66
function getFixturesRootDir(): string {
77
return path.join(__dirname, 'fixtures');
@@ -109,20 +109,71 @@ const invalidTestCases: Array<TSESLint.InvalidTestCase<MessageIds, Options>> = [
109109
})),
110110
];
111111

112-
ruleTester.run('unbound-method jest edition', rule, {
113-
valid: validTestCases,
114-
invalid: invalidTestCases,
112+
const requireRule = (throwWhenRequiring: boolean) => {
113+
jest.resetModuleRegistry();
114+
115+
TSESLintPluginRef.throwWhenRequiring = throwWhenRequiring;
116+
117+
// eslint-disable-next-line @typescript-eslint/no-require-imports,node/no-missing-require
118+
return require('../unbound-method').default;
119+
};
120+
121+
const TSESLintPluginRef: { throwWhenRequiring: boolean } = {
122+
throwWhenRequiring: false,
123+
};
124+
125+
jest.mock('@typescript-eslint/eslint-plugin', () => {
126+
if (TSESLintPluginRef.throwWhenRequiring) {
127+
throw new Error('oh noes!');
128+
}
129+
130+
return jest.requireActual('@typescript-eslint/eslint-plugin');
115131
});
116132

117-
new ESLintUtils.RuleTester({
118-
parser: '@typescript-eslint/parser',
119-
parserOptions: {
120-
sourceType: 'module',
121-
tsconfigRootDir: rootPath,
122-
},
123-
}).run('unbound-method jest edition without type service', rule, {
124-
valid: validTestCases.concat(invalidTestCases.map(({ code }) => code)),
125-
invalid: [],
133+
describe('error handling', () => {
134+
describe('when @typescript-eslint/eslint-plugin is not available', () => {
135+
const ruleTester = new ESLintUtils.RuleTester({
136+
parser: '@typescript-eslint/parser',
137+
parserOptions: {
138+
sourceType: 'module',
139+
tsconfigRootDir: rootPath,
140+
project: './tsconfig.json',
141+
},
142+
});
143+
144+
ruleTester.run(
145+
'unbound-method jest edition without type service',
146+
requireRule(true),
147+
{
148+
valid: validTestCases.concat(invalidTestCases.map(({ code }) => code)),
149+
invalid: [],
150+
},
151+
);
152+
});
153+
154+
describe('when "project" is not set', () => {
155+
const ruleTester = new ESLintUtils.RuleTester({
156+
parser: '@typescript-eslint/parser',
157+
parserOptions: {
158+
sourceType: 'module',
159+
tsconfigRootDir: rootPath,
160+
},
161+
});
162+
163+
ruleTester.run(
164+
'unbound-method jest edition without "project" property',
165+
requireRule(false),
166+
{
167+
valid: validTestCases.concat(invalidTestCases.map(({ code }) => code)),
168+
invalid: [],
169+
},
170+
);
171+
});
172+
});
173+
174+
ruleTester.run('unbound-method jest edition', requireRule(false), {
175+
valid: validTestCases,
176+
invalid: invalidTestCases,
126177
});
127178

128179
function addContainsMethodsClass(code: string): string {
@@ -160,7 +211,7 @@ function addContainsMethodsClassInvalid(
160211
}));
161212
}
162213

163-
ruleTester.run('unbound-method', rule, {
214+
ruleTester.run('unbound-method', requireRule(false), {
164215
valid: [
165216
'Promise.resolve().then(console.log);',
166217
"['1', '2', '3'].map(Number.parseInt);",

0 commit comments

Comments
 (0)