Repository navigation
docs(ct): add component testing best practices section - #39780
Matt Van Horn (mvanhorn) wants to merge 1 commit into
Conversation
Add a Best Practices section covering the 8 topics from issue microsoft#31361: test scope, implementation details, test naming, locator usage, emulation over mocking, flat test structure, test isolation, and verifying tests can fail. Fixes microsoft#31361
Sander (sand4rt)
left a comment
There was a problem hiding this comment.
I’m not sure whether the Playwright team still want this, but here are my thoughts:
|
|
||
| ### Avoid nesting and describe blocks | ||
|
|
||
| Flat test structure is easier to read and maintain. Each test should set up its own state through `mount` rather than relying on shared setup in nested `describe` blocks. If multiple tests need the same mounting logic, extract it into a helper function or use `test.beforeEach`. |
There was a problem hiding this comment.
|
|
||
| ### Use emulation over mocking | ||
|
|
||
| Component tests run in a real browser. Take advantage of this by using Playwright’s built-in emulation (viewport, locale, color scheme) and network interception instead of mocking browser APIs or framework internals. Use `hooksConfig` to inject dependencies like routers or stores via `beforeMount` hooks rather than patching modules. |
There was a problem hiding this comment.
Would explain why this is better then mocking features like: vi.fn, vi.mock or vi.spyOn
|
|
||
| ### Use the appropriate locator | ||
|
|
||
| Use semantic locators that reflect how users find elements: `getByRole`, `getByText`, `getByLabel`. Avoid CSS selectors or test IDs when a semantic locator is available. Scope locator queries to the component handle returned by `mount` rather than using `page` directly. |
There was a problem hiding this comment.
Would also describe the priority: https://testing-library.com/docs/queries/about#priority
|
|
||
| ### Name tests clearly | ||
|
|
||
| Write test names that describe the behavior being verified, not the method being called. A good test name reads like a specification: `increments counter when plus button is clicked` is better than `test click`. |
There was a problem hiding this comment.
Would also mention a few dont's: like not all test should start with the word should. You could make the first word plurel intead:
Bad:
test("should hide the popup on escape press")
Better:
test("hides the popup on escape press")
|
|
||
| ### Determine what to test | ||
|
|
||
| Focus on testing the component’s public contract: the props it accepts, the events it emits, and the DOM it renders. A good component test verifies that given specific inputs, the component produces the expected visible output and responds correctly to user interaction. |
There was a problem hiding this comment.
I've seen a lot of useless component tests that are only verifying that the props are being rendered without some type of logic in the component. Would mention some tests that are actually usefull.
|
|
||
| ### Ensure your test can fail | ||
|
|
||
| After writing a test, verify it catches real regressions. Temporarily change the component or assertion to confirm the test fails with a clear error message. A test that never fails provides no value. |
There was a problem hiding this comment.
If you dont use TDD right?
|
Looks like something agents are already aware of! |
|
Ha — fair point. 😄 If you'd be open to the content with the feedback from Sander (@sand4rt) addressed, I'm happy to revise. Otherwise, understood! |
Summary
The 8 topics come directly from the issue outline that Dmitry Gozman (@dgozman) endorsed as "a good starting point."
Fixes #31361
This contribution was developed with AI assistance (Claude Code + Codex).