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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 8 additions & 8 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 8 additions & 8 deletions
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
});

0 commit comments

Comments
 (0)