You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Feature]: Removal of mount() inside test.beforeEach() and test.afterAll() #31339
The removal of mount() inside test.beforeEach() and test.afterAll()
Example
This pattern:
consttest=baseTest.extend({mount({ mount },use){returnuse((props)=>mount(<Context><Buttontitle="default_title"{...props}/></Context>));}});test('ok',async({ mount })=>{constcomponent=awaitmount({title: 'override_title'});awaitexpect(component).toContainText('override_title');});
Results in a way simpler testbase compared to:
letcomponent;letprops={};test.beforeEach(async({ mount })=>{component=awaitmount(<Context><Buttontitle="default_title"{...props}/></Context>);props={};});test('bad',async()=>{props={title: 'override_title'};awaitexpect(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.
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.
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.
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 😄
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!
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.
Activity
dgozman commentedon Jun 17, 2024
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.sand4rt commentedon Jun 17, 2024
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.
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.sand4rt commentedon Jun 17, 2024
That's alright. I might add a "best practices" section in
test-components.mdlater on.I don't think there are many people who will report this, but let's see. Any chance the
feature-componentstag 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).dgozman commentedon Jun 17, 2024
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 😄
sand4rt commentedon Jun 18, 2024
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!
pavelfeldman commentedon Sep 8, 2025
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.