Skip to content

Add no-networkidle rule #134

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
merged 4 commits into from
Jun 18, 2023
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
14 changes: 14 additions & 0 deletions docs/rules/no-networkidle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Disallow usage of the `networkidle` option (`no-networkidle`)

Using `networkidle` is discouraged in favor of using
[web first assertions](https://playwright.dev/docs/best-practices#use-web-first-assertions).

## Rule Details

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

```javascript
await page.waitForLoadState('networkidle');
await page.waitForURL('...', { waitUntil: 'networkidle' });
await page.goto('...', { waitUntil: 'networkidle' });
```
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import noElementHandle from './rules/no-element-handle';
import noEval from './rules/no-eval';
import noFocusedTest from './rules/no-focused-test';
import noForceOption from './rules/no-force-option';
import noNetworkidle from './rules/no-networkidle';
import noPagePause from './rules/no-page-pause';
import noRestrictedMatchers from './rules/no-restricted-matchers';
import noSkippedTest from './rules/no-skipped-test';
Expand Down Expand Up @@ -33,6 +34,7 @@ const recommended = {
'playwright/no-eval': 'warn',
'playwright/no-focused-test': 'error',
'playwright/no-force-option': 'warn',
'playwright/no-networkidle': 'error',
'playwright/no-page-pause': 'warn',
'playwright/no-skipped-test': 'warn',
'playwright/no-useless-not': 'warn',
Expand Down Expand Up @@ -87,6 +89,7 @@ export = {
'no-eval': noEval,
'no-focused-test': noFocusedTest,
'no-force-option': noForceOption,
'no-networkidle': noNetworkidle,
'no-page-pause': noPagePause,
'no-restricted-matchers': noRestrictedMatchers,
'no-skipped-test': noSkippedTest,
Expand Down
63 changes: 63 additions & 0 deletions src/rules/no-networkidle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Rule } from 'eslint';
import * as ESTree from 'estree';
import { getStringValue, isStringLiteral } from '../utils/ast';

const messageId = 'noNetworkidle';
const methods = new Set([
'goBack',
'goForward',
'goto',
'reload',
'setContent',
'waitForLoadState',
'waitForURL',
]);

export default {
create(context) {
return {
CallExpression(node) {
if (node.callee.type !== 'MemberExpression') return;

const methodName = getStringValue(node.callee.property);
if (!methods.has(methodName)) return;

// waitForLoadState has a single string argument
if (methodName === 'waitForLoadState') {
const arg = node.arguments[0];

if (isStringLiteral(arg, 'networkidle')) {
context.report({ messageId, node: arg });
}

return;
}

// All other methods have an options object
if (node.arguments.length >= 2) {
const [_, arg] = node.arguments;
if (arg.type !== 'ObjectExpression') return;

const property = arg.properties
.filter((p): p is ESTree.Property => p.type === 'Property')
.find((p) => isStringLiteral(p.value, 'networkidle'));

if (property) {
context.report({ messageId, node: property.value });
}
}
},
};
},
meta: {
docs: {
category: 'Possible Errors',
description: 'Prevent usage of the networkidle option',
recommended: true,
},
messages: {
noNetworkidle: 'Unexpected use of networkidle.',
},
type: 'problem',
},
} as Rule.RuleModule;
59 changes: 59 additions & 0 deletions test/spec/no-networkidle.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import rule from '../../src/rules/no-networkidle';
import { runRuleTester } from '../utils/rule-tester';

const messageId = 'noNetworkidle';

runRuleTester('no-networkidle', rule, {
invalid: [
{
code: 'page.waitForLoadState("networkidle")',
errors: [{ column: 23, endColumn: 36, line: 1, messageId }],
},
{
code: 'page.waitForURL(url, { waitUntil: "networkidle" })',
errors: [{ column: 35, endColumn: 48, line: 1, messageId }],
},
{
code: 'page["waitForURL"](url, { waitUntil: "networkidle" })',
errors: [{ column: 38, endColumn: 51, line: 1, messageId }],
},
{
code: 'page[`waitForURL`](url, { waitUntil: "networkidle" })',
errors: [{ column: 38, endColumn: 51, line: 1, messageId }],
},
{
code: 'page.goto(url, { waitUntil: "networkidle" })',
errors: [{ column: 29, endColumn: 42, line: 1, messageId }],
},
{
code: 'page.reload(url, { waitUntil: "networkidle" })',
errors: [{ column: 31, endColumn: 44, line: 1, messageId }],
},
{
code: 'page.setContent(url, { waitUntil: "networkidle" })',
errors: [{ column: 35, endColumn: 48, line: 1, messageId }],
},
{
code: 'page.goBack(url, { waitUntil: "networkidle" })',
errors: [{ column: 31, endColumn: 44, line: 1, messageId }],
},
{
code: 'page.goForward(url, { waitUntil: "networkidle" })',
errors: [{ column: 34, endColumn: 47, line: 1, messageId }],
},
],
valid: [
'foo("networkidle")',
'foo(url, { waitUntil: "networkidle" })',
'foo.bar("networkidle")',
'foo.bar(url, { waitUntil: "networkidle" })',
'page.hi("networkidle")',
'page.hi(url, { waitUntil: "networkidle" })',
'frame.hi("networkidle")',
'frame.hi(url, { waitUntil: "networkidle" })',

// Other options are valid
'page.waitForLoadState({ waitUntil: "load" })',
'page.waitForUrl(url, { waitUntil: "load" })',
],
});