Skip to content

docs(ct): add component testing best practices section - #39780

Closed
Matt Van Horn (mvanhorn) wants to merge 1 commit into
microsoft:mainfrom
mvanhorn:osc/31361-docs-ct-best-practices
Closed

Matt Van Horn (mvanhorn) wants to merge 1 commit into
microsoft:mainfrom
mvanhorn:osc/31361-docs-ct-best-practices

Conversation

@mvanhorn

Copy link
Copy Markdown
Contributor

Summary

  • Add a "Best Practices" section to the component testing docs
  • Covers all 8 topics from [Docs]: Component testing "best practices" or "pitfalls" #31361: test scope, implementation details, test naming, locator usage, emulation over mocking, flat test structure, test isolation, and verifying test failure
  • Placed after the FAQ section at the end of the page

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).

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

@sand4rt Sander (sand4rt) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


### 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


### 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`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you dont use TDD right?

@pavelfeldman

Copy link
Copy Markdown
Member

Looks like something agents are already aware of!

@mvanhorn

Copy link
Copy Markdown
Contributor Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Docs]: Component testing "best practices" or "pitfalls"

3 participants