Storybook play function "expect(...).toBeInTheDocument is not a function" in CI
A play function used a DOM matcher like toBeInTheDocument, but the test-runner's expect does not have the jest-dom matchers unless you extend it. In CI, where nothing set them up implicitly, the matcher is undefined.
What this error means
An interaction test fails with "TypeError: expect(...).toBeInTheDocument is not a function" inside a story's play function.
test-runner
TypeError: expect(...).toBeInTheDocument is not a function
at play (Button.stories.tsx:22:41)Common causes
jest-dom matchers were never registered
The play function relies on @testing-library/jest-dom matchers, but nothing extended expect with them for the runner.
The setup ran locally but not in CI
A local editor or config extended expect implicitly, while the CI runner uses a bare configuration.
How to fix it
Extend expect in the test-runner config
- Create
.storybook/test-runner.ts(or.js). - Import the jest-dom matchers so
expectgains DOM assertions. - Re-run the runner.
.storybook/test-runner.ts
// .storybook/test-runner.ts
import '@testing-library/jest-dom';Install the matcher package
Ensure the jest-dom package is a dependency so CI can import it.
Terminal
npm install -D @testing-library/jest-domHow to prevent it
- Register jest-dom matchers in the test-runner config, not ad hoc.
- Keep
@testing-library/jest-dominpackage.json. - Run the test-runner locally with the same config CI uses.
Related guides
Storybook test-runner "A snapshot ... does not match" in CIFix @storybook/test-runner "A snapshot ... does not match its stored snapshot" in CI - a story's rendered out…
Storybook interaction test failure gating the pipeline in CIUnderstand a Storybook interaction test failing a play assertion in CI - the test-runner ran a story's play f…
Storybook test-runner "No tests found" in CIFix @storybook/test-runner "No tests found" in CI - the runner discovered no stories to test, usually a wrong…