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

feat: add rule no-side-effects-wait-for #196

Merged
merged 16 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 0 additions & 2 deletions docs/rules/no-side-effects-wait-for.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ This will make your side-effect run multiple times.
Example of **incorrect** code for this rule:

```js
const foo = async () => {
await waitFor(() => {
fireEvent.keyDown(input, { key: 'ArrowDown' });
expect(b).toEqual('b');
Expand Down Expand Up @@ -39,7 +38,6 @@ const foo = async () => {
Examples of **correct** code for this rule:

```js
const foo = async () => {
fireEvent.keyDown(input, { key: 'ArrowDown' });
await waitFor(() => {
expect(b).toEqual('b');
Expand Down
15 changes: 10 additions & 5 deletions lib/rules/no-side-effects-wait-for.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils'
import { getDocsUrl } from '../utils'
import { isBlockStatement, findClosestCallNode, isMemberExpression, isCallExpression, isIdentifier } from '../node-utils'

export const RULE_NAME: string = 'no-side-effects-wait-for';
export const RULE_NAME = 'no-side-effects-wait-for';

const WAIT_EXPRESSION_QUERY: string =
const WAIT_EXPRESSION_QUERY =
'CallExpression[callee.name=/^(waitFor)$/]';

const SIDE_EFFECTS: Array<string> = ['fireEvent', 'userEvent']
Expand All @@ -30,11 +30,13 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
},
defaultOptions: [],
create: function(context) {
let isImportingTestingLibrary = false;

function reportSideEffects(
node: TSESTree.BlockStatement
) {
const totalSideEffects = (body: Array<TSESTree.Node>): Array<TSESTree.Node> =>
body.filter((node: TSESTree.ExpressionStatement) => {
const hasSideEffects = (body: Array<TSESTree.Node>): boolean =>
body.some((node: TSESTree.ExpressionStatement) => {
if (
isCallExpression(node.expression) &&
isMemberExpression(node.expression.callee) &&
Expand All @@ -48,7 +50,7 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
}
})

if (isBlockStatement(node) && totalSideEffects(node.body).length > 0) {
if (isImportingTestingLibrary && isBlockStatement(node) && hasSideEffects(node.body)) {
context.report({
node,
loc: node.loc.start,
Expand All @@ -60,6 +62,9 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
return {
[`${WAIT_EXPRESSION_QUERY} > ArrowFunctionExpression > BlockStatement`]: reportSideEffects,
[`${WAIT_EXPRESSION_QUERY} > FunctionExpression > BlockStatement`]: reportSideEffects,
ImportDeclaration(node: TSESTree.ImportDeclaration) {
isImportingTestingLibrary = /testing-library/g.test(node.source.value as string);
renatoagds marked this conversation as resolved.
Show resolved Hide resolved
}
};
}
})
22 changes: 22 additions & 0 deletions tests/lib/rules/no-side-effects-wait-for.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: `
import { waitFor } from '@testing-library/react';
renatoagds marked this conversation as resolved.
Show resolved Hide resolved
await waitFor(() => expect(a).toEqual('a'))
`,
},
{
code: `
import { waitFor } from '@testing-library/react';
await waitFor(function() {
expect(a).toEqual('a')
})
`,
},
{
code: `
import { waitFor } from '@testing-library/react';
await waitFor(() => {
console.log('testing-library')
expect(b).toEqual('b')
Expand All @@ -31,6 +34,7 @@ ruleTester.run(RULE_NAME, rule, {
},
{
code: `
import { waitFor } from '@testing-library/react';
await waitFor(function() {
console.log('testing-library')
expect(b).toEqual('b')
Expand All @@ -39,37 +43,43 @@ ruleTester.run(RULE_NAME, rule, {
},
{
code: `
import { waitFor } from '@testing-library/react';
await waitFor(() => {})
`,
},
{
code: `
import { waitFor } from '@testing-library/react';
await waitFor(function() {})
`,
},
{
code: `
import { waitFor } from '@testing-library/react';
await waitFor(() => {
// testing
})
`,
},
{
code: `
import { waitFor } from '@testing-library/react';
await waitFor(function() {
// testing
})
`,
},
{
code: `
import { waitFor } from '@testing-library/react';
fireEvent.keyDown(input, {key: 'ArrowDown'})
await waitFor(() => {
expect(b).toEqual('b')
})
`
}, {
code: `
import { waitFor } from '@testing-library/react';
fireEvent.keyDown(input, {key: 'ArrowDown'})
await waitFor(function() {
expect(b).toEqual('b')
Expand All @@ -81,6 +91,7 @@ ruleTester.run(RULE_NAME, rule, {
// fireEvent
{
code: `
import { waitFor } from '@testing-library/react';
await waitFor(() => {
fireEvent.keyDown(input, {key: 'ArrowDown'})
})
Expand All @@ -89,6 +100,7 @@ ruleTester.run(RULE_NAME, rule, {
},
{
code: `
import { waitFor } from '@testing-library/react';
await waitFor(() => {
expect(b).toEqual('b')
fireEvent.keyDown(input, {key: 'ArrowDown'})
Expand All @@ -98,6 +110,7 @@ ruleTester.run(RULE_NAME, rule, {
},
{
code: `
import { waitFor } from '@testing-library/react';
await waitFor(() => {
fireEvent.keyDown(input, {key: 'ArrowDown'})
expect(b).toEqual('b')
Expand All @@ -107,6 +120,7 @@ ruleTester.run(RULE_NAME, rule, {
},
{
code: `
import { waitFor } from '@testing-library/react';
await waitFor(function() {
fireEvent.keyDown(input, {key: 'ArrowDown'})
})
Expand All @@ -115,6 +129,7 @@ ruleTester.run(RULE_NAME, rule, {
},
{
code: `
import { waitFor } from '@testing-library/react';
await waitFor(function() {
expect(b).toEqual('b')
fireEvent.keyDown(input, {key: 'ArrowDown'})
Expand All @@ -124,6 +139,7 @@ ruleTester.run(RULE_NAME, rule, {
},
{
code: `
import { waitFor } from '@testing-library/react';
await waitFor(function() {
fireEvent.keyDown(input, {key: 'ArrowDown'})
expect(b).toEqual('b')
Expand All @@ -134,6 +150,7 @@ ruleTester.run(RULE_NAME, rule, {
// userEvent
{
code: `
import { waitFor } from '@testing-library/react';
await waitFor(() => {
userEvent.keyDown(input, {key: 'ArrowDown'})
renatoagds marked this conversation as resolved.
Show resolved Hide resolved
})
Expand All @@ -142,6 +159,7 @@ ruleTester.run(RULE_NAME, rule, {
},
{
code: `
import { waitFor } from '@testing-library/react';
await waitFor(() => {
expect(b).toEqual('b')
userEvent.keyDown(input, {key: 'ArrowDown'})
Expand All @@ -151,6 +169,7 @@ ruleTester.run(RULE_NAME, rule, {
},
{
code: `
import { waitFor } from '@testing-library/react';
await waitFor(() => {
userEvent.keyDown(input, {key: 'ArrowDown'})
expect(b).toEqual('b')
Expand All @@ -160,6 +179,7 @@ ruleTester.run(RULE_NAME, rule, {
},
{
code: `
import { waitFor } from '@testing-library/react';
await waitFor(function() {
userEvent.keyDown(input, {key: 'ArrowDown'})
})
Expand All @@ -168,6 +188,7 @@ ruleTester.run(RULE_NAME, rule, {
},
{
code: `
import { waitFor } from '@testing-library/react';
await waitFor(function() {
expect(b).toEqual('b')
userEvent.keyDown(input, {key: 'ArrowDown'})
Expand All @@ -177,6 +198,7 @@ ruleTester.run(RULE_NAME, rule, {
},
{
code: `
import { waitFor } from '@testing-library/react';
await waitFor(function() {
userEvent.keyDown(input, {key: 'ArrowDown'})
expect(b).toEqual('b')
Expand Down