Skip to content

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

Closed

Description

@sand4rt

Page(s)

Page: https://playwright.dev/docs/next/test-components#update

I think these topics would be useful to mention:

  • Determine what to test
  • Determine what not to test (implementation details)
  • How to name tests
  • Use the appropriate locator
  • Emphasise emulation over mocking
  • Avoid nesting / describe blocks
  • Avoid unnecessary abstractions (Test isolation)
  • Ensure your test can fail

Dmitry Gozman (@dgozman) do you have other topics/changes in mind?

Description

Suggestion from Dmitry Gozman (@dgozman) to include a "best practices" or "pitfalls" section for component testing: #31339 (comment). I've created a ticket to track this so I don't forget.

Activity

  1. dgozman commented on Jun 18, 2024

    @dgozman
    Collaborator

    do you have other topics/changes in mind?

    This looks like a good starting point. If anything, some of these topics might be relevant in e2e best practices as well.

  2. mvanhorn commented on Mar 19, 2026

    @mvanhorn
    Contributor

    Submitted a PR for this: #39780. Covers all 8 topics from the issue outline.

  3. bwyard commented on Apr 5, 2026

    @bwyard
    Contributor

    took a look at #39780 and the original concern from #31339.

    the gap in the closed PR was that the content could have been written without CT-specific knowledge. generic testing advice applied to CT, nothing that required knowing about the Node/browser split.

    I'd like to document the sharp edges the Node/browser split actually introduces:

    1. beforeEach({ mount }) antipattern: handle scoping and why it doesn't compose cleanly with test isolation (this was the original concern in [Feature]: Removal of mount() inside test.beforeEach() and test.afterAll() #31339)
    2. module mocking doesn't cross the boundary: vi.mock / jest.mock runs Node-side, the component is browser-side; the correct pattern is hooksConfig + beforeMount in playwright/index.ts
    3. context/page reuse: CT reuses context and page between tests as a performance optimization; what that means for test isolation and when it matters

    each item is specific to CT's architecture and wouldn't apply to Jest-based component tests or regular Playwright tests.

    does that scope match what you had in mind, or are there pitfalls from support issues worth adding? Sander (@sand4rt) Dmitry Gozman (@dgozman)

  4. ayushozha commented on Apr 6, 2026

    @ayushozha
    Contributor

    Opened a draft PR for this: #40064

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions