Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add no-duplicate-hooks rule #227

Merged
merged 1 commit into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ command line option.\
| | | | [no-commented-out-test](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-commented-out-test.md) | Disallow commented out tests |
| ✔ | | | [no-conditional-expect](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-conditional-expect.md) | Disallow calling `expect` conditionally |
| ✔ | | | [no-conditional-in-test](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-conditional-in-test.md) | Disallow conditional logic in tests |
| | | | [no-duplicate-hooks](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-duplicate-hooks.md) | Disallow duplicate setup and teardown hooks |
| ✔ | | 💡 | [no-element-handle](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-element-handle.md) | Disallow usage of element handles |
| ✔ | | | [no-eval](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-eval.md) | Disallow usage of `page.$eval()` and `page.$$eval()` |
| ✔ | | 💡 | [no-focused-test](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-focused-test.md) | Disallow usage of `.only` annotation |
Expand Down
75 changes: 75 additions & 0 deletions docs/rules/no-duplicate-hooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Disallow duplicate setup and teardown hooks (`no-duplicate-hooks`)

A `describe` block should not contain duplicate hooks.

## Rule details

Examples of **incorrect** code for this rule

```js
/* eslint playwright/no-duplicate-hooks: "error" */

test.describe('foo', () => {
test.beforeEach(() => {
// some setup
});
test.beforeEach(() => {
// some setup
});
test('foo_test', () => {
// some test
});
});

// Nested describe scenario
test.describe('foo', () => {
test.beforeEach(() => {
// some setup
});
test('foo_test', () => {
// some test
});
test.describe('bar', () => {
test('bar_test', () => {
test.afterAll(() => {
// some teardown
});
test.afterAll(() => {
// some teardown
});
});
});
});
```

Examples of **correct** code for this rule

```js
/* eslint playwright/no-duplicate-hooks: "error" */

test.describe('foo', () => {
test.beforeEach(() => {
// some setup
});
test('foo_test', () => {
// some test
});
});

// Nested describe scenario
test.describe('foo', () => {
test.beforeEach(() => {
// some setup
});
test('foo_test', () => {
// some test
});
test.describe('bar', () => {
test('bar_test', () => {
test.beforeEach(() => {
// some setup
});
});
});
});
```
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import missingPlaywrightAwait from './rules/missing-playwright-await';
import noCommentedOutTests from './rules/no-commented-out-tests';
import noConditionalExpect from './rules/no-conditional-expect';
import noConditionalInTest from './rules/no-conditional-in-test';
import noDuplicateHooks from './rules/no-duplicate-hooks';
import noElementHandle from './rules/no-element-handle';
import noEval from './rules/no-eval';
import noFocusedTest from './rules/no-focused-test';
Expand Down Expand Up @@ -46,6 +47,7 @@ const index = {
'no-commented-out-tests': noCommentedOutTests,
'no-conditional-expect': noConditionalExpect,
'no-conditional-in-test': noConditionalInTest,
'no-duplicate-hooks': noDuplicateHooks,
'no-element-handle': noElementHandle,
'no-eval': noEval,
'no-focused-test': noFocusedTest,
Expand Down
54 changes: 54 additions & 0 deletions src/rules/no-duplicate-hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Rule } from 'eslint';
import { getStringValue, isDescribeCall, isTestHook } from '../utils/ast';

export default {
create(context) {
const hookContexts: Array<Record<string, number>> = [{}];

return {
CallExpression(node) {
if (isDescribeCall(node)) {
hookContexts.push({});
}

if (!isTestHook(context, node)) {
return;
}

const currentLayer = hookContexts[hookContexts.length - 1];
const name =
node.callee.type === 'MemberExpression'
? getStringValue(node.callee.property)
: '';

currentLayer[name] ||= 0;
currentLayer[name] += 1;

if (currentLayer[name] > 1) {
context.report({
data: { hook: name },
messageId: 'noDuplicateHook',
node,
});
}
},
'CallExpression:exit'(node) {
if (isDescribeCall(node)) {
hookContexts.pop();
}
},
};
},
meta: {
docs: {
category: 'Best Practices',
description: 'Disallow duplicate setup and teardown hooks',
recommended: false,
url: 'https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-duplicate-hooks.md',
},
messages: {
noDuplicateHook: 'Duplicate {{ hook }} in describe block',
},
type: 'suggestion',
},
} as Rule.RuleModule;
Loading
Loading