Skip to content

Commit f698500

Browse files
committed
feat: save react-dom/test-utils import in detection mechanism
1 parent e49d5c5 commit f698500

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

lib/create-testing-library-rule/detect-testing-library-utils.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export interface DetectionHelpers {
124124
}
125125

126126
const USER_EVENT_PACKAGE = '@testing-library/user-event';
127+
const REACT_DOM_TEST_UTILS_PACKAGE = 'react-dom/test-utils';
127128
const FIRE_EVENT_NAME = 'fireEvent';
128129
const USER_EVENT_NAME = 'userEvent';
129130
const RENDER_NAME = 'render';
@@ -157,6 +158,7 @@ export function detectTestingLibraryUtils<
157158
let importedTestingLibraryNode: ImportModuleNode | null = null;
158159
let importedCustomModuleNode: ImportModuleNode | null = null;
159160
let importedUserEventLibraryNode: ImportModuleNode | null = null;
161+
let importedReactDomTestUtilsNode: ImportModuleNode | null = null;
160162

161163
// Init options based on shared ESLint settings
162164
const customModuleSetting =
@@ -889,11 +891,14 @@ export function detectTestingLibraryUtils<
889891
* parts of the file.
890892
*/
891893
ImportDeclaration(node: TSESTree.ImportDeclaration) {
894+
if (typeof node.source.value !== 'string') {
895+
return;
896+
}
892897
// check only if testing library import not found yet so we avoid
893898
// to override importedTestingLibraryNode after it's found
894899
if (
895900
!importedTestingLibraryNode &&
896-
/testing-library/g.test(node.source.value as string)
901+
/testing-library/g.test(node.source.value)
897902
) {
898903
importedTestingLibraryNode = node;
899904
}
@@ -904,7 +909,7 @@ export function detectTestingLibraryUtils<
904909
if (
905910
customModule &&
906911
!importedCustomModuleNode &&
907-
String(node.source.value).endsWith(customModule)
912+
node.source.value.endsWith(customModule)
908913
) {
909914
importedCustomModuleNode = node;
910915
}
@@ -913,10 +918,19 @@ export function detectTestingLibraryUtils<
913918
// to override importedUserEventLibraryNode after it's found
914919
if (
915920
!importedUserEventLibraryNode &&
916-
String(node.source.value) === USER_EVENT_PACKAGE
921+
node.source.value === USER_EVENT_PACKAGE
917922
) {
918923
importedUserEventLibraryNode = node;
919924
}
925+
926+
// check only if react-dom/test-utils import not found yet so we avoid
927+
// to override importedReactDomTestUtilsNode after it's found
928+
if (
929+
!importedUserEventLibraryNode &&
930+
node.source.value === REACT_DOM_TEST_UTILS_PACKAGE
931+
) {
932+
importedReactDomTestUtilsNode = node;
933+
}
920934
},
921935

922936
// Check if Testing Library related modules are loaded with required.

0 commit comments

Comments
 (0)