Skip to content

Commit 2e469ba

Browse files
committed
fix: updates docs, adds tests
1 parent ef1d4d9 commit 2e469ba

File tree

3 files changed

+84
-18
lines changed

3 files changed

+84
-18
lines changed

docs/rules/no-render-in-setup.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Rule Details
44

5-
This rule disallows the usage of `render` in setup functions (`beforeEach` and `beforeAll`) in favor of moving `render` closer to test assertions.
5+
This rule disallows the usage of `render` (or a custom render function) in setup functions (`beforeEach` and `beforeAll`) in favor of moving `render` closer to test assertions.
66

77
Examples of **incorrect** code for this rule:
88

@@ -18,20 +18,29 @@ it('Should have foo', () => {
1818
it('Should have bar', () => {
1919
expect(screen.getByText('bar')).toBeInTheDocument();
2020
});
21+
```
22+
23+
```js
24+
beforeAll(() => {
25+
render(<MyComponent />);
26+
});
2127

22-
it('Should have baz', () => {
23-
expect(screen.getByText('baz')).toBeInTheDocument();
28+
it('Should have foo', () => {
29+
expect(screen.getByText('foo')).toBeInTheDocument();
30+
});
31+
32+
it('Should have bar', () => {
33+
expect(screen.getByText('bar')).toBeInTheDocument();
2434
});
2535
```
2636

2737
Examples of **correct** code for this rule:
2838

2939
```js
30-
it('Should have foo, bar and baz', () => {
40+
it('Should have foo and bar', () => {
3141
render(<MyComponent />);
3242
expect(screen.getByText('foo')).toBeInTheDocument();
3343
expect(screen.getByText('bar')).toBeInTheDocument();
34-
expect(screen.getByText('baz')).toBeInTheDocument();
3544
});
3645
```
3746

@@ -41,7 +50,7 @@ If you use [custom render functions](https://testing-library.com/docs/example-re
4150
"testing-library/no-render-in-setup": ["error", {"renderFunctions": ["renderWithRedux", "renderWithRouter"]}],
4251
```
4352

44-
If you would like to allow the use of `render` (or custom render function) in _either_ `beforeAll` or `beforeEach`, this can be configured using the option `allowTestingFrameworkSetupHook`. This may be useful if you have configured your tests to [skip auto cleanup](https://testing-library.com/docs/react-testing-library/setup#skipping-auto-cleanup). `allowTestingFrameworkSetupHook` is an enum that accepts either `"beforeAll"` or `"beforeEach"`.
53+
If you would like to allow the use of `render` (or a custom render function) in _either_ `beforeAll` or `beforeEach`, this can be configured using the option `allowTestingFrameworkSetupHook`. This may be useful if you have configured your tests to [skip auto cleanup](https://testing-library.com/docs/react-testing-library/setup#skipping-auto-cleanup). `allowTestingFrameworkSetupHook` is an enum that accepts either `"beforeAll"` or `"beforeEach"`.
4554

4655
```
4756
"testing-library/no-render-in-setup": ["error", {"allowTestingFrameworkSetupHook": "beforeAll"}],

lib/node-utils.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,16 @@ export function isRenderFunction(
5454
callNode: TSESTree.CallExpression,
5555
renderFunctions: string[]
5656
) {
57-
return ['render', ...renderFunctions].some(
58-
name => isIdentifier(callNode.callee) && name === callNode.callee.name
59-
);
57+
// returns true for `render` and e.g. `customRenderFn`
58+
// as well as `someLib.render` and `someUtils.customRenderFn`
59+
return ['render', ...renderFunctions].some(name => {
60+
return (
61+
(isIdentifier(callNode.callee) && name === callNode.callee.name) ||
62+
(isMemberExpression(callNode.callee) &&
63+
isIdentifier(callNode.callee.property) &&
64+
name === callNode.callee.property.name)
65+
);
66+
});
6067
}
6168

6269
export function isObjectPattern(

tests/lib/rules/no-render-in-setup.test.ts

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ ruleTester.run(RULE_NAME, rule, {
1717
})
1818
`,
1919
},
20+
// test config options
2021
...TESTING_FRAMEWORK_SETUP_HOOKS.map(setupHook => ({
2122
code: `
2223
${setupHook}(() => {
23-
const wrapper = () => {
24-
renderWithRedux(<Component/>)
25-
}
26-
wrapper();
24+
renderWithRedux(<Component/>)
2725
})
2826
`,
2927
options: [
@@ -33,6 +31,14 @@ ruleTester.run(RULE_NAME, rule, {
3331
},
3432
],
3533
})),
34+
// // test usage of a non-Testing Library render fn
35+
// ...TESTING_FRAMEWORK_SETUP_HOOKS.map(setupHook => ({
36+
// code: `import { render } from 'imNoTestingLibrary';
37+
38+
// ${setupHook}(() => {
39+
// render(<Component/>)
40+
// })`,
41+
// })),
3642
],
3743

3844
invalid: [
@@ -85,7 +91,7 @@ ruleTester.run(RULE_NAME, rule, {
8591
const wrapper = () => {
8692
render(<Component/>)
8793
}
88-
wrapper();
94+
wrapper()
8995
})
9096
`,
9197
errors: [
@@ -101,10 +107,7 @@ ruleTester.run(RULE_NAME, rule, {
101107
return {
102108
code: `
103109
${disallowedHook}(() => {
104-
const wrapper = () => {
105-
render(<Component/>)
106-
}
107-
wrapper();
110+
render(<Component/>)
108111
})
109112
`,
110113
options: [
@@ -119,5 +122,52 @@ ruleTester.run(RULE_NAME, rule, {
119122
],
120123
};
121124
}),
125+
...TESTING_FRAMEWORK_SETUP_HOOKS.map(setupHook => ({
126+
code: `import { render } from '@testing-library/foo';
127+
128+
${setupHook}(() => {
129+
render(<Component/>)
130+
})`,
131+
errors: [
132+
{
133+
messageId: 'noRenderInSetup',
134+
},
135+
],
136+
})),
137+
...TESTING_FRAMEWORK_SETUP_HOOKS.map(setupHook => ({
138+
code: `import * as testingLibrary from '@testing-library/foo';
139+
140+
${setupHook}(() => {
141+
testingLibrary.render(<Component/>)
142+
})`,
143+
errors: [
144+
{
145+
messageId: 'noRenderInSetup',
146+
},
147+
],
148+
})),
149+
...TESTING_FRAMEWORK_SETUP_HOOKS.map(setupHook => ({
150+
code: `import { render } from 'imNoTestingLibrary';
151+
import * as testUtils from '../utils';
152+
153+
${setupHook}(() => {
154+
testUtils.renderWithRedux(<Component/>)
155+
})
156+
157+
it('Test', () => {
158+
render(<Component/>)
159+
})
160+
`,
161+
options: [
162+
{
163+
renderFunctions: ['renderWithRedux'],
164+
},
165+
],
166+
errors: [
167+
{
168+
messageId: 'noRenderInSetup',
169+
},
170+
],
171+
})),
122172
],
123173
});

0 commit comments

Comments
 (0)