Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request: new rule; no-render-in-setup #207

Closed
benmonro opened this issue Aug 4, 2020 · 12 comments · Fixed by #209
Closed

feature request: new rule; no-render-in-setup #207

benmonro opened this issue Aug 4, 2020 · 12 comments · Fixed by #209
Assignees
Labels
new rule New rule to be included in the plugin released

Comments

@benmonro
Copy link
Member

benmonro commented Aug 4, 2020

imo, it would be nice to have a rule that doesn't allow the usage of render in setup functions (before, beforeEach, etc)

Would also be nice to have the ability to configure custom render functions, we have one for renderWithContext for example.

I've seen people do code like this:

beforeEach(() => {
  render(<MyComponent />);
});

it("Should have foo", () => {
  expect(screen.getByText("foo")).toBeInTheDocument();
});

it("Should have bar", () => {
  expect(screen.getByText("bar")).toBeInTheDocument();
});

it("Should have baz", () => {
  expect(screen.getByText("baz")).toBeInTheDocument();
});

imo this should just be a single test. why render it N times when nothing is changing in the render?

@Belco90 Belco90 added the new rule New rule to be included in the plugin label Aug 4, 2020
@Belco90
Copy link
Member

Belco90 commented Aug 4, 2020

Thanks for your idea @benmonro. Are you interested in implementing this one yourself?

Ability to support custom render functions will be added globally on version 4: #198

@benmonro
Copy link
Member Author

benmonro commented Aug 4, 2020

Possibly. I've been quite busy lately not sure when I can get to it. I'd someone else wants it that'd be awesome

@alessbell
Copy link
Contributor

👋 I'm interested in picking this up. Ok if I assign myself @Belco90 ?

@Belco90
Copy link
Member

Belco90 commented Aug 4, 2020

@alessbell Sure! That'd be amazing, thanks ✨

@Belco90
Copy link
Member

Belco90 commented Aug 10, 2020

🎉 This issue has been resolved in version 3.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@kevinbeal
Copy link

What is the rationale for this rule?

@Belco90
Copy link
Member

Belco90 commented Jul 14, 2022

What is the rationale for this rule?

https://kentcdodds.com/blog/avoid-nesting-when-youre-testing

(We should add this to the rule's doc)

@kevinbeal
Copy link

@Belco90 Thanks for getting back so fast. Not sure I share Kent's concern, but I'm glad to have read it :). Obviously, I can just disable the rule if I want to.

@Floby
Copy link

Floby commented Jul 26, 2022

The author's explanation of the blog post linked above is almost uniquely expressed as a personal taste. This rule seems a bit too specific to be applied by default as an error, given that the error message and associated doc doesn't mention the cause for such a rule.

let me submit a pull request about this =)

@bvandercar-vt
Copy link

imo, it would be nice to have a rule that doesn't allow the usage of render in setup functions (before, beforeEach, etc)

Would also be nice to have the ability to configure custom render functions, we have one for renderWithContext for example.

I've seen people do code like this:

beforeEach(() => {
  render(<MyComponent />);
});

it("Should have foo", () => {
  expect(screen.getByText("foo")).toBeInTheDocument();
});

it("Should have bar", () => {
  expect(screen.getByText("bar")).toBeInTheDocument();
});

it("Should have baz", () => {
  expect(screen.getByText("baz")).toBeInTheDocument();
});

imo this should just be a single test. why render it N times when nothing is changing in the render?

Your example is a badly coded example. There is no rule that can prevent badly written tests like your case.

render in beforeEach is used when the state of the component is needed to be reset, which is incredibly common in testing.

We all agree that in your example, it should all be in one test. But just because you have this one-off case of a badly written test suite doesn't mean there should be a rule to prevent using render in beforeEach, when there are much more likely, frequent, and acceptable cases where people do that, and should be allowed to do it.

@bvandercar-vt
Copy link

bvandercar-vt commented Nov 30, 2022

The author's explanation of the blog post linked above is almost uniquely expressed as a personal taste. This rule seems a bit too specific to be applied by default as an error, given that the error message and associated doc doesn't mention the cause for such a rule.

let me submit a pull request about this =)

I agree with everything in this sentiment.

In addition, the author states:

This pattern:

test('whatever', () => {
  const foo = someThing()
  // use foo
})

makes for a WAY simpler testbase than:

let foo
beforeEach(() => {
  foo = someThing()
})

test('whatever', () => {
  // use foo
})

but this is just badly written code to begin with. What about the common case where no variable is assigned to?

beforeEach(() => {
  render(<Component />)
})

I see no reason this shouldn't be allowed.

It just seems like we are taking away from the entire intent of the existence of the beforeEach hook here.

@Belco90
Copy link
Member

Belco90 commented Nov 30, 2022

Some rules prevent actual errors, others are recommendations. This rule won't prevent an actual error, it's just considered a best practice.

The example illustrated is a common scenario. It's true that there could be a better example. However, it doesn't matter if you need to reset the component between tests, or pass several props or none at all. The recommendation is to avoid abstractions in tests to make it simple to follow and maintain.

If you don't agree with the explanation given, or you found a scenario in your codebase to allow this pattern, it's as simple as turning off the rule.

@testing-library testing-library locked as resolved and limited conversation to collaborators Dec 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
new rule New rule to be included in the plugin released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants