-
Notifications
You must be signed in to change notification settings - Fork 151
Open
Open
Copy link
Labels
bugSomething isn't workingSomething isn't working
Description
Have you read the Troubleshooting section?
Yes
Plugin version
v7.5.4
ESLint version
v9.30.1
Node.js version
v22.16.0
Bug description
We're using a helper function to render components in our tests. This helper also calls userEvent.setup
and returns the result as a value called user
. Since v7.5.4 (issue also present in v7.6.0) the plugin reports usages of this user
object in tests as errors under the testing-library/no-node-access
rule, e.g. the following is now an error:
const { user } = renderComponent(<TestComponent />);
await user.click(screen.getByRole('button', { name: 'Click me' }));
Steps to reproduce
Issue only happens when the helper function is imported from another file.
TestUtils.tsx
:
import { render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
export function renderComponent(component: React.ReactElement<any>) {
const user = userEvent.setup({ advanceTimers: jest.advanceTimersByTimeAsync });
const { container, rerender, unmount } = render(component);
return { user, container, rerender, unmount };
}
Component.test.tsx
:
import { screen } from '@testing-library/react';
import { useState } from 'react';
import { renderComponent } from './TestUtils';
describe('Component', () => {
function TestComponent() {
const [counter, setCounter] = useState(0);
return (
<div>
<div data-testid="counter">{counter}</div>
<button onClick={() => setCounter((c) => c + 1)}>Click me</button>
</div>
);
}
it('increments counter on click', async () => {
const { user } = renderComponent(<TestComponent />);
// error: Avoid direct Node access. Prefer using the methods from Testing Library testing-library/no-node-access
await user.click(screen.getByRole('button', { name: 'Click me' }));
expect(screen.getByTestId('counter')).toHaveTextContent('1');
});
});
Error output/screenshots
No response
ESLint configuration
import eslint from '@eslint/js';
import eslintPluginTestingLibrary from 'eslint-plugin-testing-library';
import tseslint from 'typescript-eslint';
export default tseslint.config(
{
name: 'Base eslint config',
files: ['**/*.ts', '**/*.tsx', '**/*.js', '**/*.mjs'],
extends: [eslint.configs.recommended, ...tseslint.configs.recommended, ...tseslint.configs.stylistic],
settings: {
react: {
version: 'detect',
},
},
languageOptions: {
ecmaVersion: 6,
parserOptions: {
warnOnUnsupportedTypeScriptVersion: true,
},
},
rules: {},
},
{
name: 'frontend Tests',
files: ['frontend/**/*.test.tsx', 'frontend/**/*.test.ts'],
plugins: {
'testing-library': eslintPluginTestingLibrary,
},
extends: [eslintPluginTestingLibrary.configs['flat/react']],
}
);
Rule(s) affected
testing-library/no-node-access
Anything else?
No response
Do you want to submit a pull request to fix this bug?
No
Simek, jimmy-guzman, IkumaTadokoro and Xelfie
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working