Describe the bug
The prefer-presence-queries autofix (introduced in v7.4.0, #1020) only rewrites the Identifier at the call site inside expect(), but does not update the corresponding ObjectPattern destructure binding. This produces syntactically valid but broken code — getByRole is called but was never declared, causing a ReferenceError at runtime.
Minimal reproducible example
// broken.test.js — BEFORE eslint --fix
it('example', () => {
const { queryByRole } = render(something());
expect(queryByRole('alert')).toBeInTheDocument(); // ← fires prefer-presence-queries
});
// broken.test.js — AFTER eslint --fix
it('example', () => {
const { queryByRole } = render(something()); // ← destructure untouched, now unused
expect(getByRole('alert')).toBeInTheDocument(); // ← getByRole is NOT IN SCOPE → ReferenceError
});
Steps to reproduce
mkdir repro && cd repro
npm init -y
npm install eslint eslint-plugin-testing-library
// eslint.config.mjs
import testingLibrary from 'eslint-plugin-testing-library';
export default [{
files: ['**/*.test.js'],
plugins: { 'testing-library': testingLibrary },
rules: { 'testing-library/prefer-presence-queries': 'error' },
}];
// broken.test.js
it('example', () => {
const { queryByRole } = render(something());
expect(queryByRole('alert')).toBeInTheDocument();
});
npx eslint --fix broken.test.js
cat broken.test.js
Output after fix:
it('example', () => {
const { queryByRole } = render(something()); // ← not updated
expect(getByRole('alert')).toBeInTheDocument(); // ← not in scope → ReferenceError at runtime
});
Expected behavior
The fix should update both the destructure binding and the call site:
it('example', () => {
const { getByRole } = render(something()); // ← updated
expect(getByRole('alert')).toBeInTheDocument(); // ← correct
});
Versions
Root cause
The fixer targets the Identifier node matched by the "CallExpression Identifier" selector — the identifier at the call site inside expect(). It does not walk up the scope to find and update the ObjectPattern in the VariableDeclarator that originally destructured the same query from the render result.
Related
Describe the bug
The
prefer-presence-queriesautofix (introduced in v7.4.0, #1020) only rewrites theIdentifierat the call site insideexpect(), but does not update the correspondingObjectPatterndestructure binding. This produces syntactically valid but broken code —getByRoleis called but was never declared, causing aReferenceErrorat runtime.Minimal reproducible example
Steps to reproduce
Output after fix:
Expected behavior
The fix should update both the destructure binding and the call site:
Versions
eslint-plugin-testing-library: 7.16.2 (first appeared in 7.4.0 when autofix was added via feat(prefer-presence-queries): Add autofix support #1020)eslint: 9.xRoot cause
The fixer targets the
Identifiernode matched by the"CallExpression Identifier"selector — the identifier at the call site insideexpect(). It does not walk up the scope to find and update theObjectPatternin theVariableDeclaratorthat originally destructured the same query from the render result.Related