Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

testing-library/eslint-plugin-testing-library

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

eslint-plugin-testing-library

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


Package versioneslint-remote-testereslint-plugin-testing-librarycodecovMIT License
semantic-releasePRs WelcomeAll Contributors

Prerequisites

To use this plugin, you must haveNode.js (^18.18.0,^20.9.0, or>=21.1.0) installed.

Installation

You'll first need toinstall ESLint.

Next, installeslint-plugin-testing-library:

$ pnpm add --save-dev eslint-plugin-testing-library# or$ 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 installeslint-plugin-testing-library globally.

Migrating

You can find detailed guides for migratingeslint-plugin-testing-library in themigration guide docs:

Usage

Addtesting-library to the plugins section of your.eslintrc.js configuration file. You can omit theeslint-plugin- prefix:

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

Then configure the rules you want to use withinrules 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:

ESLintoverrides

One way of restricting ESLint config by file patterns is by usingESLintoverrides.

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

// .eslintrc.jsmodule.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 throughESLint 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 enableeslint-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

Note

eslint.config.js compatible versions of configs are available prefixed withflat/, though most of the plugin documentation still currently uses.eslintrc syntax.

Refer to theESLint documentation on the new configuration file formatfor more.

This plugin exports several recommended configurations that enforce good practices for specific Testing Library packages.You can find more info about enabled rules in theSupported Rules section, under theConfigurations 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 thismodule.exports={extends:['plugin:testing-library/dom','plugin:testing-library/react'],};
// ✅ Just do this insteadmodule.exports={extends:['plugin:testing-library/react'],};

DOM Testing Library

Enforces recommended rules for DOM Testing Library.

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

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

To enable this configuration witheslint.config.js, usetestingLibrary.configs['flat/dom']:

consttestingLibrary=require('eslint-plugin-testing-library');module.exports=[{files:[/* glob matching your test files */],...testingLibrary.configs['flat/dom'],},];

Angular

Enforces recommended rules for Angular Testing Library.

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

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

To enable this configuration witheslint.config.js, usetestingLibrary.configs['flat/angular']:

consttestingLibrary=require('eslint-plugin-testing-library');module.exports=[{files:[/* glob matching your test files */],...testingLibrary.configs['flat/angular'],},];

React

Enforces recommended rules for React Testing Library.

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

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

To enable this configuration witheslint.config.js, usetestingLibrary.configs['flat/react']:

consttestingLibrary=require('eslint-plugin-testing-library');module.exports=[{files:[/* glob matching your test files */],...testingLibrary.configs['flat/react'],},];

Vue

Enforces recommended rules for Vue Testing Library.

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

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

To enable this configuration witheslint.config.js, usetestingLibrary.configs['flat/vue']:

consttestingLibrary=require('eslint-plugin-testing-library');module.exports=[{files:[/* glob matching your test files */],...testingLibrary.configs['flat/vue'],},];

Svelte

Enforces recommended rules for Svelte Testing Library.

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

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

To enable this configuration witheslint.config.js, usetestingLibrary.configs['flat/svelte']:

consttestingLibrary=require('eslint-plugin-testing-library');module.exports=[{files:[/* glob matching your test files */],...testingLibrary.configs['flat/svelte'],},];

Marko

Enforces recommended rules for Marko Testing Library.

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

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

To enable this configuration witheslint.config.js, usetestingLibrary.configs['flat/marko']:

consttestingLibrary=require('eslint-plugin-testing-library');module.exports=[{files:[/* glob matching your test files */],...testingLibrary.configs['flat/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-eventsEnforce promises from async event methods are handledbadge-angularbadge-dombadge-markobadge-reactbadge-sveltebadge-vue🔧
await-async-queriesEnforce promises from async queries to be handledbadge-angularbadge-dombadge-markobadge-reactbadge-sveltebadge-vue🔧
await-async-utilsEnforce promises from async utils to be awaited properlybadge-angularbadge-dombadge-markobadge-reactbadge-sveltebadge-vue🔧
consistent-data-testidEnsures consistent usage ofdata-testid
no-await-sync-eventsDisallow unnecessaryawait for sync eventsbadge-angularbadge-dombadge-react
no-await-sync-queriesDisallow unnecessaryawait for sync queriesbadge-angularbadge-dombadge-markobadge-reactbadge-sveltebadge-vue🔧
no-containerDisallow the use ofcontainer methodsbadge-angularbadge-markobadge-reactbadge-sveltebadge-vue
no-debugging-utilsDisallow the use of debugging utilities likedebugbadge-angularbadge-markobadge-reactbadge-sveltebadge-vue
no-dom-importDisallow importing from DOM Testing Librarybadge-angularbadge-markobadge-reactbadge-sveltebadge-vue🔧
no-global-regexp-flag-in-queryDisallow the use of the global RegExp flag (/g) in queriesbadge-angularbadge-dombadge-markobadge-reactbadge-sveltebadge-vue🔧
no-manual-cleanupDisallow the use ofcleanupbadge-reactbadge-sveltebadge-vue
no-node-accessDisallow direct Node accessbadge-angularbadge-dombadge-markobadge-reactbadge-sveltebadge-vue
no-promise-in-fire-eventDisallow the use of promises passed to afireEvent methodbadge-angularbadge-dombadge-markobadge-reactbadge-sveltebadge-vue
no-render-in-lifecycleDisallow the use ofrender in testing frameworks setup functionsbadge-angularbadge-markobadge-reactbadge-sveltebadge-vue
no-test-id-queriesEnsure nodata-testid queries are used
no-unnecessary-actDisallow wrapping Testing Library utils or empty callbacks inactbadge-markobadge-react
no-wait-for-multiple-assertionsDisallow the use of multipleexpect calls insidewaitForbadge-angularbadge-dombadge-markobadge-reactbadge-sveltebadge-vue🔧
no-wait-for-side-effectsDisallow the use of side effects inwaitForbadge-angularbadge-dombadge-markobadge-reactbadge-sveltebadge-vue🔧
no-wait-for-snapshotEnsures no snapshot is generated inside of awaitFor callbadge-angularbadge-dombadge-markobadge-reactbadge-sveltebadge-vue
prefer-explicit-assertSuggest using explicit assertions rather than standalone queries
prefer-find-bySuggest usingfind(All)By* query instead ofwaitFor +get(All)By* to wait for elementsbadge-angularbadge-dombadge-markobadge-reactbadge-sveltebadge-vue🔧
prefer-implicit-assertSuggest using implicit assertions for getBy* & findBy* queries
prefer-presence-queriesEnsure appropriateget*/query* queries are used with their respective matchersbadge-angularbadge-dombadge-markobadge-reactbadge-sveltebadge-vue🔧
prefer-query-by-disappearanceSuggest usingqueryBy* queries when waiting for disappearancebadge-angularbadge-dombadge-markobadge-reactbadge-sveltebadge-vue
prefer-query-matchersEnsure the configuredget*/query* query is used with the corresponding matchers
prefer-screen-queriesSuggest usingscreen while queryingbadge-angularbadge-dombadge-markobadge-reactbadge-sveltebadge-vue
prefer-user-eventSuggest usinguserEvent overfireEvent for simulating user interactions
render-result-naming-conventionEnforce a valid naming for return value fromrenderbadge-angularbadge-markobadge-reactbadge-sveltebadge-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 canread more about this feature here.

If you are looking to restricting or switching off this feature, please refer to theShared 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 usingESLint 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 aboutthe advantages ofeslint-plugin-testing-library Aggressive Reporting feature, andhow 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 toAggressive Imports Reporting.

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

You can find more details about theutils-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 toAggressive Renders Reporting.

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

You can find more details about thecustom-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 toAggressive Reporting - Queries

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

You can find more details about thecustom-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.jsmodule.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 toeslint-plugin-testing-library in files other than testing, this could be caused byAggressive Reporting.

You can avoid this by:

  1. runningeslint-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, pleasefill 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 byAggressive 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, pleasefill 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

💻⚠️
justintoman
justintoman

💻⚠️
Anthony Devick
Anthony Devick

💻⚠️📖
Richard Maisano
Richard Maisano

💻⚠️
Aleksei Androsov
Aleksei Androsov

💻⚠️
Nicolas Bonduel
Nicolas Bonduel

📖
Alexey Ryabov
Alexey Ryabov

🚧
Jemi Salo
Jemi Salo

💻⚠️
nostro
nostro

💻
Daniel Rentz
Daniel Rentz

📖
StyleShit
StyleShit

💻⚠️📖
Yukihiro Hasegawa
Yukihiro Hasegawa

💻⚠️🤔
Charley Pugmire
Charley Pugmire

💻⚠️
Andrew Kazakov
Andrew Kazakov

💻
Kevin Bon
Kevin Bon

💻⚠️

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


[8]ページ先頭

©2009-2025 Movatter.jp