Skip to content

[Feature]: Removal of mount() inside test.beforeEach() and test.afterAll() #31339

Closed

Description

@sand4rt

🚀 Feature Request

The removal of mount() inside test.beforeEach() and test.afterAll()

Example

This pattern:

const test = baseTest.extend({
  mount({ mount }, use) {
    return use((props) => mount(<Context><Button title="default_title" {...props} /></Context>));
  }
});

test('ok', async ({ mount }) => {
  const component = await mount({ title: 'override_title' });
  await expect(component).toContainText('override_title');
});

Results in a way simpler testbase compared to:

let component;
let props = {};
test.beforeEach(async ({ mount }) => {
  component = await mount(<Context><Button title="default_title" {...props} /></Context>);
  props = {};
});

test('bad', async () => {
  props = { title: 'override_title' };
  await expect(component).toContainText('override_title');
});

Motivation

I've often seen that the use of test.beforeEach and test.afterEach in component testing creates an unmaintainable mess. Kent C. Dodds also discussed this in a blog post and introduced an ESLint rule to address this issue.

One of the great advantages of Playwright fixtures is how they can prevent this issue, so there is no need for an ESLint rule.

Activity

  1. dgozman commented on Jun 17, 2024

    @dgozman
    Collaborator

    Sander (@sand4rt) I think ESLint rule and/or docs is the way to fix this, instead of arbitrarily restricting mount() in some environments. There is always a way to introduce unmaintainable mess to the codebase 😄 We should have some "best practices" or "pitfalls" in the documentation describing this. Let me know what you think.

  2. sand4rt commented on Jun 17, 2024

    @sand4rt
    ContributorAuthor

    I agree that there will always be ways to introduce mess, but i don't think there is any benefit of using test.beforeEach(async ({ mount }) or test.afterEach(async ({ mount }) in component testing though.

    That's right. I just don't want us to introduce special cases like this, at least for now, until we see a lot of users bumping into this particular issue.

    Some "best practices" or "pitfalls" would definitly be nice anyway. What would be the right place for this?

    I guess the same test-components.md? We have not yet figured out a way to structure component testing docs alongside e2e docs, so let's pile up more content in that large guide for now.

  3. sand4rt commented on Jun 17, 2024

    @sand4rt
    ContributorAuthor

    That's alright. I might add a "best practices" section in test-components.md later on.

    I don't think there are many people who will report this, but let's see. Any chance the feature-components tag could be added? We can also see whether the counter is increasing here: https://github.com/search?q=beforeEach%28async+%28%7B+mount+%7D%29&type=code (43 matches ATM).

  4. dgozman commented on Jun 17, 2024

    @dgozman
    Collaborator

    We can also see whether the counter is increasing here: https://github.com/search?q=beforeEach%28async+%28%7B+mount+%7D%29&type=code (43 matches ATM).

    Thank you for the link! Looking at the usages, some of them look totally fine to me. Maybe it's not that obvious a decision, and folks should decide for themselves 😄

  5. sand4rt commented on Jun 18, 2024

    @sand4rt
    ContributorAuthor

    Thank you for the link! Looking at the usages, some of them look totally fine to me. Maybe it's not that obvious a decision, and folks should decide for themselves 😄

    Some of them look fine now, but will look terrible when they evolve, especially if different props are needed for a few tests. In my opinion, it's better to provide users the right handles rather than trying to support everything in component testing. This same mistake was made during the Enzyme days and was a painfull lesson learned. Might be interesting to save a few links and set a reminder to check in after a few months :D

    I totally respect that you don't want to add an special case just for component testing tho!

  6. pavelfeldman commented on Sep 8, 2025

    @pavelfeldman
    Member

    Why was this issue closed?

    Thank you for your involvement. This issue was closed due to limited engagement (upvotes/activity), lack of recent activity, and insufficient actionability. To maintain a manageable database, we prioritize issues based on these factors.

    If you have additional information not present in this issue that you think will help prioritizing it, please open a new issue and reference this one. More support or clarity on its necessity may prompt a review. Your understanding and cooperation are appreciated.

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