diff --git a/docs/rules/require-hook.md b/docs/rules/require-hook.md index ce0116525..350f5b5ef 100644 --- a/docs/rules/require-hook.md +++ b/docs/rules/require-hook.md @@ -20,6 +20,7 @@ directly within the body of a `describe`, _except_ for the following: - `import` statements - `const` variables - `let` _declarations_, and initializations to `null` or `undefined` +- Classes - Types - Calls to the standard Jest globals diff --git a/src/rules/__tests__/require-hook.test.ts b/src/rules/__tests__/require-hook.test.ts index 3e1515cd8..5c6f83ec3 100644 --- a/src/rules/__tests__/require-hook.test.ts +++ b/src/rules/__tests__/require-hook.test.ts @@ -32,6 +32,25 @@ ruleTester.run('require-hook', rule, { expect(myFn()).toBe(1); }); `, + { + code: dedent` + import { myFn } from '../functions'; + + test('myFn', () => { + expect(myFn()).toBe(1); + }); + `, + parserOptions: { sourceType: 'module' }, + }, + dedent` + class MockLogger { + log() {} + } + + test('myFn', () => { + expect(myFn()).toBe(1); + }); + `, dedent` const { myFn } = require('../functions'); @@ -357,3 +376,53 @@ ruleTester.run('require-hook', rule, { }, ], }); + +new TSESLint.RuleTester({ + parser: require.resolve('@typescript-eslint/parser'), +}).run('require-hook: typescript edition', rule, { + valid: [ + dedent` + import { myFn } from '../functions'; + + // todo: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/56545 + declare module 'eslint' { + namespace ESLint { + interface LintResult { + fatalErrorCount: number; + } + } + } + + test('myFn', () => { + expect(myFn()).toBe(1); + }); + `, + ], + invalid: [ + { + code: dedent` + import { setup } from '../test-utils'; + + // todo: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/56545 + declare module 'eslint' { + namespace ESLint { + interface LintResult { + fatalErrorCount: number; + } + } + } + + describe('some tests', () => { + setup(); + }); + `, + errors: [ + { + messageId: 'useHook', + line: 13, + column: 3, + }, + ], + }, + ], +});