Skip to content

Feature add no force option rule #47

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

Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
83ca91d
rule: add new noElementHandle rule
elaichenkov Apr 10, 2022
09acdee
chore: move end range to const
elaichenkov Apr 10, 2022
bf274cc
fix: getRange method for non-awaiting statements
elaichenkov Apr 10, 2022
6ba89e1
test: add additional tests for the noElementHandle rule
elaichenkov Apr 10, 2022
e151e3c
chore: improve noElementHandle rule
elaichenkov Apr 10, 2022
fc8c9b3
docs: update the wording of noElementHandle rule
elaichenkov Apr 11, 2022
3e8c4e8
rule: update message to match with other rule naming conventions
elaichenkov Apr 11, 2022
54725f3
rule: change noElementHandle to warn level
elaichenkov Apr 11, 2022
bad4c71
rule: noElementHandle change fixable to suggestion
elaichenkov Apr 11, 2022
4890c75
test: add valid tests for noElementHandle rule
elaichenkov Apr 11, 2022
7bb335b
rule: change type for noElementHandle to suggestion
elaichenkov Apr 11, 2022
03f016e
rule: update noElementHandle rule's message for suggestions
elaichenkov Apr 11, 2022
c776eed
rule: update noElementHandle with getRange method
elaichenkov Apr 11, 2022
1651e94
Merge branch 'playwright-community:master' into no-element-handle-rule
elaichenkov Apr 11, 2022
5e9c6e8
Merge branch 'master' of https://github.com/playwright-community/esli…
elaichenkov Apr 12, 2022
813fc04
Merge branch 'master' of https://github.com/playwright-community/esli…
elaichenkov Apr 13, 2022
2f7fec8
Merge branch 'master' of https://github.com/playwright-community/esli…
elaichenkov Apr 15, 2022
bd0899f
rule: add noForceOption
elaichenkov Apr 22, 2022
27aba43
docs: change Example > Examples
elaichenkov Apr 22, 2022
96058de
test: add wrapInTest function
elaichenkov Apr 24, 2022
c6f6a3b
Merge branch 'master' of https://github.com/playwright-community/esli…
elaichenkov Apr 24, 2022
fafc23f
chore: use helper function
elaichenkov Apr 24, 2022
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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,27 @@ test.describe('two tests', () => {
test('two', async ({ page }) => {});
});
```

### `no-force-option`

Disallow usage of the `{ force: true }` option.

Examples of **incorrect** code for this rule:

```js
await page.locator('button').click({ force: true });

await page.locator('check').check({ force: true });

await page.locator('input').fill('something', { force: true });
```

Examples of **correct** code for this rule:

```js
await page.locator('button').click();

await page.locator('check').check();

await page.locator('input').fill('something');
```
3 changes: 3 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const noEval = require("./rules/no-eval");
const noFocusedTest = require("./rules/no-focused-test");
const noSkippedTest = require("./rules/no-skipped-test");
const noWaitForTimeout = require("./rules/no-wait-for-timeout");
const noForceOption = require("./rules/no-force-option");

module.exports = {
configs: {
Expand All @@ -22,6 +23,7 @@ module.exports = {
"playwright/no-focused-test": "error",
"playwright/no-skipped-test": "warn",
"playwright/no-wait-for-timeout": "warn",
"playwright/no-force-option": "warn",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one, I'm not sure about for the default rules, I can see both sides. @mxschmitt Thoughts?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usually you never have to use force. Only when Playwright has bugs or the application which you are testing does not follow the accessibility standards. So I'd vote for emitting a warning, if users scream, we can change it anyway or they disable the rule manually.

},
},
"jest-playwright": {
Expand Down Expand Up @@ -65,5 +67,6 @@ module.exports = {
"no-focused-test": noFocusedTest,
"no-skipped-test": noSkippedTest,
"no-wait-for-timeout": noWaitForTimeout,
"no-force-option": noForceOption,
},
};
52 changes: 52 additions & 0 deletions lib/rules/no-force-option.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
function isForceOptionEnabled({ parent }) {
return (
parent &&
parent.arguments &&
parent.arguments.length &&
parent.arguments.some(
(argument) =>
argument.type === 'ObjectExpression' &&
argument.properties.some(({ key, value }) => key && key.name === 'force' && value && value.value === true)
)
);
}

// https://playwright.dev/docs/api/class-locator
const methodsWithForceOption = new Set([
'check',
'uncheck',
'click',
'dblclick',
'dragTo',
'fill',
'hover',
'selectOption',
'selectText',
'setChecked',
'tap',
]);

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
create(context) {
return {
MemberExpression(node) {
if (node.property && methodsWithForceOption.has(node.property.name) && isForceOptionEnabled(node)) {
context.report({ messageId: 'noForceOption', node });
}
},
};
},
meta: {
docs: {
category: 'Best Practices',
description: 'Prevent usage of `{ force: true }` option.',
recommended: true,
url: 'https://github.com/playwright-community/eslint-plugin-playwright#no-force-option',
},
messages: {
noForceOption: 'Unexpected use of { force: true } option.',
},
type: 'suggestion',
},
};
58 changes: 58 additions & 0 deletions test/no-force-option.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const { RuleTester } = require('eslint');
const rule = require('../lib/rules/no-force-option');

RuleTester.setDefaultConfig({
parserOptions: {
ecmaVersion: 2018,
},
});

const invalid = (code) => ({
code,
errors: [{ messageId: 'noForceOption' }],
});

new RuleTester().run('no-force-option', rule, {
invalid: [
invalid(`test('should work', async ({ page }) => { await page.locator('check').check({ force: true }) });`),
invalid(`test('should work', async ({ page }) => { await page.locator('check').uncheck({ force: true }) });`),
invalid(`test('should work', async ({ page }) => { await page.locator('button').click({ force: true }) });`),
invalid(
`test('should work', async ({ page }) => { const button = page.locator('button'); await button.click({ force: true }) });`
),
invalid(
`test('should work', async ({ page }) => { await page.locator('button').locator('btn').click({ force: true }) });`
),
invalid(`test('should work', async ({ page }) => { await page.locator('button').dblclick({ force: true }) });`),
invalid(`test('should work', async ({ page }) => { await page.locator('input').dragTo({ force: true }) });`),
invalid(
`test('should work', async ({ page }) => { await page.locator('input').fill('something', { force: true }) });`
),
invalid(`test('should work', async ({ page }) => { await page.locator('elm').hover({ force: true }) });`),
invalid(
`test('should work', async ({ page }) => { await page.locator('select').selectOption({ label: 'Blue' }, { force: true }) });`
),
invalid(`test('should work', async ({ page }) => { await page.locator('select').selectText({ force: true }) });`),
invalid(
`test('should work', async ({ page }) => { await page.locator('checkbox').setChecked(true, { force: true }) });`
),
invalid(`test('should work', async ({ page }) => { await page.locator('button').tap({ force: true }) });`),
],
valid: [
`test('should work', async ({ page }) => { await page.locator('check').check() });`,
`test('should work', async ({ page }) => { await page.locator('check').uncheck() });`,
`test('should work', async ({ page }) => { await page.locator('button').click() });`,
`test('should work', async ({ page }) => { await page.locator('button').locator('btn').click() });`,
`test('should work', async ({ page }) => { await page.locator('button').click({ delay: 500, noWaitAfter: true }) });`,
`test('should work', async ({ page }) => { await page.locator('button').dblclick() });`,
`test('should work', async ({ page }) => { await page.locator('input').dragTo() });`,
`test('should work', async ({ page }) => { await page.locator('input').fill('something', { timeout: 1000 }) });`,
`test('should work', async ({ page }) => { await page.locator('elm').hover() });`,
`test('should work', async ({ page }) => { await page.locator('select').selectOption({ label: 'Blue' }) });`,
`test('should work', async ({ page }) => { await page.locator('select').selectText() });`,
`test('should work', async ({ page }) => { await page.locator('checkbox').setChecked(true) });`,
`test('should work', async ({ page }) => { await page.locator('button').tap() });`,
`doSomething({ force: true });`,
`test('should work', async ({ page }) => { await doSomething({ force: true }) });`,
],
});