Skip to content

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
merged 12 commits into from
Apr 28, 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
20 changes: 20 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Test
on:
push:
branches: [master]
pull_request:
branches: [master]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x, 14.x, 16.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
56 changes: 44 additions & 12 deletions README.md
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"] }
]
}
```
30 changes: 0 additions & 30 deletions index.js

This file was deleted.

39 changes: 39 additions & 0 deletions lib/index.js
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,
},
};
69 changes: 69 additions & 0 deletions lib/rules/missing-playwright-await.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
function getPromiseMemberExpressionNode(node, matchers) {
if (node.property.type === "Identifier" && matchers.has(node.property.name)) {
return node;
}
}

function isValidExpect(node) {
const parentType =
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";
}

module.exports = {
create(context) {
const options = context.options[0] || {};
const matchers = new Set([
"toHaveSelector",
"toHaveSelectorCount",
"toHaveText",
"toEqualText",
"toEqualValue",
"toEqualUrl",
"toHaveFocus",
// Add any custom matchers to the set
...(options.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",
},
};
Loading