Best practices when using userEvent and asynchronous queries and assertions in interaction testing, e.g. await expect, canvas.findBy, await waitFor() with Storybook Test Runner (not Vitest addon)
#33195
Replies: 2 comments 5 replies
-
|
Here are the current best practices for async queries, assertions, and userEvent usage in Storybook Test Runner play functions: Async assertions and queries:
userEvent usage:
Your code examples:
Example pattern: import { expect, userEvent } from 'storybook/test';
export const Example = {
play: async ({ canvas }) => {
await userEvent.click(await canvas.findByRole('button', { name: 'Open dialog' }));
const dialog = await canvas.findByRole('dialog');
await expect(dialog).toBeVisible();
},
};This approach matches official Storybook templates and documentation [example]. You’re set to use either import style for To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
-
|
For this part:
I guess that means that the Storybook I think I've having trouble distinguishing between the Vitest browser mode |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Some background:
jest-image-snapshotwhich still works with Storybook Test Runner like in this blog and Storybook v8 docs)userEventin myplayfunctions.Async queries and assertions in Vitest/Jest non-browser mode versus
playfunctions in Storybookhttps://testing-library.com/docs/dom-testing-library/api-async/ has some good documentation about when to use
findByqueries andawait waitForfor more synchronous checks which I'm used to following when writing unit tests in Vitest/Jest (non-browser mode).Now that I'm starting to write interaction tests in Storybook through the
playfunction, I see thatexpectfromstorybook/testis asynchronous, so it's less clear now about what we can / can't / shouldn't do.For example, I knew in Jest / Vitest (non-browser mode) something like the following would never actually "wait" for something to appear because
expectis synchronous:This was particularly relevant for an assertion like
toBeVisible()which does a lot more thantoBeInTheDocument().What is the correct way to use
await expectandawait waitFornow, particularly withtoBeVisible()assertions?Where should we import
userEventfrom?There are conflicting examples about getting
userEventfrom theplayfunction versus importing it directly fromstorybook/test.Using the example from https://storybook.js.org/docs/writing-tests/interaction-testing#writing-interaction-tests:
For userEvent, is there a functional difference between that and importing it from
storybook/test, e.g.:The second one seems to work for me in my testing, and I favour it a bit more because it's more like imports from
@testing-library/reactthat I'm used to in unit tests.Thanks!
Beta Was this translation helpful? Give feedback.
All reactions