atsikov / eslint-jest-testing-library-codemod

A set of autofixes for eslint-plugin-testing-library to make migration less painful
https://github.com/atsikov/eslint-jest-testing-library-codemod
15 stars 2 forks source link
autofix codemod eslint fixers testing-library

eslint-jest-testing-library-codemod

eslint-jest-testing-library-codemod provides a set of autofixes to make migration to eslint-plugin-testing-library ruleset less painful. Codemod is based on jscodeshift.

Usage

yarn transform --fix=<fixer name> --<option>=<value> <path to folder>

Fixer name is a module name from src/transforms/. Fixers could be provided as a comma-separated list or multiple --fix arguments.

Available fixers in recommended order:

prefer-screen-queries
options:
  memberExpressions: true

prefer-presence-queries

no-manual-cleanup

Codemod will pick all js, jsx, ts, tsx files, ignoring node_modules. jscodeshift params could be passed as well.

Example

yarn transform --fix=prefer-screen-queries,prefer-presence-queries,no-manual-cleanup ./src

Looks cool, right?

Sample transform diff

Fixers

prefer-screen-queries

Fixes testing-library/prefer-screen-queries rule.

Fixer is capable to fix various scenarios. screen import will be added if necessary.

no-manual-cleanup

Fixes testing-library/no-manual-cleanup rule.

it('test', () => { cleanup() render(

) })

is transformed to 

it('test', () => { render(

) })


* Removes jest lifecycle methods in case `cleanup` was the only call

import { cleanup, render } from '@testing-library/react'

afterEach(() => { cleanup() })

test('test', () => { render() })

is transformed to 

import { render } from '@testing-library/react'

it('test', () => { render(

) })


* Handles case when `cleanup` is passed to jest lifecycle method as a callback

import { cleanup, render } from '@testing-library/react'

afterEach(cleanup)

test('test', () => { render() })

is transformed to 

import { render } from '@testing-library/react'

it('test', () => { render(

) })



### prefer-presence-queries

Fixes `testing-library/prefer-presence-queries` rule.

* Replace truthy assertions with `getBy*` queries
* Replace falsy assertions with `queryBy*` queries