A modal test fails because the focus trap finds nothing to focus
Your focus-trap must have at least one container with at least one tabbable node in it at all timesShort answer
focus-trap throws when its container has no element it considers tabbable. In Jest the DOM is jsdom, which has no layout, so tabbable's visibility check can reject every element. Set tabbableOptions.displayCheck to 'none' for tests, give the trap a fallbackFocus element, and make sure the content is rendered before the trap activates.
Fix it fast
1Find out which cause you have
Find out which modal or dialog the failing test renders, and check whether it contains a button, link or input. A container with none will always throw, in any browser.
npx jest path/to/failing.test.js --runInBand2Match what you see, then jump to its fix
- 1
The component works in a real browser and fails only under Jest.jsdom cannot answer tabbable's visibility checkFix - 2
The dialog content appears after a fetch, a state update or an animation, and the error fires on open.The trap is active while its content is empty or not yet renderedFix - 3
A dialog that only shows text, or whose buttons are disabled, throws as soon as it opens.The container has no focusable element and no fallbackFocusFix
- 1
What does the error look like?
Your focus-trap must have at least one container with at least one tabbable node in it at all times
Cause 1 of 3
jsdom cannot answer tabbable's visibility check
1Check
The libraries state that JSDom is not officially supported. Tabbable's default display check uses DOM APIs such as getClientRects(), which JSDom does not fully implement, so elements a user would see can be judged hidden.
grep -rn "displayCheck\|tabbableOptions" src/ | head2Fix
For test runs, configure the trap with tabbableOptions.displayCheck set to 'none'. If a wrapper library creates the trap for you, pass the option through the wrapper's own options prop (see its docs).
import { createFocusTrap } from 'focus-trap';
const trap = createFocusTrap(element, {
tabbableOptions: { displayCheck: process.env.NODE_ENV === 'test' ? 'none' : 'full' },
});3Verify
Run the test again; the error is gone.
npx jest path/to/failing.test.jsConfirmed by github.com
Cause 2 of 3
The trap is active while its content is empty or not yet rendered
1Check
The library throws when the trap's containers hold no tabbable elements, which often happens when the traps are related to elements that appear and disappear dynamically.
grep -rn "activate\|focusTrap" src/ | head2Fix
Activate the trap only after the content is in the DOM. In the test, wait for the dialog's first button or input to appear before asserting.
import { screen } from '@testing-library/react';
await screen.findByRole('button', { name: /close/i });3Verify
The test passes without a timing workaround such as a fixed delay.
npx jest path/to/failing.test.jsConfirmed by github.com
Cause 3 of 3
The container has no focusable element and no fallbackFocus
1Check
Without fallbackFocus, an error is thrown if the trap contains no elements in its tab order.
grep -rn "fallbackFocus" src/ | head2Fix
Give the dialog container a negative tabindex and name it as fallbackFocus, so focus has somewhere valid to go.
// <div id="dialog" tabindex="-1"> ... </div>
const trap = createFocusTrap('#dialog', { fallbackFocus: '#dialog' });3Verify
Open the text-only dialog in the test.
npx jest path/to/failing.test.jsConfirmed by github.com
How do I stop it coming back?
- Keep one helper that builds trap options, so the test-only displayCheck setting lives in one place.
- Give every dialog at least one real control, such as a close button.
- Test focus behaviour in a real browser (Playwright or Cypress) as well, since jsdom is not officially supported.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Back end
Jest cannot find the jsdom test environment after an upgrade
Config line in play: testEnvironment: 'jsdom'
Since Jest 28 the jsdom environment is a separate package, so a config that says testEnvironment jsdom fails until you install jest-environment-jsdom as a dev dependency.
3 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fix