Skip to content

Commit 71044c0

Browse files
authored
chore(lint): use jest/prefer-to-have-length rule (#13370)
1 parent 50bab21 commit 71044c0

File tree

34 files changed

+112
-108
lines changed

34 files changed

+112
-108
lines changed

.eslintrc.cjs

+2
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ module.exports = {
167167
'jest/no-focused-tests': 'error',
168168
'jest/no-identical-title': 'error',
169169
'jest/prefer-to-be': 'error',
170+
'jest/prefer-to-contain': 'error',
171+
'jest/prefer-to-have-length': 'error',
170172
'jest/valid-expect': 'error',
171173
},
172174
},

docs/Es6ClassMocks.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ jest.mock('./sound-player', () => {
349349
});
350350
```
351351

352-
This will let us inspect usage of our mocked class, using `SoundPlayer.mock.calls`: `expect(SoundPlayer).toHaveBeenCalled();` or near-equivalent: `expect(SoundPlayer.mock.calls.length).toBe(1);`
352+
This will let us inspect usage of our mocked class, using `SoundPlayer.mock.calls`: `expect(SoundPlayer).toHaveBeenCalled();` or near-equivalent: `expect(SoundPlayer.mock.calls.length).toBeGreaterThan(0);`
353353

354354
### Mocking non-default class exports
355355

docs/JestObjectAPI.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -230,16 +230,16 @@ const example = jest.createMockFromModule('../example');
230230
test('should run example code', () => {
231231
// creates a new mocked function with no formal arguments.
232232
expect(example.function.name).toBe('square');
233-
expect(example.function.length).toBe(0);
233+
expect(example.function).toHaveLength(0);
234234

235235
// async functions get the same treatment as standard synchronous functions.
236236
expect(example.asyncFunction.name).toBe('asyncSquare');
237-
expect(example.asyncFunction.length).toBe(0);
237+
expect(example.asyncFunction).toHaveLength(0);
238238

239239
// creates a new class with the same interface, member functions and properties are mocked.
240240
expect(example.class.constructor.name).toBe('Bar');
241241
expect(example.class.foo.name).toBe('foo');
242-
expect(example.class.array.length).toBe(0);
242+
expect(example.class.array).toHaveLength(0);
243243

244244
// creates a deeply cloned version of the original object.
245245
expect(example.object).toEqual({
@@ -251,7 +251,7 @@ test('should run example code', () => {
251251
});
252252

253253
// creates a new empty array, ignoring the original array.
254-
expect(example.array.length).toBe(0);
254+
expect(example.array).toHaveLength(0);
255255

256256
// creates a new property with the same primitive value as the original property.
257257
expect(example.number).toBe(123);

e2e/__tests__/jasmineAsync.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('async jasmine', () => {
4747
expect(json.numPendingTests).toBe(0);
4848
expect(json.testResults[0].message).toBe('');
4949

50-
expect((result.stderr.match(/unset flag/g) || []).length).toBe(1);
50+
expect(result.stderr.match(/unset flag/g) || []).toHaveLength(1);
5151
});
5252

5353
it('works with afterEach', () => {

e2e/__tests__/setupFilesAfterEnvConfig.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('setupFilesAfterEnv', () => {
4040

4141
expect(result.json.numTotalTests).toBe(2);
4242
expect(result.json.numPassedTests).toBe(2);
43-
expect(result.json.testResults.length).toBe(2);
43+
expect(result.json.testResults).toHaveLength(2);
4444
expect(result.exitCode).toBe(0);
4545
});
4646

@@ -61,7 +61,7 @@ describe('setupFilesAfterEnv', () => {
6161

6262
expect(result.json.numTotalTests).toBe(1);
6363
expect(result.json.numPassedTests).toBe(1);
64-
expect(result.json.testResults.length).toBe(1);
64+
expect(result.json.testResults).toHaveLength(1);
6565
expect(result.exitCode).toBe(0);
6666
});
6767
});

e2e/__tests__/testInRoot.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ it('runs tests in only test.js and spec.js', () => {
1919
.map(name => path.basename(name))
2020
.sort();
2121

22-
expect(testNames.length).toBe(2);
22+
expect(testNames).toHaveLength(2);
2323
expect(testNames[0]).toBe('spec.js');
2424
expect(testNames[1]).toBe('test.js');
2525
});

e2e/auto-clear-mocks/with-auto-clear/__tests__/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ test('first test', () => {
1515
importedFn();
1616
expect(localFn()).toBe('abcd');
1717

18-
expect(importedFn.mock.calls.length).toBe(1);
19-
expect(localFn.mock.calls.length).toBe(1);
18+
expect(importedFn).toHaveBeenCalledTimes(1);
19+
expect(localFn).toHaveBeenCalledTimes(1);
2020
});
2121

2222
test('second test', () => {
2323
importedFn();
2424
expect(localFn()).toBe('abcd');
2525

26-
expect(importedFn.mock.calls.length).toBe(1);
27-
expect(localFn.mock.calls.length).toBe(1);
26+
expect(importedFn).toHaveBeenCalledTimes(1);
27+
expect(localFn).toHaveBeenCalledTimes(1);
2828
});

e2e/auto-clear-mocks/without-auto-clear/__tests__/index.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ describe('without an explicit reset', () => {
1717
importedFn();
1818
expect(localFn()).toBe('abcd');
1919

20-
expect(importedFn.mock.calls.length).toBe(1);
21-
expect(localFn.mock.calls.length).toBe(1);
20+
expect(importedFn).toHaveBeenCalledTimes(1);
21+
expect(localFn).toHaveBeenCalledTimes(1);
2222
});
2323

2424
test('second test', () => {
2525
importedFn();
2626
expect(localFn()).toBe('abcd');
2727

28-
expect(importedFn.mock.calls.length).toBe(2);
29-
expect(localFn.mock.calls.length).toBe(2);
28+
expect(importedFn).toHaveBeenCalledTimes(2);
29+
expect(localFn).toHaveBeenCalledTimes(2);
3030
});
3131
});
3232

@@ -39,15 +39,15 @@ describe('with an explicit reset', () => {
3939
importedFn();
4040
expect(localFn()).toBe('abcd');
4141

42-
expect(importedFn.mock.calls.length).toBe(1);
43-
expect(localFn.mock.calls.length).toBe(1);
42+
expect(importedFn).toHaveBeenCalledTimes(1);
43+
expect(localFn).toHaveBeenCalledTimes(1);
4444
});
4545

4646
test('second test', () => {
4747
importedFn();
4848
expect(localFn()).toBe('abcd');
4949

50-
expect(importedFn.mock.calls.length).toBe(1);
51-
expect(localFn.mock.calls.length).toBe(1);
50+
expect(importedFn).toHaveBeenCalledTimes(1);
51+
expect(localFn).toHaveBeenCalledTimes(1);
5252
});
5353
});

e2e/auto-reset-mocks/with-auto-reset/__tests__/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ test('first test', () => {
1515
importedFn();
1616
localFn();
1717

18-
expect(importedFn.mock.calls.length).toBe(1);
19-
expect(localFn.mock.calls.length).toBe(1);
18+
expect(importedFn).toHaveBeenCalledTimes(1);
19+
expect(localFn).toHaveBeenCalledTimes(1);
2020
});
2121

2222
test('second test', () => {
2323
importedFn();
2424
localFn();
2525

26-
expect(importedFn.mock.calls.length).toBe(1);
27-
expect(localFn.mock.calls.length).toBe(1);
26+
expect(importedFn).toHaveBeenCalledTimes(1);
27+
expect(localFn).toHaveBeenCalledTimes(1);
2828
});

e2e/auto-reset-mocks/without-auto-reset/__tests__/index.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ describe('without an explicit reset', () => {
1717
importedFn();
1818
localFn();
1919

20-
expect(importedFn.mock.calls.length).toBe(1);
21-
expect(localFn.mock.calls.length).toBe(1);
20+
expect(importedFn).toHaveBeenCalledTimes(1);
21+
expect(localFn).toHaveBeenCalledTimes(1);
2222
});
2323

2424
test('second test', () => {
2525
importedFn();
2626
localFn();
2727

28-
expect(importedFn.mock.calls.length).toBe(2);
29-
expect(localFn.mock.calls.length).toBe(2);
28+
expect(importedFn).toHaveBeenCalledTimes(2);
29+
expect(localFn).toHaveBeenCalledTimes(2);
3030
});
3131
});
3232

@@ -39,15 +39,15 @@ describe('with an explicit reset', () => {
3939
importedFn();
4040
localFn();
4141

42-
expect(importedFn.mock.calls.length).toBe(1);
43-
expect(localFn.mock.calls.length).toBe(1);
42+
expect(importedFn).toHaveBeenCalledTimes(1);
43+
expect(localFn).toHaveBeenCalledTimes(1);
4444
});
4545

4646
test('second test', () => {
4747
importedFn();
4848
localFn();
4949

50-
expect(importedFn.mock.calls.length).toBe(1);
51-
expect(localFn.mock.calls.length).toBe(1);
50+
expect(importedFn).toHaveBeenCalledTimes(1);
51+
expect(localFn).toHaveBeenCalledTimes(1);
5252
});
5353
});

examples/manual-mocks/__tests__/file_summarizer.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ describe('listFilesInDirectorySync', () => {
2020
const fileSummary =
2121
FileSummarizer.summarizeFilesInDirectorySync('/path/to');
2222

23-
expect(fileSummary.length).toBe(2);
23+
expect(fileSummary).toHaveLength(2);
2424
});
2525
});

packages/babel-jest/src/__tests__/getCacheKey.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('getCacheKey', () => {
4444
const oldCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);
4545

4646
test('returns cache key hash', () => {
47-
expect(oldCacheKey.length).toBe(32);
47+
expect(oldCacheKey).toHaveLength(32);
4848
});
4949

5050
test('if `THIS_FILE` value is changing', () => {

packages/jest-config/src/__tests__/normalize.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ describe('testMatch', () => {
878878
{} as Config.Argv,
879879
);
880880

881-
expect(options.testMatch.length).toBe(0);
881+
expect(options.testMatch).toHaveLength(0);
882882
});
883883

884884
it('testRegex default not applied if testMatch is set', async () => {

packages/jest-create-cache-key-function/src/__tests__/index.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ test('creation of a cache key', () => {
4040
instrument: true,
4141
});
4242

43-
expect(hashA.length).toBe(32);
43+
expect(hashA).toHaveLength(32);
4444
expect(hashA).not.toEqual(hashB);
4545
expect(hashA).not.toEqual(hashC);
4646
});

packages/jest-each/src/__tests__/array.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,8 @@ describe('jest-each', () => {
450450
const testFunction = get(eachObject, keyPath);
451451
testFunction('expected string', function (hello, done) {
452452
expect(hello).toBe('hello');
453-
expect(arguments.length).toBe(1);
453+
// eslint-disable-next-line prefer-rest-params
454+
expect(arguments).toHaveLength(1);
454455
expect(done).toBeUndefined();
455456
});
456457
get(globalTestMocks, keyPath).mock.calls[0][1]('DONE');

packages/jest-each/src/__tests__/template.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,8 @@ describe('jest-each', () => {
549549
expect(b).toBe(1);
550550
expect(expected).toBe(1);
551551
expect(done).toBeUndefined();
552-
expect(arguments.length).toBe(1);
552+
// eslint-disable-next-line prefer-rest-params
553+
expect(arguments).toHaveLength(1);
553554
});
554555
get(globalTestMocks, keyPath).mock.calls[0][1]('DONE');
555556
},

packages/jest-haste-map/src/__tests__/index.test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ describe('HasteMap', () => {
722722
expect(data.map.get('fbjs')).toBeUndefined();
723723

724724
// cache file + 5 modules - the node_module
725-
expect(fs.readFileSync.mock.calls.length).toBe(6);
725+
expect(fs.readFileSync).toHaveBeenCalledTimes(6);
726726
});
727727

728728
it('warns on duplicate mock files', async () => {
@@ -886,7 +886,7 @@ describe('HasteMap', () => {
886886

887887
// The first run should access the file system once for the (empty)
888888
// cache file and five times for the files in the system.
889-
expect(fs.readFileSync.mock.calls.length).toBe(6);
889+
expect(fs.readFileSync).toHaveBeenCalledTimes(6);
890890

891891
fs.readFileSync.mockClear();
892892

@@ -902,7 +902,7 @@ describe('HasteMap', () => {
902902
const {__hasteMapForTest: data} = await (
903903
await HasteMap.create(defaultConfig)
904904
).build();
905-
expect(fs.readFileSync.mock.calls.length).toBe(1);
905+
expect(fs.readFileSync).toHaveBeenCalledTimes(1);
906906
if (require('v8').deserialize) {
907907
expect(fs.readFileSync).toBeCalledWith(cacheFilePath);
908908
} else {
@@ -936,7 +936,7 @@ describe('HasteMap', () => {
936936
await HasteMap.create(defaultConfig)
937937
).build();
938938

939-
expect(fs.readFileSync.mock.calls.length).toBe(2);
939+
expect(fs.readFileSync).toHaveBeenCalledTimes(2);
940940

941941
if (require('v8').serialize) {
942942
expect(fs.readFileSync).toBeCalledWith(cacheFilePath);
@@ -1279,9 +1279,9 @@ describe('HasteMap', () => {
12791279
})
12801280
).build();
12811281

1282-
expect(jestWorker.mock.calls.length).toBe(1);
1282+
expect(jestWorker).toHaveBeenCalledTimes(1);
12831283

1284-
expect(mockWorker.mock.calls.length).toBe(5);
1284+
expect(mockWorker).toHaveBeenCalledTimes(5);
12851285

12861286
expect(mockWorker.mock.calls).toEqual([
12871287
[

0 commit comments

Comments
 (0)