-
Hi, I just updated eslint plugin and immediately it started complaining about having the render method in beforeEach method. I came across this: https://github.com/testing-library/eslint-plugin-testing-library/blob/main/docs/rules/no-render-in-setup.md but im a little confused, isnt this a bit redundant? since instead of rendering in beforeEach ill be rendering inside each test? or am i supposed to render in the first test only and then only then necessary rerender it? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi! The idea behind this rule is to render the elements as close as possible to your tests, so you render them inside each test in the "arrange" step. This is also a good way to encourage merging several assertions into the same test so you render once in a test and then assert as much as possible. This doesn't mean you must have a single test to check everything, but will help you trying to write less unit tests and more integration ones. Additionally, this helps avoiding weird workarounds when you need a different fixture/scenario setup before rendering when the render is located in some You can find a bit more in the original issue: #207 Perhaps we should update the rule doc for clarifying this. |
Beta Was this translation helpful? Give feedback.
-
So, if im getting it right, is this a right example of how it should be done? test('should be not emit "checkOut" prop if user click to add to cart button and "isCartLoading" prop is true', () => {
props.isCartLoading = true;
const { container } = render(renderComponent());
expect(props.isCartLoading).toBeTruthy();
const button = within(container).getByRole('button', { name: 'checkout-button' });
userEvent.click(button);
expect(props.addToCart).not.toHaveBeenCalled();
});
test('should be emit "share" prop if user click to share button', () => {
const { container } = render(renderComponent());
const button = within(container).getByRole('button', { name: 'share-button' });
userEvent.click(button);
expect(props.share).toHaveBeenCalled();
expect(props.share).toHaveBeenCalledTimes(1);
}); I also have my doubt about the usage of |
Beta Was this translation helpful? Give feedback.
Hi! The idea behind this rule is to render the elements as close as possible to your tests, so you render them inside each test in the "arrange" step.
This is also a good way to encourage merging several assertions into the same test so you render once in a test and then assert as much as possible. This doesn't mean you must have a single test to check everything, but will help you trying to write less unit tests and more integration ones.
Additionally, this helps avoiding weird workarounds when you need a different fixture/scenario setup before rendering when the render is located in some
before*
hook.You can find a bit more in the original issue: #207
Perhaps we should update the rule …