Skip to content
\n

Additional context

\n

No response

","upvoteCount":1,"answerCount":4,"acceptedAnswer":{"@type":"Answer","text":"

You are correct. The issue you're encountering stems from the difference between synchronous and asynchronous operations in your Storybook play function, especially when running in a CI environment like Chromatic which can have different rendering timings than your local machine.

\n

The error message Unable to find an accessible element with the role \"button\" and name /back/i occurs because you are using getByRole. This is a synchronous query from the Testing Library. It expects the \"back\" button to be present in the DOM the instant the code executes. When the component takes a fraction of a second longer to render in Chromatic, the test fails because the button isn't there yet.

\n

The correct solution, as you pointed out, is to use an asynchronous query.

\n

The Solution: Use findByRole

\n

Instead of getByRole, you should use findByRole. The findBy... queries are asynchronous and will wait for the element to appear in the DOM, retrying for a brief period before timing out. This makes your tests resilient to variations in rendering speed.

\n

You need to await the result of the findByRole call.

\n

Here is how you should adjust your play function:

\n

Incorrect Code (Current):

\n
// This can fail if the button isn't rendered immediately\nconst backButton = within(canvasElement).getByRole(\"button\", { name: /back/i });\nawait userEvent.click(backButton);
\n

Correct Code (Proposed):

\n
import { within, userEvent } from '@storybook/test';\n\n// ... inside your play function\n\n// The 'await' pauses execution until the button is found or the query times out.\nconst backButton = await within(canvasElement).findByRole(\"button\", { name: /back/i });\nawait userEvent.click(backButton);
\n

By making this change, your test will now patiently wait for the button to become available before attempting to interact with it, resolving the inconsistency between your local and Chromatic test runs.

","upvoteCount":1,"url":"https://github.com/storybookjs/storybook/discussions/32010#discussioncomment-13724327"}}}

[Bug]: Inconsistence between local storybook test vs chromatic #32010

Discussion options

You must be logged in to vote

You are correct. The issue you're encountering stems from the difference between synchronous and asynchronous operations in your Storybook play function, especially when running in a CI environment like Chromatic which can have different rendering timings than your local machine.

The error message Unable to find an accessible element with the role "button" and name /back/i occurs because you are using getByRole. This is a synchronous query from the Testing Library. It expects the "back" button to be present in the DOM the instant the code executes. When the component takes a fraction of a second longer to render in Chromatic, the test fails because the button isn't there yet.

The correct …

Replies: 4 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by valentinpalkovic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
2 participants
Converted from issue

This discussion was converted from issue #32002 on July 10, 2025 19:00.