Skip to main content

SitecoreAI · Tutorial

Testing Sitecore Components: Fixing Flaky Storybook/Chromatic Assertions

Why `getByText` inside `waitFor` passes locally but fails in Chromatic — and the `findBy` fix.

GavAusWeb2 min read

A Storybook play function that passes every time on your machine can still fail reliably in Chromatic. The usual culprit looks harmless: query for an element with getByText, then wrap an assertion in waitFor.

Flakyjs
const heading = canvas.getByText(/Site maintenance planned/i);
await waitFor(() => expect(heading).toBeInTheDocument());

Why it fails in ChromaticLink to this section

The problem is the order of operations. getByText throws immediately if the element isn't in the DOM yet — it doesn't wait or retry. waitFor only retries the callback passed to it (the assertion), not the query that already ran before waitFor was called. So by the time waitFor starts, the test has already thrown and there's nothing left to retry.

Locally this goes unnoticed because rendering is effectively synchronous by the time the test runs, so the element already exists when getByText fires. In Chromatic, component rendering is asynchronous, so the element frequently isn't there on the first tick — and the test explodes before waitFor gets a chance to do anything.

The fix: findBy queriesLink to this section

Swap getBy* for findBy*. findBy* queries wait for the element to appear and retry automatically, which is exactly what async rendering needs — no separate waitFor required:

Stablejs
import { within } from '@storybook/testing-library';

play: async ({ canvasElement }) => {
  const canvas = within(canvasElement);

  const heading = await canvas.findByText(/Site maintenance planned/i);
  expect(heading).toBeInTheDocument();

  const description = await canvas.findByText(
    'We will be performing maintenance on Saturday.'
  );
  expect(description).toBeInTheDocument();
};

That version works locally, works in Chromatic, and doesn't need a manual waitFor around it at all.

When you need an element by attribute or class rather than text, wrap the DOM query itself in waitFor (not just the assertion) so the query keeps retrying until the node exists. For something like a link with a specific accessible name, findByRole handles both the waiting and the query in one call:

js
const banner = await waitFor(() =>
  canvasElement.querySelector('[data-slot="callout-banner"]')
);

const cta = await canvas.findByRole('link', { name: /primary cta/i });

Before you blame the codeLink to this section

  • When a site works in one environment but not locally, the fix is often mundane: re-pull the home item from Dev, rebuild the solution, and bounce the local Docker environment (down, then up).
  • JSS grid settings aren't always the Sitecore/Tailwind defaults. Projects sometimes duplicate the base Tailwind grid definition into their own Sitecore settings item so it can be customised per site — if a layout's grid behaves unexpectedly, check for a duplicated grid definition item overriding the base one before chasing it in CSS.