Skip to content

Add support for test.step in missing-playwright-await rule. #29

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 6 commits into from
Oct 5, 2021
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
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,33 @@ This plugin bundles two configurations to work with both `@playwright/test` or `

### `missing-playwright-await` 🔧

Enforce Playwright expect statements to be awaited.
Identify false positives when async Playwright APIs are not properly awaited.

#### Example

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

```js
expect(page).toMatchText("text");

test.step("clicks the button", async () => {
await page.click("button");
});
```

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

```js
await expect(page).toMatchText("text");

await test.step("clicks the button", async () => {
await page.click("button");
});
```

#### Options

The rule accepts a non-required option which can be used to specify custom matchers which this rule should also warn about. This is useful when creating your own async matchers.
The rule accepts a non-required option which can be used to specify custom matchers which this rule should also warn about. This is useful when creating your own async `expect` matchers.

```json
{
Expand Down
47 changes: 27 additions & 20 deletions lib/rules/missing-playwright-await.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
function getPromiseMemberExpressionNode(node, matchers) {
if (node.property.type === "Identifier" && matchers.has(node.property.name)) {
return node;
function getMemberPartName(node, part) {
return node[part].type === "Identifier" ? node[part].name : undefined;
}

function getMemberExpressionNode(node, matchers) {
const propertyName = getMemberPartName(node, "property");

if (getMemberPartName(node, "object") === "test") {
return propertyName === "step" ? { node, type: "testStep" } : undefined;
}

return matchers.has(propertyName) ? { node, type: "expect" } : undefined;
}

function isValidExpect(node) {
const parentType =
function isValid(node) {
const grandparentType =
node.parent && node.parent.parent && node.parent.parent.type;

// Don't report on nodes which are already awaited or returned
return (
parentType === "AwaitExpression" ||
parentType === "ReturnStatement" ||
parentType === "ArrowFunctionExpression"
grandparentType === "AwaitExpression" ||
grandparentType === "ReturnStatement" ||
grandparentType === "ArrowFunctionExpression"
);
}

Expand Down Expand Up @@ -70,28 +77,28 @@ module.exports = {

return {
MemberExpression(statement) {
const node = getPromiseMemberExpressionNode(statement, matchers);
if (!node || isValidExpect(node)) return;
const result = getMemberExpressionNode(statement, matchers);

context.report({
fix(fixer) {
return fixer.insertTextBefore(node, "await ");
},
messageId: "missingAwait",
node,
});
if (result && !isValid(result.node)) {
context.report({
fix: (fixer) => fixer.insertTextBefore(result.node, "await "),
messageId: result.type,
node: result.node,
});
}
},
};
},
meta: {
docs: {
category: "Possible Errors",
description: "Enforce expect-playwright matchers to be awaited.",
description: `Identify false positives when async Playwright APIs are not properly awaited.`,
recommended: true,
},
fixable: "code",
messages: {
missingAwait: "expect-playwright matchers must be awaited or returned.",
expect: "'expect' matchers must be awaited or returned.",
testStep: "'test.step' must be awaited or returned.",
},
schema: [
{
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 30 additions & 13 deletions test/missing-playwright-await.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ RuleTester.setDefaultConfig({

const wrapInTest = (input) => `test('a', async () => { ${input} })`;

const invalid = (code, output, options = []) => ({
const invalid = (messageId, code, output, options = []) => ({
code: wrapInTest(code),
errors: [{ messageId: "missingAwait" }],
errors: [{ messageId }],
options,
output: wrapInTest(output),
});
Expand All @@ -25,32 +25,46 @@ const options = [{ customMatchers: ["toBeCustomThing"] }];

new RuleTester().run("missing-playwright-await", rule, {
invalid: [
invalid(`expect(page).toBeChecked()`, `await expect(page).toBeChecked()`),
invalid(
`expect(page).not.toBeEnabled()`,
`await expect(page).not.toBeEnabled()`
"expect",
"expect(page).toBeChecked()",
"await expect(page).toBeChecked()"
),
invalid(
"expect",
"expect(page).not.toBeEnabled()",
"await expect(page).not.toBeEnabled()"
),

// Custom matchers
invalid(
`expect(page).toBeCustomThing(false)`,
`await expect(page).toBeCustomThing(false)`,
"expect",
"expect(page).toBeCustomThing(false)",
"await expect(page).toBeCustomThing(false)",
options
),
invalid(
`expect(page).not.toBeCustomThing(true)`,
`await expect(page).not.toBeCustomThing(true)`,
"expect",
"expect(page).not.toBeCustomThing(true)",
"await expect(page).not.toBeCustomThing(true)",
options
),

// test.step
invalid(
"testStep",
"test.step('foo', async () => {})",
"await test.step('foo', async () => {})"
),
],
valid: [
valid(`await expect(page).toEqualTitle("text")`),
valid(`await expect(page).not.toHaveText("text")`),
valid('await expect(page).toEqualTitle("text")'),
valid('await expect(page).not.toHaveText("text")'),

// Doesn't require an await when returning
valid(`return expect(page).toHaveText("text")`),
valid('return expect(page).toHaveText("text")'),
{
code: `const a = () => expect(page).toHaveText("text")`,
code: 'const a = () => expect(page).toHaveText("text")',
options,
},

Expand All @@ -60,5 +74,8 @@ new RuleTester().run("missing-playwright-await", rule, {
valid("await expect(page).toBeCustomThing(true)", options),
valid("await expect(page).toBeCustomThing(true)"),
valid("expect(page).toBeCustomThing(true)"),

// test.step
valid("await test.step('foo', async () => {})"),
],
});