|
1 | 1 | import path from 'path'; |
2 | 2 | import { ESLintUtils, TSESLint } from '@typescript-eslint/experimental-utils'; |
3 | 3 | import dedent from 'dedent'; |
4 | | -import rule, { MessageIds, Options } from '../unbound-method'; |
| 4 | +import type { MessageIds, Options } from '../unbound-method'; |
5 | 5 |
|
6 | 6 | function getFixturesRootDir(): string { |
7 | 7 | return path.join(__dirname, 'fixtures'); |
@@ -109,20 +109,71 @@ const invalidTestCases: Array<TSESLint.InvalidTestCase<MessageIds, Options>> = [ |
109 | 109 | })), |
110 | 110 | ]; |
111 | 111 |
|
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'); |
115 | 131 | }); |
116 | 132 |
|
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, |
126 | 177 | }); |
127 | 178 |
|
128 | 179 | function addContainsMethodsClass(code: string): string { |
@@ -160,7 +211,7 @@ function addContainsMethodsClassInvalid( |
160 | 211 | })); |
161 | 212 | } |
162 | 213 |
|
163 | | -ruleTester.run('unbound-method', rule, { |
| 214 | +ruleTester.run('unbound-method', requireRule(false), { |
164 | 215 | valid: [ |
165 | 216 | 'Promise.resolve().then(console.log);', |
166 | 217 | "['1', '2', '3'].map(Number.parseInt);", |
|
0 commit comments