From 583ac8f5aba8840718dd99706b32037ae9b8402d Mon Sep 17 00:00:00 2001
From: Marin Atanasov <8436925+tyxla@users.noreply.github.com>
Date: Mon, 17 Oct 2022 13:24:09 +0200
Subject: [PATCH] ESLint: Add and enable `eslint-plugin-jest-dom` (#44983)
* ESLint: Enable eslint-plugin-jest-dom
* Fix a couple of false positives
---
.eslintrc.js | 5 +++++
package-lock.json | 22 +++++++++++++++++++
package.json | 1 +
.../components/src/ui/tooltip/test/index.js | 14 ++++++------
4 files changed, 35 insertions(+), 7 deletions(-)
diff --git a/.eslintrc.js b/.eslintrc.js
index 35e98799ec7c5c..87a98f5429d10b 100644
--- a/.eslintrc.js
+++ b/.eslintrc.js
@@ -320,6 +320,11 @@ module.exports = {
excludedFiles: [ 'test/e2e/**/*.js' ],
extends: [ 'plugin:@wordpress/eslint-plugin/test-unit' ],
},
+ {
+ files: [ '**/test/**/*.js' ],
+ excludedFiles: [ '**/*.@(android|ios|native).js' ],
+ extends: [ 'plugin:jest-dom/recommended' ],
+ },
{
files: [ 'packages/e2e-test*/**/*.js' ],
excludedFiles: [ 'packages/e2e-test-utils-playwright/**/*.js' ],
diff --git a/package-lock.json b/package-lock.json
index 43701791134cd9..189b4392c65292 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -33453,6 +33453,28 @@
"@typescript-eslint/experimental-utils": "^5.0.0"
}
},
+ "eslint-plugin-jest-dom": {
+ "version": "4.0.2",
+ "resolved": "https://registry.npmjs.org/eslint-plugin-jest-dom/-/eslint-plugin-jest-dom-4.0.2.tgz",
+ "integrity": "sha512-Jo51Atwyo2TdcUncjmU+UQeSTKh3sc2LF/M5i/R3nTU0Djw9V65KGJisdm/RtuKhy2KH/r7eQ1n6kwYFPNdHlA==",
+ "dev": true,
+ "requires": {
+ "@babel/runtime": "^7.16.3",
+ "@testing-library/dom": "^8.11.1",
+ "requireindex": "^1.2.0"
+ },
+ "dependencies": {
+ "@babel/runtime": {
+ "version": "7.19.4",
+ "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.19.4.tgz",
+ "integrity": "sha512-EXpLCrk55f+cYqmHsSR+yD/0gAIMxxA9QK9lnQWzhMCvt+YmoBN7Zx94s++Kv0+unHk39vxNO8t+CMA2WSS3wA==",
+ "dev": true,
+ "requires": {
+ "regenerator-runtime": "^0.13.4"
+ }
+ }
+ }
+ },
"eslint-plugin-jsdoc": {
"version": "37.0.3",
"resolved": "https://registry.npmjs.org/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-37.0.3.tgz",
diff --git a/package.json b/package.json
index c2dea5555d1707..e488f504b58365 100755
--- a/package.json
+++ b/package.json
@@ -185,6 +185,7 @@
"eslint-import-resolver-node": "0.3.4",
"eslint-plugin-eslint-comments": "3.1.2",
"eslint-plugin-import": "2.25.2",
+ "eslint-plugin-jest-dom": "4.0.2",
"eslint-plugin-playwright": "0.8.0",
"eslint-plugin-ssr-friendly": "1.0.6",
"execa": "4.0.2",
diff --git a/packages/components/src/ui/tooltip/test/index.js b/packages/components/src/ui/tooltip/test/index.js
index 9251fd476ba99a..0a13319b999256 100644
--- a/packages/components/src/ui/tooltip/test/index.js
+++ b/packages/components/src/ui/tooltip/test/index.js
@@ -37,14 +37,14 @@ describe( 'props', () => {
{ invisibleTooltipTriggerContent }
);
- const tooltips = screen.getAllByRole( /tooltip/i );
+ const tooltip = screen.getByRole( /tooltip/i );
const invisibleTooltipTrigger = screen.getByText(
invisibleTooltipTriggerContent
);
- // The invisible tooltip should not render.
- expect( tooltips ).toHaveLength( 1 );
+ // The base tooltip should render only; invisible tooltip should not render.
+ expect( tooltip ).toBeInTheDocument();
// Assert that the rendered tooltip is indeed the base tooltip.
- expect( tooltips[ 0 ].id ).toBe( baseTooltipId );
+ expect( tooltip.id ).toBe( baseTooltipId );
// But the invisible tooltip's trigger still should have rendered.
expect( invisibleTooltipTrigger ).not.toBeUndefined();
} );
@@ -70,9 +70,9 @@ describe( 'props', () => {
WordPress.org
);
- const tooltips = screen.getAllByRole( /tooltip/i );
+ const tooltip = screen.getByRole( /tooltip/i );
// Assert only the base tooltip rendered.
- expect( tooltips ).toHaveLength( 1 );
- expect( tooltips[ 0 ].id ).toBe( baseTooltipId );
+ expect( tooltip ).toBeInTheDocument();
+ expect( tooltip.id ).toBe( baseTooltipId );
} );
} );