Skip to content

Commit d172bda

Browse files
authored
Add jest lint rules (#29760)
## Overview Updates `eslint-plugin-jest` and enables the recommended rules with some turned off that are unhelpful. The main motivations is: a) we have a few duplicated tests, which this found an I deleted b) making sure we don't accidentally commit skipped tests
1 parent c50d593 commit d172bda

File tree

91 files changed

+790
-812
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+790
-812
lines changed

.eslintrc.js

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const WARNING = 1;
1212
const ERROR = 2;
1313

1414
module.exports = {
15-
extends: ['prettier'],
15+
extends: ['prettier', 'plugin:jest/recommended'],
1616

1717
// Stop ESLint from looking for a configuration file in parent folders
1818
root: true,
@@ -376,16 +376,49 @@ module.exports = {
376376
files: ['**/__tests__/*.js'],
377377
rules: {
378378
// https://github.com/jest-community/eslint-plugin-jest
379-
'jest/no-focused-tests': ERROR,
380-
'jest/valid-expect': ERROR,
381-
'jest/valid-expect-in-promise': ERROR,
379+
// Meh, who cares.
380+
'jest/consistent-test-it': OFF,
381+
// Meh, we have a lot of these, who cares.
382+
'jest/no-alias-methods': OFF,
383+
// We do conditions based on feature flags.
384+
'jest/no-conditional-expect': OFF,
385+
// We have our own assertion helpers.
386+
'jest/expect-expect': OFF,
387+
// Lame rule that fires in itRender helpers or in render methods.
388+
'jest/no-standalone-expect': OFF,
382389
},
383390
},
384391
{
385-
// disable no focused tests for test setup helper files even if they are inside __tests__ directory
386-
files: ['**/setupTests.js'],
392+
// Rules specific to test setup helper files.
393+
files: [
394+
'**/setupTests.js',
395+
'**/setupEnv.js',
396+
'**/jest/TestFlags.js',
397+
'**/dom-event-testing-library/testHelpers.js',
398+
'**/utils/ReactDOMServerIntegrationTestUtils.js',
399+
'**/babel/transform-react-version-pragma.js',
400+
'**/babel/transform-test-gate-pragma.js',
401+
],
387402
rules: {
403+
// Some helpers intentionally focus tests.
388404
'jest/no-focused-tests': OFF,
405+
// Test fn helpers don't use static text names.
406+
'jest/valid-title': OFF,
407+
// We have our own assertion helpers.
408+
'jest/expect-expect': OFF,
409+
// Some helpers intentionally disable tests.
410+
'jest/no-disabled-tests': OFF,
411+
// Helpers export text function helpers.
412+
'jest/no-export': OFF,
413+
// The examples in comments trigger false errors.
414+
'jest/no-commented-out-tests': OFF,
415+
},
416+
},
417+
{
418+
files: ['**/jest/TestFlags.js'],
419+
rules: {
420+
// The examples in comments trigger false errors.
421+
'jest/no-commented-out-tests': OFF,
389422
},
390423
},
391424
{

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"eslint-plugin-babel": "^5.3.0",
5757
"eslint-plugin-eslint-plugin": "^3.5.3",
5858
"eslint-plugin-ft-flow": "^2.0.3",
59-
"eslint-plugin-jest": "^22.15.0",
59+
"eslint-plugin-jest": "28.4.0",
6060
"eslint-plugin-no-for-of-loops": "^1.0.0",
6161
"eslint-plugin-no-function-declare-after-return": "^1.0.0",
6262
"eslint-plugin-react": "^6.7.1",

packages/dom-event-testing-library/__tests__/index-test.internal.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe('createEventTarget', () => {
4545
resetActivePointers();
4646
});
4747

48-
test('returns expected API', () => {
48+
it('returns expected API', () => {
4949
const target = createEventTarget(node);
5050
expect(target.node).toEqual(node);
5151
expect(Object.keys(target)).toMatchInlineSnapshot(`
@@ -77,15 +77,15 @@ describe('createEventTarget', () => {
7777
*/
7878

7979
describe('.blur()', () => {
80-
test('default', () => {
80+
it('default', () => {
8181
const target = createEventTarget(node);
8282
node.addEventListener('blur', e => {
8383
expect(e.relatedTarget).toMatchInlineSnapshot(`null`);
8484
});
8585
target.blur();
8686
});
8787

88-
test('custom payload', () => {
88+
it('custom payload', () => {
8989
const target = createEventTarget(node);
9090
node.addEventListener('blur', e => {
9191
expect(e.relatedTarget).toMatchInlineSnapshot(`null`);
@@ -95,7 +95,7 @@ describe('createEventTarget', () => {
9595
});
9696

9797
describe('.click()', () => {
98-
test('default', () => {
98+
it('default', () => {
9999
const target = createEventTarget(node);
100100
node.addEventListener('click', e => {
101101
expect(e.altKey).toEqual(false);
@@ -122,7 +122,7 @@ describe('createEventTarget', () => {
122122
target.click();
123123
});
124124

125-
test('custom payload', () => {
125+
it('custom payload', () => {
126126
const target = createEventTarget(node);
127127
node.addEventListener('click', e => {
128128
expect(e.altKey).toEqual(true);
@@ -162,15 +162,15 @@ describe('createEventTarget', () => {
162162
});
163163

164164
describe('.focus()', () => {
165-
test('default', () => {
165+
it('default', () => {
166166
const target = createEventTarget(node);
167167
node.addEventListener('focus', e => {
168168
expect(e.relatedTarget).toMatchInlineSnapshot(`null`);
169169
});
170170
target.blur();
171171
});
172172

173-
test('custom payload', () => {
173+
it('custom payload', () => {
174174
const target = createEventTarget(node);
175175
node.addEventListener('focus', e => {
176176
expect(e.relatedTarget).toMatchInlineSnapshot(`null`);
@@ -180,7 +180,7 @@ describe('createEventTarget', () => {
180180
});
181181

182182
describe('.keydown()', () => {
183-
test('default', () => {
183+
it('default', () => {
184184
const target = createEventTarget(node);
185185
node.addEventListener('keydown', e => {
186186
expect(e.altKey).toEqual(false);
@@ -195,7 +195,7 @@ describe('createEventTarget', () => {
195195
target.keydown();
196196
});
197197

198-
test('custom payload', () => {
198+
it('custom payload', () => {
199199
const target = createEventTarget(node);
200200
node.addEventListener('keydown', e => {
201201
expect(e.altKey).toEqual(true);
@@ -217,7 +217,7 @@ describe('createEventTarget', () => {
217217
});
218218

219219
describe('.keyup()', () => {
220-
test('default', () => {
220+
it('default', () => {
221221
const target = createEventTarget(node);
222222
node.addEventListener('keyup', e => {
223223
expect(e.altKey).toEqual(false);
@@ -232,7 +232,7 @@ describe('createEventTarget', () => {
232232
target.keydown();
233233
});
234234

235-
test('custom payload', () => {
235+
it('custom payload', () => {
236236
const target = createEventTarget(node);
237237
node.addEventListener('keyup', e => {
238238
expect(e.altKey).toEqual(true);
@@ -254,7 +254,7 @@ describe('createEventTarget', () => {
254254
});
255255

256256
describe('.scroll()', () => {
257-
test('default', () => {
257+
it('default', () => {
258258
const target = createEventTarget(node);
259259
node.addEventListener('scroll', e => {
260260
expect(e.type).toEqual('scroll');
@@ -264,7 +264,7 @@ describe('createEventTarget', () => {
264264
});
265265

266266
describe('.virtualclick()', () => {
267-
test('default', () => {
267+
it('default', () => {
268268
const target = createEventTarget(node);
269269
node.addEventListener('click', e => {
270270
expect(e.altKey).toEqual(false);
@@ -291,7 +291,7 @@ describe('createEventTarget', () => {
291291
target.virtualclick();
292292
});
293293

294-
test('custom payload', () => {
294+
it('custom payload', () => {
295295
const target = createEventTarget(node);
296296
node.addEventListener('click', e => {
297297
// expect most of the custom payload to be ignored
@@ -334,7 +334,7 @@ describe('createEventTarget', () => {
334334
* Other APIs
335335
*/
336336

337-
test('.setBoundingClientRect()', () => {
337+
it('.setBoundingClientRect()', () => {
338338
const target = createEventTarget(node);
339339
expect(node.getBoundingClientRect()).toMatchInlineSnapshot(`
340340
{

packages/internal-test-utils/__tests__/ReactInternalTestUtils-test.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const {
3636
} = require('../ReactInternalTestUtils');
3737

3838
describe('ReactInternalTestUtils', () => {
39-
test('waitFor', async () => {
39+
it('waitFor', async () => {
4040
const Yield = ({id}) => {
4141
Scheduler.log(id);
4242
return id;
@@ -61,7 +61,7 @@ describe('ReactInternalTestUtils', () => {
6161
expect(root).toMatchRenderedOutput(<div>foobarbaz</div>);
6262
});
6363

64-
test('waitForAll', async () => {
64+
it('waitForAll', async () => {
6565
const Yield = ({id}) => {
6666
Scheduler.log(id);
6767
return id;
@@ -82,7 +82,7 @@ describe('ReactInternalTestUtils', () => {
8282
expect(root).toMatchRenderedOutput(<div>foobarbaz</div>);
8383
});
8484

85-
test('waitForThrow', async () => {
85+
it('waitForThrow', async () => {
8686
const Yield = ({id}) => {
8787
Scheduler.log(id);
8888
return id;
@@ -117,7 +117,7 @@ describe('ReactInternalTestUtils', () => {
117117
]);
118118
});
119119

120-
test('waitForPaint', async () => {
120+
it('waitForPaint', async () => {
121121
function App({prop}) {
122122
const deferred = useDeferredValue(prop);
123123
const text = `Urgent: ${prop}, Deferred: ${deferred}`;
@@ -143,7 +143,7 @@ describe('ReactInternalTestUtils', () => {
143143
expect(root).toMatchRenderedOutput('Urgent: B, Deferred: B');
144144
});
145145

146-
test('assertLog', async () => {
146+
it('assertLog', async () => {
147147
const Yield = ({id}) => {
148148
Scheduler.log(id);
149149
React.useEffect(() => {
@@ -732,7 +732,7 @@ describe('ReactInternalTestUtils console assertions', () => {
732732
await waitForAll(['foo', 'bar', 'baz']);
733733
});
734734

735-
test('should fail if waitForThrow is called before asserting', async () => {
735+
it('should fail if waitForThrow is called before asserting', async () => {
736736
const Yield = ({id}) => {
737737
Scheduler.log(id);
738738
return id;
@@ -774,7 +774,7 @@ describe('ReactInternalTestUtils console assertions', () => {
774774
await waitForAll(['A', 'B', 'A', 'B']);
775775
});
776776

777-
test('should fail if waitForPaint is called before asserting', async () => {
777+
it('should fail if waitForPaint is called before asserting', async () => {
778778
function App({prop}) {
779779
const deferred = useDeferredValue(prop);
780780
const text = `Urgent: ${prop}, Deferred: ${deferred}`;
@@ -1664,7 +1664,7 @@ describe('ReactInternalTestUtils console assertions', () => {
16641664
await waitForAll(['foo', 'bar', 'baz']);
16651665
});
16661666

1667-
test('should fail if waitForThrow is called before asserting', async () => {
1667+
it('should fail if waitForThrow is called before asserting', async () => {
16681668
const Yield = ({id}) => {
16691669
Scheduler.log(id);
16701670
return id;
@@ -1706,7 +1706,7 @@ describe('ReactInternalTestUtils console assertions', () => {
17061706
await waitForAll(['A', 'B', 'A', 'B']);
17071707
});
17081708

1709-
test('should fail if waitForPaint is called before asserting', async () => {
1709+
it('should fail if waitForPaint is called before asserting', async () => {
17101710
function App({prop}) {
17111711
const deferred = useDeferredValue(prop);
17121712
const text = `Urgent: ${prop}, Deferred: ${deferred}`;
@@ -2640,7 +2640,7 @@ describe('ReactInternalTestUtils console assertions', () => {
26402640
await waitForAll(['foo', 'bar', 'baz']);
26412641
});
26422642

2643-
test('should fail if waitForThrow is called before asserting', async () => {
2643+
it('should fail if waitForThrow is called before asserting', async () => {
26442644
const Yield = ({id}) => {
26452645
Scheduler.log(id);
26462646
return id;
@@ -2682,7 +2682,7 @@ describe('ReactInternalTestUtils console assertions', () => {
26822682
await waitForAll(['A', 'B', 'A', 'B']);
26832683
});
26842684

2685-
test('should fail if waitForPaint is called before asserting', async () => {
2685+
it('should fail if waitForPaint is called before asserting', async () => {
26862686
function App({prop}) {
26872687
const deferred = useDeferredValue(prop);
26882688
const text = `Urgent: ${prop}, Deferred: ${deferred}`;

packages/react-devtools-inline/__tests__/__e2e__/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const config = require('../../playwright.config');
77
const {test} = require('@playwright/test');
88

99
function runOnlyForReactRange(range) {
10+
// eslint-disable-next-line jest/no-disabled-tests
1011
test.skip(
1112
!semver.satisfies(config.use.react_version, range),
1213
`This test requires a React version of ${range} to run. ` +

packages/react-devtools-shared/src/__tests__/TimelineProfiler-test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ describe('Timeline profiler', () => {
141141
// TODO(hoxyq): investigate why running this test with React 18 fails
142142
// @reactVersion <= 18.2
143143
// @reactVersion >= 18.0
144-
xit('should mark sync render with suspense that resolves', async () => {
144+
// eslint-disable-next-line jest/no-disabled-tests
145+
it.skip('should mark sync render with suspense that resolves', async () => {
145146
const fakeSuspensePromise = Promise.resolve(true);
146147
function Example() {
147148
throw fakeSuspensePromise;
@@ -186,7 +187,8 @@ describe('Timeline profiler', () => {
186187
// TODO(hoxyq): investigate why running this test with React 18 fails
187188
// @reactVersion <= 18.2
188189
// @reactVersion >= 18.0
189-
xit('should mark sync render with suspense that rejects', async () => {
190+
// eslint-disable-next-line jest/no-disabled-tests
191+
it.skip('should mark sync render with suspense that rejects', async () => {
190192
const fakeSuspensePromise = Promise.reject(new Error('error'));
191193
function Example() {
192194
throw fakeSuspensePromise;
@@ -1528,7 +1530,7 @@ describe('Timeline profiler', () => {
15281530
`);
15291531
});
15301532

1531-
it('should mark concurrent render without suspends or state updates', () => {
1533+
it('should mark concurrent render without suspends with state updates', () => {
15321534
let updaterFn;
15331535

15341536
function Example() {

packages/react-devtools-shared/src/__tests__/__serializers__/dehydratedValueSerializer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @flow
88
*/
99

10-
// test() is part of Jest's serializer API
10+
// `test` is part of Jest's serializer API
1111
export function test(maybeDehydratedValue) {
1212
const {meta} = require('react-devtools-shared/src/hydration');
1313

packages/react-devtools-shared/src/__tests__/__serializers__/hookSerializer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function serializeHook(hook) {
3434
};
3535
}
3636

37-
// test() is part of Jest's serializer API
37+
// `test` is part of Jest's serializer API
3838
export function test(maybeHook) {
3939
if (maybeHook === null || typeof maybeHook !== 'object') {
4040
return false;

packages/react-devtools-shared/src/__tests__/__serializers__/inspectedElementSerializer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// test() is part of Jest's serializer API
1+
// `test` is part of Jest's serializer API
22
export function test(maybeInspectedElement) {
33
if (
44
maybeInspectedElement === null ||

packages/react-devtools-shared/src/__tests__/__serializers__/numberToFixedSerializer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const MAX_DECIMAL_PLACES = 3;
22

3-
// test() is part of Jest's serializer API
3+
// `test` is part of Jest's serializer API
44
export function test(maybeNumber) {
55
return (
66
typeof maybeNumber === 'number' &&

0 commit comments

Comments
 (0)