Skip to content

Commit

Permalink
Add require-assertion rule
Browse files Browse the repository at this point in the history
  • Loading branch information
backspace committed Aug 28, 2023
1 parent 1ef6659 commit 3bdfa90
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
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' }],
},
],
});

0 comments on commit 3bdfa90

Please sign in to comment.