-
Notifications
You must be signed in to change notification settings - Fork 43
Missing playwright expect rule #4
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
mxschmitt
merged 12 commits into
playwright-community:master
from
mskelton:missing-playwright-expect-rule
Apr 28, 2021
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
97eddf9
Install Jest
80a25d9
Move output file
47b7044
Add missing playwright await rule
b7c84d4
Add docs
0c8d65c
Fix up docs
3ded0b0
Fix package-lock
9e0ff18
Remove newline
58e5ee0
Better test
bd583c1
Add test action
b3451f2
Fix for Node 12
0b2567e
Oops
4d7313d
Update action
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,12 @@ | ||
name: Test | ||
on: [push] | ||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-node@v1 | ||
with: | ||
node-version: 12.x | ||
- run: npm ci | ||
- run: npm test |
This file contains hidden or 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 @@ | ||
node_modules/ |
This file contains hidden or 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 |
---|---|---|
@@ -1,29 +1,61 @@ | ||
# ESLint Jest Playwright globals | ||
# ESLint Jest Playwright | ||
|
||
> ESLint globals for your [Jest](https://jestjs.io/) [Playwright](https://github.com/microsoft/playwright) ([jest-playwright](https://github.com/mmarkelov/jest-playwright/)) installation. | ||
> ESLint plugin for your [Jest](https://jestjs.io/) [Playwright](https://github.com/microsoft/playwright) ([jest-playwright](https://github.com/mmarkelov/jest-playwright/)) installation. | ||
|
||
## Installation | ||
|
||
Yarn | ||
|
||
```txt | ||
yarn add -D eslint-plugin-jest-playwright | ||
``` | ||
|
||
NPM | ||
|
||
```txt | ||
npm install -D eslint-plugin-jest-playwright | ||
``` | ||
|
||
## Usage | ||
|
||
Add `plugin:jest-playwright/recommended` to your extends ESLint configuration. For example: | ||
|
||
```json | ||
{ | ||
"extends": [ | ||
"plugin:jest-playwright/recommended" | ||
] | ||
"extends": ["plugin:jest-playwright/recommended"], | ||
"plugins": ["jest-playwright"] | ||
} | ||
``` | ||
|
||
## Installation | ||
## Rules | ||
|
||
Yarn | ||
### `missing-playwright-expect` 🔧 | ||
|
||
```txt | ||
yarn add -D eslint-plugin-jest-playwright | ||
Enforce Jest Playwright expect statements to be awaited. | ||
|
||
#### Example | ||
|
||
Example of **incorrect** code for this rule: | ||
|
||
```js | ||
expect(page).toHaveText("text"); | ||
``` | ||
|
||
NPM | ||
Example of **correct** code for this rule: | ||
|
||
```txt | ||
npm install -D eslint-plugin-jest-playwright | ||
```js | ||
await expect(page).toHaveText("text"); | ||
``` | ||
|
||
#### 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. | ||
|
||
```json | ||
{ | ||
"jest-playwright/missing-playwright-await": [ | ||
"error", | ||
{ "customMatchers": ["toHaveAttribute"] } | ||
] | ||
} | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,39 @@ | ||
const missingPlaywrightAwait = require("./rules/missing-playwright-await"); | ||
|
||
module.exports = { | ||
configs: { | ||
recommended: { | ||
env: { | ||
"shared-node-browser": true, | ||
jest: true, | ||
}, | ||
rules: { | ||
"jest-playwright/missing-playwright-await": "error", | ||
"jest/no-standalone-expect": [ | ||
"error", | ||
{ | ||
additionalTestBlockFunctions: [ | ||
"test.jestPlaywrightDebug", | ||
"it.jestPlaywrightDebug", | ||
"test.jestPlaywrightSkip", | ||
"it.jestPlaywrightSkip", | ||
"test.jestPlaywrightConfig", | ||
"it.jestPlaywrightConfig", | ||
], | ||
}, | ||
], | ||
}, | ||
globals: { | ||
browserName: true, | ||
deviceName: true, | ||
browser: true, | ||
context: true, | ||
page: true, | ||
jestPlaywright: true, | ||
}, | ||
}, | ||
}, | ||
rules: { | ||
"missing-playwright-await": missingPlaywrightAwait, | ||
}, | ||
}; |
This file contains hidden or 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,67 @@ | ||
function getPromiseMemberExpressionNode(node, matchers) { | ||
if (node.property.type === "Identifier" && matchers.has(node.property.name)) { | ||
return node; | ||
} | ||
} | ||
|
||
function isValidExpect(node) { | ||
const parentType = node.parent?.parent?.type; | ||
|
||
// Don't report on nodes which are already awaited or returned | ||
return parentType === "AwaitExpression" || parentType === "ReturnStatement"; | ||
} | ||
|
||
module.exports = { | ||
create(context) { | ||
const matchers = new Set([ | ||
"toHaveSelector", | ||
"toHaveSelectorCount", | ||
"toHaveText", | ||
"toEqualText", | ||
"toEqualValue", | ||
"toEqualUrl", | ||
"toHaveFocus", | ||
// Add any custom matchers to the set | ||
...(context.options[0]?.customMatchers ?? []), | ||
]); | ||
|
||
return { | ||
MemberExpression(statement) { | ||
const node = getPromiseMemberExpressionNode(statement, matchers); | ||
if (!node || isValidExpect(node)) return; | ||
|
||
context.report({ | ||
fix(fixer) { | ||
return fixer.insertTextBefore(node, "await "); | ||
}, | ||
messageId: "missingAwait", | ||
node, | ||
}); | ||
}, | ||
}; | ||
}, | ||
meta: { | ||
docs: { | ||
category: "Possible Errors", | ||
description: "Enforce Jest Playwright expect statements to be awaited.", | ||
recommended: true, | ||
}, | ||
fixable: "code", | ||
messages: { | ||
missingAwait: "Playwright expectations must be awaited or returned.", | ||
}, | ||
schema: [ | ||
{ | ||
additionalProperties: false, | ||
properties: { | ||
customMatchers: { | ||
items: { type: "string" }, | ||
type: "array", | ||
}, | ||
}, | ||
type: "object", | ||
}, | ||
], | ||
type: "problem", | ||
}, | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.