Skip to content

Commit 8497c48

Browse files
authored
fix: don't report expect(el.innerHTML).toBe(foo) (#63)
1 parent 32a5c14 commit 8497c48

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

docs/rules/prefer-empty.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ readability.
88

99
This autofixable rule aims to ensure usage of `.toBeEmptyDOMElement()`
1010

11-
Examples of **incorrect** code for this rule:
11+
Examples of **correct** code for this rule:
1212

1313
```js
1414
expect(element.innerHTML).toBe("foo");
15+
expect(element.innerHTML).toBe(foo);
1516
expect(element.innerHTML).not.toBe("foo");
17+
expect(element.innerHTML).not.toBe(foo);
1618
expect(element.firstChild).toBe("foo");
1719
expect(element.firstChild).not.toBe("foo");
1820
expect(getByText("foo").innerHTML).toBe("foo");
@@ -28,7 +30,7 @@ expect(element.firstChild !== null).toBe(false);
2830
expect(element.firstChild === null).toBe(false);
2931
```
3032

31-
Examples of **correct** code for this rule:
33+
Examples of **incorrect** code for this rule:
3234

3335
```js
3436
expect(element.innerHTML).toBe("");

src/__tests__/lib/rules/prefer-empty.js

+2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ const ruleTester = new RuleTester();
1818
ruleTester.run("prefer-empty", rule, {
1919
valid: [
2020
`expect(element.innerHTML).toBe('foo')`,
21+
`expect(element.innerHTML).toBe(foo)`,
2122
`expect(element.innerHTML).not.toBe('foo')`,
23+
`expect(element.innerHTML).not.toBe(foo)`,
2224
`expect(element.firstChild).toBe('foo')`,
2325
`expect(element.firstChild).not.toBe('foo')`,
2426
`expect(getByText("foo").innerHTML).toBe('foo')`,

src/rules/prefer-empty.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ export const create = (context) => ({
5555
[`MemberExpression[property.name = 'innerHTML'][parent.callee.name = 'expect'][parent.parent.property.name = /toBe$|to(Strict)?Equal/]`](
5656
node
5757
) {
58-
if (node.parent.parent.parent.arguments[0].value) {
58+
const args = node.parent.parent.parent.arguments[0];
59+
if (args.value || args.name) {
5960
return;
6061
}
6162

@@ -72,7 +73,8 @@ export const create = (context) => ({
7273
[`MemberExpression[property.name='innerHTML'][parent.parent.property.name='not'][parent.parent.parent.property.name=/toBe$|to(Strict)?Equal$/][parent.parent.object.callee.name='expect']`](
7374
node
7475
) {
75-
if (node.parent.parent.parent.parent.arguments[0].value) {
76+
const args = node.parent.parent.parent.parent.arguments[0];
77+
if (args.value || args.name) {
7678
return;
7779
}
7880

0 commit comments

Comments
 (0)