• Stars
    star
    958
  • Rank 46,050 (Top 1.0 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated 16 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ESLint plugin to follow best practices and anticipate common mistakes when writing tests with Testing Library

eslint-plugin-testing-library

ESLint plugin to follow best practices and anticipate common mistakes when writing tests with Testing Library


Build status Package version eslint-remote-tester eslint-plugin-testing-library MIT License
semantic-release PRs Welcome All Contributors
Watch on Github Star on Github Tweet

Installation

You'll first need to install ESLint:

$ npm install --save-dev eslint
# or
$ yarn add --dev eslint

Next, install eslint-plugin-testing-library:

$ npm install --save-dev eslint-plugin-testing-library
# or
$ yarn add --dev eslint-plugin-testing-library

Note: If you installed ESLint globally (using the -g flag) then you must also install eslint-plugin-testing-library globally.

Migrating

You can find detailed guides for migrating eslint-plugin-testing-library in the migration guide docs:

Usage

Add testing-library to the plugins section of your .eslintrc.js configuration file. You can omit the eslint-plugin- prefix:

module.exports = {
	plugins: ['testing-library'],
};

Then configure the rules you want to use within rules property of your .eslintrc:

module.exports = {
	rules: {
		'testing-library/await-async-queries': 'error',
		'testing-library/no-await-sync-queries': 'error',
		'testing-library/no-debugging-utils': 'warn',
		'testing-library/no-dom-import': 'off',
	},
};

Run the plugin only against test files

With the default setup mentioned before, eslint-plugin-testing-library will be run against your whole codebase. If you want to run this plugin only against your tests files, you have the following options:

ESLint overrides

One way of restricting ESLint config by file patterns is by using ESLint overrides.

Assuming you are using the same pattern for your test files as Jest by default, the following config would run eslint-plugin-testing-library only against your test files:

// .eslintrc.js
module.exports = {
	// 1) Here we have our usual config which applies to the whole project, so we don't put testing-library preset here.
	extends: ['airbnb', 'plugin:prettier/recommended'],

	// 2) We load other plugins than eslint-plugin-testing-library globally if we want to.
	plugins: ['react-hooks'],

	overrides: [
		{
			// 3) Now we enable eslint-plugin-testing-library rules or preset only for matching testing files!
			files: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'],
			extends: ['plugin:testing-library/react'],
		},
	],
};

ESLint Cascading and Hierarchy

Another approach for customizing ESLint config by paths is through ESLint Cascading and Hierarchy. This is useful if all your tests are placed under the same folder, so you can place there another .eslintrc where you enable eslint-plugin-testing-library for applying it only to the files under such folder, rather than enabling it on your global .eslintrc which would apply to your whole project.

Shareable configurations

This plugin exports several recommended configurations that enforce good practices for specific Testing Library packages. You can find more info about enabled rules in the Supported Rules section, under the Configurations column.

Since each one of these configurations is aimed at a particular Testing Library package, they are not extendable between them, so you should use only one of them at once per .eslintrc file. For example, if you want to enable recommended configuration for React, you don't need to combine it somehow with DOM one:

// ❌ Don't do this
module.exports = {
	extends: ['plugin:testing-library/dom', 'plugin:testing-library/react'],
};
// βœ… Just do this instead
module.exports = {
	extends: ['plugin:testing-library/react'],
};

DOM Testing Library

Enforces recommended rules for DOM Testing Library.

To enable this configuration use the extends property in your .eslintrc.js config file:

module.exports = {
	extends: ['plugin:testing-library/dom'],
};

Angular

Enforces recommended rules for Angular Testing Library.

To enable this configuration use the extends property in your .eslintrc.js config file:

module.exports = {
	extends: ['plugin:testing-library/angular'],
};

React

Enforces recommended rules for React Testing Library.

To enable this configuration use the extends property in your .eslintrc.js config file:

module.exports = {
	extends: ['plugin:testing-library/react'],
};

Vue

Enforces recommended rules for Vue Testing Library.

To enable this configuration use the extends property in your .eslintrc.js config file:

module.exports = {
	extends: ['plugin:testing-library/vue'],
};

Marko

Enforces recommended rules for Marko Testing Library.

To enable this configuration use the extends property in your .eslintrc.js config file:

module.exports = {
	extends: ['plugin:testing-library/marko'],
};

Supported Rules

Remember that all rules from this plugin are prefixed by "testing-library/"

πŸ’Ό Configurations enabled in.
⚠️ Configurations set to warn in.
πŸ”§ Automatically fixable by the --fix CLI option.

NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  Description πŸ’Ό ⚠️ πŸ”§
await-async-events Enforce promises from async event methods are handled badge-angular badge-dom badge-marko badge-react badge-vue πŸ”§
await-async-queries Enforce promises from async queries to be handled badge-angular badge-dom badge-marko badge-react badge-vue
await-async-utils Enforce promises from async utils to be awaited properly badge-angular badge-dom badge-marko badge-react badge-vue
consistent-data-testid Ensures consistent usage of data-testid
no-await-sync-events Disallow unnecessary await for sync events badge-angular badge-dom badge-react
no-await-sync-queries Disallow unnecessary await for sync queries badge-angular badge-dom badge-marko badge-react badge-vue
no-container Disallow the use of container methods badge-angular badge-marko badge-react badge-vue
no-debugging-utils Disallow the use of debugging utilities like debug badge-angular badge-marko badge-react badge-vue
no-dom-import Disallow importing from DOM Testing Library badge-angular badge-marko badge-react badge-vue πŸ”§
no-global-regexp-flag-in-query Disallow the use of the global RegExp flag (/g) in queries badge-angular badge-dom badge-marko badge-react badge-vue πŸ”§
no-manual-cleanup Disallow the use of cleanup badge-react badge-vue
no-node-access Disallow direct Node access badge-angular badge-dom badge-marko badge-react badge-vue
no-promise-in-fire-event Disallow the use of promises passed to a fireEvent method badge-angular badge-dom badge-marko badge-react badge-vue
no-render-in-lifecycle Disallow the use of render in testing frameworks setup functions badge-angular badge-marko badge-react badge-vue
no-unnecessary-act Disallow wrapping Testing Library utils or empty callbacks in act badge-marko badge-react
no-wait-for-multiple-assertions Disallow the use of multiple expect calls inside waitFor badge-angular badge-dom badge-marko badge-react badge-vue
no-wait-for-side-effects Disallow the use of side effects in waitFor badge-angular badge-dom badge-marko badge-react badge-vue
no-wait-for-snapshot Ensures no snapshot is generated inside of a waitFor call badge-angular badge-dom badge-marko badge-react badge-vue
prefer-explicit-assert Suggest using explicit assertions rather than standalone queries
prefer-find-by Suggest using find(All)By* query instead of waitFor + get(All)By* to wait for elements badge-angular badge-dom badge-marko badge-react badge-vue πŸ”§
prefer-presence-queries Ensure appropriate get*/query* queries are used with their respective matchers badge-angular badge-dom badge-marko badge-react badge-vue
prefer-query-by-disappearance Suggest using queryBy* queries when waiting for disappearance badge-angular badge-dom badge-marko badge-react badge-vue
prefer-query-matchers Ensure the configured get*/query* query is used with the corresponding matchers
prefer-screen-queries Suggest using screen while querying badge-angular badge-dom badge-marko badge-react badge-vue
prefer-user-event Suggest using userEvent over fireEvent for simulating user interactions
render-result-naming-convention Enforce a valid naming for return value from render badge-angular badge-marko badge-react badge-vue

Aggressive Reporting

In v4 this plugin introduced a new feature called "Aggressive Reporting", which intends to detect Testing Library utils usages even if they don't come directly from a Testing Library package (i.e. using a custom utility file to re-export everything from Testing Library). You can read more about this feature here.

If you are looking to restricting or switching off this feature, please refer to the Shared Settings section to do so.

Shared Settings

There are some configuration options available that will be shared across all the plugin rules. This is achieved using ESLint Shared Settings. These Shared Settings are meant to be used if you need to restrict or switch off the Aggressive Reporting, which is an out of the box advanced feature to lint Testing Library usages in a simpler way for most of the users. So please before configuring any of these settings, read more about the advantages of eslint-plugin-testing-library Aggressive Reporting feature, and how it's affected by these settings.

If you are sure about configuring the settings, these are the options available:

testing-library/utils-module

The name of your custom utility file from where you re-export everything from the Testing Library package, or "off" to switch related Aggressive Reporting mechanism off. Relates to Aggressive Imports Reporting.

// .eslintrc.js
module.exports = {
	settings: {
		'testing-library/utils-module': 'my-custom-test-utility-file',
	},
};

You can find more details about the utils-module setting here.

testing-library/custom-renders

A list of function names that are valid as Testing Library custom renders, or "off" to switch related Aggressive Reporting mechanism off. Relates to Aggressive Renders Reporting.

// .eslintrc.js
module.exports = {
	settings: {
		'testing-library/custom-renders': ['display', 'renderWithProviders'],
	},
};

You can find more details about the custom-renders setting here.

testing-library/custom-queries

A list of query names/patterns that are valid as Testing Library custom queries, or "off" to switch related Aggressive Reporting mechanism off. Relates to Aggressive Reporting - Queries

// .eslintrc.js
module.exports = {
	settings: {
		'testing-library/custom-queries': ['ByIcon', 'getByComplexText'],
	},
};

You can find more details about the custom-queries setting here.

Switching all Aggressive Reporting mechanisms off

Since each Shared Setting is related to one Aggressive Reporting mechanism, and they accept "off" to opt out of that mechanism, you can switch the entire feature off by doing:

// .eslintrc.js
module.exports = {
	settings: {
		'testing-library/utils-module': 'off',
		'testing-library/custom-renders': 'off',
		'testing-library/custom-queries': 'off',
	},
};

Troubleshooting

Errors reported in non-testing files

If you find ESLint errors related to eslint-plugin-testing-library in files other than testing, this could be caused by Aggressive Reporting.

You can avoid this by:

  1. running eslint-plugin-testing-library only against testing files
  2. limiting the scope of Aggressive Reporting through Shared Settings
  3. switching Aggressive Reporting feature off

If you think the error you are getting is not related to this at all, please fill a new issue with as many details as possible.

False positives in testing files

If you are getting false positive ESLint errors in your testing files, this could be caused by Aggressive Reporting.

You can avoid this by:

  1. limiting the scope of Aggressive Reporting through Shared Settings
  2. switching Aggressive Reporting feature off

If you think the error you are getting is not related to this at all, please fill a new issue with as many details as possible.

Other documentation

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Mario BeltrΓ‘n AlarcΓ³n
Mario BeltrΓ‘n AlarcΓ³n

πŸ’» πŸ“– πŸ‘€ ⚠️ πŸš‡ πŸ›
Thomas Lombart
Thomas Lombart

πŸ’» πŸ“– πŸ‘€ ⚠️ πŸš‡
Ben Monro
Ben Monro

πŸ’» πŸ“– ⚠️
Nicola Molinari
Nicola Molinari

πŸ’» ⚠️ πŸ“– πŸ‘€
AarΓ³n GarcΓ­a HervΓ‘s
AarΓ³n GarcΓ­a HervΓ‘s

πŸ“–
Matej Ε nuderl
Matej Ε nuderl

πŸ€” πŸ“–
AdriΓ  Fontcuberta
AdriΓ  Fontcuberta

πŸ’» ⚠️
Jon Aldinger
Jon Aldinger

πŸ“–
Thomas Knickman
Thomas Knickman

πŸ’» πŸ“– ⚠️
Kevin Sullivan
Kevin Sullivan

πŸ“–
Jakub JastrzΔ™bski
Jakub JastrzΔ™bski

πŸ’» πŸ“– ⚠️
Nikolay Stoynov
Nikolay Stoynov

πŸ“–
marudor
marudor

πŸ’» ⚠️
Tim Deschryver
Tim Deschryver

πŸ’» πŸ“– πŸ€” πŸ‘€ ⚠️ πŸ› πŸš‡ πŸ“¦
Tobias Deekens
Tobias Deekens

πŸ›
Victor Cordova
Victor Cordova

πŸ’» ⚠️ πŸ›
Dmitry Lobanov
Dmitry Lobanov

πŸ’» ⚠️
Kent C. Dodds
Kent C. Dodds

πŸ›
Gonzalo D'Elia
Gonzalo D'Elia

πŸ’» ⚠️ πŸ“– πŸ‘€
Jeff Rifwald
Jeff Rifwald

πŸ“–
Leandro Lourenci
Leandro Lourenci

πŸ› πŸ’» ⚠️
Miguel Erja GonzΓ‘lez
Miguel Erja GonzΓ‘lez

πŸ›
Pavel Pustovalov
Pavel Pustovalov

πŸ›
Jacob Parish
Jacob Parish

πŸ› πŸ’» ⚠️
Nick McCurdy
Nick McCurdy

πŸ€” πŸ’» πŸ‘€
Stefan Cameron
Stefan Cameron

πŸ›
Mateus Felix
Mateus Felix

πŸ’» ⚠️ πŸ“–
Renato Augusto Gama dos Santos
Renato Augusto Gama dos Santos

πŸ€” πŸ’» πŸ“– ⚠️
Josh Kelly
Josh Kelly

πŸ’»
Alessia Bellisario
Alessia Bellisario

πŸ’» ⚠️ πŸ“–
Spencer Miskoviak
Spencer Miskoviak

πŸ’» ⚠️ πŸ“– πŸ€”
Giorgio Polvara
Giorgio Polvara

πŸ’» ⚠️ πŸ“–
Josh David
Josh David

πŸ“–
MichaΓ«l De Boey
MichaΓ«l De Boey

πŸ’» πŸ“¦ 🚧 πŸš‡ πŸ‘€
Jian Huang
Jian Huang

πŸ’» ⚠️ πŸ“–
Philipp Fritsche
Philipp Fritsche

πŸ’»
Tomas Zaicevas
Tomas Zaicevas

πŸ› πŸ’» ⚠️ πŸ“–
Gareth Jones
Gareth Jones

πŸ’» πŸ“– ⚠️
HonkingGoose
HonkingGoose

πŸ“– 🚧
Julien Wajsberg
Julien Wajsberg

πŸ› πŸ’» ⚠️
Marat Dyatko
Marat Dyatko

πŸ› πŸ’»
David Tolman
David Tolman

πŸ›
Ari PerkkiΓΆ
Ari PerkkiΓΆ

⚠️
Diego Castillo
Diego Castillo

πŸ’»
Bruno Pinto
Bruno Pinto

πŸ’» ⚠️
themagickoala
themagickoala

πŸ’» ⚠️
Prashant Ashok
Prashant Ashok

πŸ’» ⚠️
Ivan Aprea
Ivan Aprea

πŸ’» ⚠️
Dmitry Semigradsky
Dmitry Semigradsky

πŸ’» ⚠️ πŸ“–
Senja
Senja

πŸ’» ⚠️ πŸ“–
Breno Cota
Breno Cota

πŸ’» ⚠️
Nick Bolles
Nick Bolles

πŸ’» ⚠️ πŸ“–
Bryan Mishkin
Bryan Mishkin

πŸ“– πŸ”§
Nim G
Nim G

πŸ“–
Patrick Ahmetovic
Patrick Ahmetovic

πŸ€” πŸ’» ⚠️
Josh Justice
Josh Justice

πŸ’» ⚠️ πŸ“– πŸ€”
Dale Karp
Dale Karp

πŸ’» ⚠️ πŸ“–
Nathan
Nathan

πŸ’» ⚠️

This project follows the all-contributors specification. Contributions of any kind welcome!

More Repositories

1

react-testing-library

🐐 Simple and complete React DOM testing utilities that encourage good testing practices.
JavaScript
18,655
star
2

react-hooks-testing-library

🐏 Simple and complete React hooks testing utilities that encourage good testing practices.
TypeScript
5,083
star
3

jest-dom

πŸ¦‰ Custom jest matchers to test the state of the DOM
JavaScript
4,303
star
4

dom-testing-library

πŸ™ Simple and complete DOM testing utilities that encourage good testing practices.
JavaScript
3,234
star
5

user-event

πŸ• Simulate user events
TypeScript
2,119
star
6

cypress-testing-library

πŸ… Simple and complete custom Cypress commands and utilities that encourage good testing practices.
JavaScript
1,782
star
7

vue-testing-library

🦎 Simple and complete Vue.js testing utilities that encourage good testing practices.
JavaScript
1,047
star
8

testing-playground

Simple and complete DOM testing playground that encourage good testing practices.
JavaScript
770
star
9

angular-testing-library

πŸ¦” Simple and complete Angular testing utilities that encourage good testing practices
TypeScript
626
star
10

svelte-testing-library

🐿️ Simple and complete Svelte DOM testing utilities that encourage good testing practices
JavaScript
601
star
11

native-testing-library

🐳 Simple and complete React Native testing utilities that encourage good testing practices.
JavaScript
516
star
12

testing-library-docs

docs site for @testing-library/*
JavaScript
442
star
13

jest-native

πŸ¦… Custom jest matchers to test the state of React Native
TypeScript
432
star
14

eslint-plugin-jest-dom

eslint rules for use with jest-dom
JavaScript
352
star
15

pptr-testing-library

puppeteer + dom-testing-library = πŸ’–
TypeScript
283
star
16

playwright-testing-library

πŸ” Find elements in Playwright with queries from Testing Library
TypeScript
246
star
17

testing-library-recorder-extension

Testing Library Extension for Chrome DevTools Recorder
TypeScript
143
star
18

preact-testing-library

Simple and complete Preact DOM testing utilities that encourage good testing practices.
JavaScript
139
star
19

which-query

🦩 Which query should I use?
CSS
124
star
20

testcafe-testing-library

πŸ‚ Simple and complete custom Selectors for Testcafe that encourage good testing practices.
TypeScript
71
star
21

preact-hooks-testing-library

Simple and complete Preact hooks testing utilities that encourage good testing practices.
TypeScript
56
star
22

jasmine-dom

πŸ¦₯ Custom Jasmine matchers to test the state of the DOM
JavaScript
45
star
23

nightwatch-testing-library

πŸ¦‡Simple and complete custom queries for Nightwatch that encourage good testing practices.
JavaScript
31
star
24

dom-testing-library-template

Template repository for bug reports to @testing-library/dom, @testing-library/react, and @testing-library/jest-dom
JavaScript
17
star
25

webdriverio-testing-library

πŸ•·οΈ Simple and complete WebdriverIO DOM testing utilities that encourage good testing practices.
TypeScript
16
star
26

native-testing-library-docs

🐳 Docs site for native-testing-library
JavaScript
16
star
27

react-testing-library-help

Fork this repo to reproduce your issue
HTML
12
star
28

web-testing-library

πŸ™ Experimental Web testing utilities that encourage good testing practices.
JavaScript
3
star