Skip to content
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ command line option.\

| ✔ | 🔧 | 💡 | Rule | Description |
| :-: | :-: | :-: | --------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------- |
| ✔ | | | [expect-expect](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/expect-expect.md) | Enforce assertion to be made in a test body |
| ✔ | | | [max-nested-describe](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/max-nested-describe.md) | Enforces a maximum depth to nested describe calls |
| ✔ | 🔧 | | [missing-playwright-await](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/missing-playwright-await.md) | Enforce Playwright APIs to be awaited |
| ✔ | | | [no-conditional-in-test](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-conditional-in-test.md) | Disallow conditional logic in tests |
Expand Down
29 changes: 29 additions & 0 deletions docs/rules/expect-expect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Enforce assertion to be made in a test body (`expect-expect`)

Ensure that there is at least one `expect` call made in a test.

## Rule Details

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

```javascript
test('should be a test', () => {
console.log('no assertion');
});

test('should assert something', () => {});
```

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

```javascript
test('should be a test', async () => {
await expect(page).toHaveTitle('foo');
});

test('should work with callbacks/async', async () => {
await test.step('step 1', async () => {
await expect(page).toHaveTitle('foo');
});
});
```
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import expectExpect from './rules/expect-expect';
import maxNestedDescribe from './rules/max-nested-describe';
import missingPlaywrightAwait from './rules/missing-playwright-await';
import noConditionalInTest from './rules/no-conditional-in-test';
Expand Down Expand Up @@ -30,6 +31,7 @@ const recommended = {
plugins: ['playwright'],
rules: {
'no-empty-pattern': 'off',
'playwright/expect-expect': 'warn',
'playwright/max-nested-describe': 'warn',
'playwright/missing-playwright-await': 'error',
'playwright/no-conditional-in-test': 'warn',
Expand Down Expand Up @@ -87,6 +89,7 @@ export = {
recommended,
},
rules: {
'expect-expect': expectExpect,
'max-nested-describe': maxNestedDescribe,
'missing-playwright-await': missingPlaywrightAwait,
'no-conditional-in-test': noConditionalInTest,
Expand Down
48 changes: 48 additions & 0 deletions src/rules/expect-expect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Rule } from 'eslint';
import * as ESTree from 'estree';
import { isExpectCall, isTest } from '../utils/ast';

export default {
create(context) {
const unchecked: ESTree.CallExpression[] = [];

function checkExpressions(nodes: ESTree.Node[]) {
for (const node of nodes) {
const index =
node.type === 'CallExpression' ? unchecked.indexOf(node) : -1;

if (index !== -1) {
unchecked.splice(index, 1);
break;
}
}
}

return {
CallExpression(node) {
if (isTest(node)) {
unchecked.push(node);
} else if (isExpectCall(node)) {
checkExpressions(context.getAncestors());
}
},
'Program:exit'() {
unchecked.forEach((node) => {
context.report({ messageId: 'noAssertions', node });
});
},
};
},
meta: {
docs: {
category: 'Best Practices',
description: 'Enforce assertion to be made in a test body',
recommended: true,
url: 'https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/expect-expect.md',
},
messages: {
noAssertions: 'Test has no assertions',
},
type: 'problem',
},
} as Rule.RuleModule;
21 changes: 21 additions & 0 deletions test/spec/expect-expect.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import rule from '../../src/rules/expect-expect';
import { runRuleTester } from '../utils/rule-tester';

runRuleTester('expect-expect', rule, {
invalid: [
{
code: 'test("should fail", () => {});',
errors: [{ messageId: 'noAssertions' }],
},
{
code: 'test.skip("should fail", () => {});',
errors: [{ messageId: 'noAssertions' }],
},
],
valid: [
'foo();',
'["bar"]();',
'testing("will test something eventually", () => {})',
'test("should pass", () => expect(true).toBeDefined())',
],
});