Skip to content
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

Add require-assertion rule #336

Merged
merged 1 commit into from
Sep 13, 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Rules
| :-------------------------------------------------- | :---------------------------------------------------- | :- | :- |
| [no-checked-selector](rules/no-checked-selector.md) | disallow use of `assert.dom('.foo:checked').exists()` || 🔧 |
| [no-ok-find](rules/no-ok-find.md) | disallow use of `assert.ok(find(...))` || 🔧 |
| [require-assertion](rules/require-assertion.md) | require at least one assertion on `assert.dom()` || 🔧 |

<!-- end auto-generated rules list -->

Expand Down
1 change: 1 addition & 0 deletions configs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Object {
"rules": Object {
"qunit-dom/no-checked-selector": "error",
"qunit-dom/no-ok-find": "error",
"qunit-dom/require-assertion": "error",
},
}
`);
Expand Down
41 changes: 41 additions & 0 deletions rules/require-assertion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const ASSERT_DOM_SELECTOR =
'CallExpression' +
'[callee.type="MemberExpression"]' +
'[callee.object.type="Identifier"]' +
'[callee.object.name="assert"]' +
'[callee.property.type="Identifier"]' +
'[callee.property.name="dom"]';

module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'require at least one assertion on `assert.dom()`',
recommended: true,
url: 'https://github.com/Mainmatter/eslint-plugin-qunit-dom/blob/main/rules/require-assertion.md',
},
fixable: 'code',
schema: [],
messages: {
default: 'use at least one assertion on assert.dom(...)',
},
},

create(context) {
return {
[ASSERT_DOM_SELECTOR](node) {
if (node.parent.type === 'ExpressionStatement') {
context.report({
node: node,
messageId: 'default',
fix(fixer) {
return fixer.insertTextAfter(node, '.exists()');
},
});
} else {
return;
}
},
};
},
};
23 changes: 23 additions & 0 deletions rules/require-assertion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# require-assertion

💼 This rule is enabled in the ✅ `recommended` config.

🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

<!-- end auto-generated rule header -->

At least one assertion should be made after calling `assert.dom()`.

## Examples

This rule **forbids** the following:

```js
assert.dom('.foo');
```

This rule **allows** the following:

```js
assert.dom('.foo').exists();
```
27 changes: 27 additions & 0 deletions rules/require-assertion.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { RuleTester } = require('eslint');

const rule = require('./require-assertion');

let ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
},
});

ruleTester.run('require-assertion', rule, {
valid: ['assert.dom().exists()', 'assert.dom(node).exists()'],

invalid: [
{
code: 'assert.dom()',
output: 'assert.dom().exists()',
errors: [{ messageId: 'default' }],
},
{
code: 'assert.dom(node)',
output: 'assert.dom(node).exists()',
errors: [{ messageId: 'default' }],
},
],
});