Skip to content

fix: Aggressive Reporting support in resolveToTestingLibraryFn #1043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions lib/utils/resolve-to-testing-library-fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,12 @@ export const resolveToTestingLibraryFn = <
}

const customModuleSetting = context.settings['testing-library/utils-module'];
if (
[...LIBRARY_MODULES, USER_EVENT_MODULE, customModuleSetting].some(
(module) => module === maybeImport.source
)
) {
const hasImportModuleMatch =
[...LIBRARY_MODULES, USER_EVENT_MODULE].includes(maybeImport.source) ||
(typeof customModuleSetting === 'string' &&
maybeImport.source.endsWith(customModuleSetting));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are several utils related to detect Testing Library modules in lib/create-testing-library-rule/detect-testing-library-utils.ts, perhaps we can reuse something here?

If not, we probably need to combine that module with this one at some point.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right.
Taking this opportunity, I implemented a function(isTestingLibraryModule)to detect Testing Library modules.


if (hasImportModuleMatch) {
return {
original: maybeImport.imported,
local: maybeImport.local,
Expand Down
10 changes: 10 additions & 0 deletions tests/lib/rules/no-node-access.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,16 @@ ruleTester.run(RULE_NAME, rule, {

const buttonText = screen.getByText('submit');
fe.click(buttonText);
`,
},
{
settings: { 'testing-library/utils-module': 'test-utils' },
code: `
// case: custom module set but not imported using ${testingFramework} (aggressive reporting limited)
import { screen, fireEvent } from '../test-utils';

const buttonText = screen.getByText('submit');
fireEvent.click(buttonText);
`,
},
{
Expand Down
47 changes: 38 additions & 9 deletions tests/lib/utils/resolve-to-testing-library-fn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,6 @@ ruleTester.run('esm', rule, {
userEvent.default.setup()
`,
},
{
// Verifies that a local './test-utils' import doesn't match the configured 'test-utils' utils module
settings: { 'testing-library/utils-module': 'test-utils' },
code: `
import { userEvent } from './test-utils';

userEvent.setup()
`,
},
...LIBRARY_MODULES.map((module) => ({
code: `
import * as testingLibrary from '${module}';
Expand Down Expand Up @@ -172,6 +163,44 @@ ruleTester.run('esm', rule, {
},
],
},
{
settings: { 'testing-library/utils-module': 'test-utils' },
code: `
import userEvent from '../test-utils';

userEvent.setup()
`,
errors: [
{
messageId: 'details',
data: {
data: {
original: null,
local: 'userEvent',
},
},
},
],
},
{
settings: { 'testing-library/utils-module': 'test-utils' },
code: `
import userEvent from '@/test-utils';

userEvent.setup()
`,
errors: [
{
messageId: 'details',
data: {
data: {
original: null,
local: 'userEvent',
},
},
},
],
},
{
settings: {
'testing-library/custom-renders': ['customRender', 'renderWithRedux'],
Expand Down