Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rules): add
require-top-level-describe
rule (#407)
Closes #401
- Loading branch information
Showing
5 changed files
with
208 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Require top-level describe block (require-top-level-describe) | ||
|
||
Jest allows you to organise your test files the way you want it. However, the | ||
more your codebase grows, the more it becomes hard to navigate in your test | ||
files. This rule makes sure that you provide at least a top-level describe block | ||
in your test file. | ||
|
||
## Rule Details | ||
|
||
This rule triggers a warning if a test case (`test` and `it`) or a hook | ||
(`beforeAll`, `beforeEach`, `afterEach`, `afterAll`) is not located in a | ||
top-level describe block. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
// Above a describe block | ||
test('my test', () => {}); | ||
describe('test suite', () => { | ||
it('test', () => {}); | ||
}); | ||
|
||
// Below a describe block | ||
describe('test suite', () => {}); | ||
test('my test', () => {}); | ||
|
||
// Same for hooks | ||
beforeAll('my beforeAll', () => {}); | ||
describe('test suite', () => {}); | ||
afterEach('my afterEach', () => {}); | ||
``` | ||
|
||
The following patterns are **not** considered warnings: | ||
|
||
```js | ||
// In a describe block | ||
describe('test suite', () => { | ||
test('my test', () => {}); | ||
}); | ||
|
||
// In a nested describe block | ||
describe('test suite', () => { | ||
test('my test', () => {}); | ||
describe('another test suite', () => { | ||
test('my other test', () => {}); | ||
}); | ||
}); | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
Don't use this rule on non-jest test files. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { TSESLint } from '@typescript-eslint/experimental-utils'; | ||
import rule from '../require-top-level-describe'; | ||
|
||
const ruleTester = new TSESLint.RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2015, | ||
}, | ||
}); | ||
|
||
ruleTester.run('no-standalone-hook', rule, { | ||
valid: [ | ||
'describe("test suite", () => { test("my test") });', | ||
'describe("test suite", () => { it("my test") });', | ||
` | ||
describe("test suite", () => { | ||
beforeEach("a", () => {}); | ||
describe("b", () => {}); | ||
test("c", () => {}) | ||
}); | ||
`, | ||
'describe("test suite", () => { beforeAll("my beforeAll") });', | ||
'describe("test suite", () => { afterEach("my afterEach") });', | ||
'describe("test suite", () => { afterAll("my afterAll") });', | ||
` | ||
describe("test suite", () => { | ||
it("my test", () => {}) | ||
describe("another test suite", () => { | ||
}); | ||
test("my other test", () => {}) | ||
}); | ||
`, | ||
'foo()', | ||
], | ||
invalid: [ | ||
{ | ||
code: 'beforeEach("my test", () => {})', | ||
errors: [{ messageId: 'unexpectedHook' }], | ||
}, | ||
{ | ||
code: ` | ||
test("my test", () => {}) | ||
describe("test suite", () => {}); | ||
`, | ||
errors: [{ messageId: 'unexpectedTestCase' }], | ||
}, | ||
{ | ||
code: ` | ||
test("my test", () => {}) | ||
describe("test suite", () => { | ||
it("test", () => {}) | ||
}); | ||
`, | ||
errors: [{ messageId: 'unexpectedTestCase' }], | ||
}, | ||
{ | ||
code: ` | ||
describe("test suite", () => {}); | ||
afterAll("my test", () => {}) | ||
`, | ||
errors: [{ messageId: 'unexpectedHook' }], | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { TSESTree } from '@typescript-eslint/experimental-utils'; | ||
|
||
import { createRule, isDescribe, isHook, isTestCase } from './utils'; | ||
|
||
export default createRule({ | ||
name: __filename, | ||
meta: { | ||
docs: { | ||
category: 'Best Practices', | ||
description: | ||
'Prevents test cases and hooks to be outside of a describe block', | ||
recommended: false, | ||
}, | ||
messages: { | ||
unexpectedTestCase: 'All test cases must be wrapped in a describe block.', | ||
unexpectedHook: 'All hooks must be wrapped in a describe block.', | ||
}, | ||
type: 'suggestion', | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
let numberOfDescribeBlocks = 0; | ||
return { | ||
CallExpression(node) { | ||
if (isDescribe(node)) { | ||
numberOfDescribeBlocks++; | ||
return; | ||
} | ||
|
||
if (numberOfDescribeBlocks === 0) { | ||
if (isTestCase(node)) { | ||
context.report({ node, messageId: 'unexpectedTestCase' }); | ||
return; | ||
} | ||
|
||
if (isHook(node)) { | ||
context.report({ node, messageId: 'unexpectedHook' }); | ||
return; | ||
} | ||
} | ||
}, | ||
'CallExpression:exit'(node: TSESTree.CallExpression) { | ||
if (isDescribe(node)) { | ||
numberOfDescribeBlocks--; | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |