7.1.1 • Published 3 months ago

eslint-plugin-testing-library v7.1.1

Weekly downloads
1,275,630
License
MIT
Repository
github
Last release
3 months ago

Package version eslint-remote-tester eslint-plugin-testing-library codecov MIT License semantic-release PRs Welcome All Contributors

Prerequisites

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

Installation

You'll first need to install ESLint.

Next, install eslint-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 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

!NOTE

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

Refer to the ESLint documentation on the new configuration file format for 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 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'],
};

To enable this configuration with eslint.config.js, use testingLibrary.configs['flat/dom']:

const testingLibrary = 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 the extends property in your .eslintrc.js config file:

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

To enable this configuration with eslint.config.js, use testingLibrary.configs['flat/angular']:

const testingLibrary = 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 the extends property in your .eslintrc.js config file:

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

To enable this configuration with eslint.config.js, use testingLibrary.configs['flat/react']:

const testingLibrary = 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 the extends property in your .eslintrc.js config file:

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

To enable this configuration with eslint.config.js, use testingLibrary.configs['flat/vue']:

const testingLibrary = 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 the extends property in your .eslintrc.js config file:

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

To enable this configuration with eslint.config.js, use testingLibrary.configs['flat/svelte']:

const testingLibrary = 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 the extends property in your .eslintrc.js config file:

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

To enable this configuration with eslint.config.js, use testingLibrary.configs['flat/marko']:

const testingLibrary = 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-angular badge-dom badge-marko badge-react badge-svelte badge-vue🔧
await-async-queriesEnforce promises from async queries to be handledbadge-angular badge-dom badge-marko badge-react badge-svelte badge-vue
await-async-utilsEnforce promises from async utils to be awaited properlybadge-angular badge-dom badge-marko badge-react badge-svelte badge-vue
consistent-data-testidEnsures consistent usage of data-testid
no-await-sync-eventsDisallow unnecessary await for sync eventsbadge-angular badge-dom badge-react
no-await-sync-queriesDisallow unnecessary await for sync queriesbadge-angular badge-dom badge-marko badge-react badge-svelte badge-vue
no-containerDisallow the use of container methodsbadge-angular badge-marko badge-react badge-svelte badge-vue
no-debugging-utilsDisallow the use of debugging utilities like debugbadge-angular badge-marko badge-react badge-svelte badge-vue
no-dom-importDisallow importing from DOM Testing Librarybadge-angular badge-marko badge-react badge-svelte badge-vue🔧
no-global-regexp-flag-in-queryDisallow the use of the global RegExp flag (/g) in queriesbadge-angular badge-dom badge-marko badge-react badge-svelte badge-vue🔧
no-manual-cleanupDisallow the use of cleanupbadge-react badge-svelte badge-vue
no-node-accessDisallow direct Node accessbadge-angular badge-dom badge-marko badge-react badge-svelte badge-vue
no-promise-in-fire-eventDisallow the use of promises passed to a fireEvent methodbadge-angular badge-dom badge-marko badge-react badge-svelte badge-vue
no-render-in-lifecycleDisallow the use of render in testing frameworks setup functionsbadge-angular badge-marko badge-react badge-svelte badge-vue
no-unnecessary-actDisallow wrapping Testing Library utils or empty callbacks in actbadge-marko badge-react
no-wait-for-multiple-assertionsDisallow the use of multiple expect calls inside waitForbadge-angular badge-dom badge-marko badge-react badge-svelte badge-vue
no-wait-for-side-effectsDisallow the use of side effects in waitForbadge-angular badge-dom badge-marko badge-react badge-svelte badge-vue
no-wait-for-snapshotEnsures no snapshot is generated inside of a waitFor callbadge-angular badge-dom badge-marko badge-react badge-svelte badge-vue
prefer-explicit-assertSuggest using explicit assertions rather than standalone queries
prefer-find-bySuggest using find(All)By* query instead of waitFor + get(All)By* to wait for elementsbadge-angular badge-dom badge-marko badge-react badge-svelte badge-vue🔧
prefer-implicit-assertSuggest using implicit assertions for getBy & findBy queries
prefer-presence-queriesEnsure appropriate get*/query* queries are used with their respective matchersbadge-angular badge-dom badge-marko badge-react badge-svelte badge-vue
prefer-query-by-disappearanceSuggest using queryBy* queries when waiting for disappearancebadge-angular badge-dom badge-marko badge-react badge-svelte badge-vue
prefer-query-matchersEnsure the configured get*/query* query is used with the corresponding matchers
prefer-screen-queriesSuggest using screen while queryingbadge-angular badge-dom badge-marko badge-react badge-svelte badge-vue
prefer-user-eventSuggest using userEvent over fireEvent for simulating user interactions
render-result-naming-conventionEnforce a valid naming for return value from renderbadge-angular badge-marko badge-react badge-svelte 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):

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

eslint-config-react-appbyteplayerkstick-react-scriptsluohe-react-scriptsluohe-react-tplreact-scripts-changedreact-scripts-ossreact-scripts-os2dadachartaboutbits-react-scriptscustom-cracra-anjandps-scriptsdps2cules-crabadabam-react-scripts-spectacleserver-selector1tt-flowchart-buildersamueltrahan-react-scriptsrn-shared-liaogjcircular-progressbar-reactkishorelabs-react-scriptsturbine-one-react-scripts@v-n-m/react-scriptsv-n-m-react-scriptsux-inboxgron-ui@deck-engineering/eslint-config@anatolzak/react-custom-scriptsreact-mxgraph-editoreslint-config-refined@martynastell/build-configs@martynastell/eslint-configcinstabsguming-shop-modalkd-ui-libskdui-libsimmo-statistic@swallowtail/eslint-config@jkyu/monet-clitest-widget-orderwp5-react-scripts@stackshirts/react-scriptsrete-two-editorform-test-reactform-test-shv@samarmohan/eslint-config-react-typescriptreact-scripts-jackreact-scripts-arteslint-config-remedy@webiocomponents/webio-componentsadalnamskra-islanduitq-richeditor-testtq.richeditor.module@evoclass/evoui@jean9696/eslint-config-custom@oneui/react-scripts@julienpapini/react-scriptsbankos-loan-formeslint-config-typicalluohe-r-tpl@rocklab/eslint-config-rocklabreact-scripts-oseslint-plugin-ultimate-typescript-configeslint-config-imprint@raull/eslint-config-react-app@samarmohan/eslint-configwindego-react-scripts@kiva/ssirius-standalone@memoryai/frontend-config@alfabank/eslint-config-sitereact-scripts-heisenbergeslint-config-martyncherrynickelodeon-ips-themesautoketing-test-scriptseslint-config-rboweripaiva-scripts@martyncherry/eslint-config-tseject-react-testingeslint-config-superchargeeslint-config-ultimate-typescriptoverwolf-react-scriptseparts-shared-uioh-custom-react-scripts@assemble-inc/eslint-config-assemble@infinitebrahmanuniverse/nolb-eslint-plugin-t@jarisinc/eslint-config-jaris@soyhuce/eslint-config-jest@whims/eslint-configeslint-config-sandboxfast-my-react-componentssklif-ui-kitsklif-api@everything-registry/sub-chunk-1614eslint-config-wishket@tribufu/electron-scripts@tribufu/react-scripts@indonesiafurs/style-guide@ventsislavnikolov/eslint-config@finale-lua/eslint-config
7.1.1

3 months ago

7.1.0

4 months ago

6.5.0

4 months ago

7.0.0

4 months ago

7.0.0-beta.6

4 months ago

7.0.0-beta.4

5 months ago

7.0.0-beta.5

5 months ago

7.0.0-beta.2

5 months ago

7.0.0-beta.3

5 months ago

6.4.0

5 months ago

6.3.4

5 months ago

6.3.3

5 months ago

6.3.2

5 months ago

6.3.1

5 months ago

6.3.0

7 months ago

7.0.0-beta.1

11 months ago

6.2.2

11 months ago

6.2.1

11 months ago

6.1.2

1 year ago

6.1.1

1 year ago

6.2.0

1 year ago

6.1.0

1 year ago

6.0.2

1 year ago

6.0.1

2 years ago

6.0.0

2 years ago

5.11.1

2 years ago

6.0.0-alpha.15

2 years ago

5.11.0

2 years ago

5.10.3

2 years ago

5.10.2

2 years ago

5.10.1

2 years ago

5.10.0

2 years ago

6.0.0-alpha.9

2 years ago

5.8.0

2 years ago

5.9.1

2 years ago

5.9.0

2 years ago

6.0.0-alpha.10

2 years ago

6.0.0-alpha.11

2 years ago

6.0.0-alpha.12

2 years ago

6.0.0-alpha.13

2 years ago

6.0.0-alpha.14

2 years ago

5.7.3

2 years ago

6.0.0-alpha.3

2 years ago

6.0.0-alpha.4

2 years ago

6.0.0-alpha.5

2 years ago

6.0.0-alpha.6

2 years ago

6.0.0-alpha.7

2 years ago

6.0.0-alpha.8

2 years ago

6.0.0-alpha.1

2 years ago

6.0.0-alpha.2

2 years ago

5.6.4

3 years ago

5.6.3

3 years ago

5.6.2

3 years ago

5.6.1

3 years ago

5.7.2

2 years ago

5.7.1

2 years ago

5.7.0

2 years ago

5.5.1

3 years ago

5.6.0

3 years ago

5.4.0

3 years ago

5.5.0

3 years ago

5.3.1

3 years ago

5.3.0

3 years ago

5.2.1

3 years ago

5.2.0

3 years ago

5.0.6

3 years ago

5.1.0

3 years ago

5.0.5

3 years ago

5.0.4

3 years ago

5.0.3

3 years ago

5.0.2

3 years ago

5.0.1

3 years ago

5.0.0

3 years ago

5.0.0-alpha.7

3 years ago

5.0.0-alpha.6

3 years ago

5.0.0-alpha.5

3 years ago

5.0.0-alpha.4

3 years ago

5.0.0-alpha.9

3 years ago

5.0.0-alpha.8

3 years ago

5.0.0-alpha.3

3 years ago

5.0.0-alpha.10

3 years ago

4.12.4

3 years ago

4.12.3

3 years ago

4.12.2

4 years ago

5.0.0-alpha.2

4 years ago

5.0.0-alpha.1

4 years ago

4.12.1

4 years ago

4.12.0

4 years ago

4.11.0

4 years ago

4.9.3

4 years ago

4.9.2

4 years ago

4.10.1

4 years ago

4.10.0

4 years ago

4.9.1

4 years ago

4.9.0

4 years ago

4.8.0

4 years ago

4.7.0

4 years ago

4.4.0

4 years ago

4.6.0

4 years ago

4.2.1

4 years ago

4.2.0

4 years ago

4.5.0

4 years ago

4.5.1

4 years ago

4.3.0

4 years ago

4.1.2

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.0.2

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

4.0.0-beta.6

4 years ago

4.0.0-beta.5

4 years ago

4.0.0-beta.4

4 years ago

4.0.0-beta.3

4 years ago

4.0.0-beta.2

4 years ago

4.0.0-beta.1

4 years ago

4.0.0-alpha.2

4 years ago

3.10.2

4 years ago

4.0.0-alpha.1

4 years ago

4.0.0-alpha.0

4 years ago

3.10.1

4 years ago

3.10.0

4 years ago

3.9.2

4 years ago

3.9.1

4 years ago

3.9.0

4 years ago

3.8.0

5 years ago

3.7.0

5 years ago

3.6.0

5 years ago

3.5.0

5 years ago

3.4.1

5 years ago

3.4.0

5 years ago

3.3.2

5 years ago

3.3.1

5 years ago

3.3.0

5 years ago

3.2.3

5 years ago

3.2.2

5 years ago

3.2.1

5 years ago

3.2.0

5 years ago

3.1.4

5 years ago

3.1.3

5 years ago

3.1.2

5 years ago

3.1.1

5 years ago

3.1.0

5 years ago

3.1.0-beta.3

5 years ago

3.1.0-beta.1

5 years ago

3.1.0-beta.2

5 years ago

3.0.4

5 years ago

3.0.3

5 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.2.3

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.0

5 years ago

2.0.0

5 years ago

1.5.0

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.4

5 years ago

1.3.3

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.1

5 years ago

1.0.0

5 years ago