Skip to content

Commit

Permalink
chore(lint): use jest/prefer-to-have-length rule (#13370)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrazauskas authored Oct 3, 2022
1 parent 50bab21 commit 71044c0
Show file tree
Hide file tree
Showing 34 changed files with 112 additions and 108 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ module.exports = {
'jest/no-focused-tests': 'error',
'jest/no-identical-title': 'error',
'jest/prefer-to-be': 'error',
'jest/prefer-to-contain': 'error',
'jest/prefer-to-have-length': 'error',
'jest/valid-expect': 'error',
},
},
Expand Down
2 changes: 1 addition & 1 deletion docs/Es6ClassMocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ jest.mock('./sound-player', () => {
});
```

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);`
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);`

### Mocking non-default class exports

Expand Down
8 changes: 4 additions & 4 deletions docs/JestObjectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,16 +230,16 @@ const example = jest.createMockFromModule('../example');
test('should run example code', () => {
// creates a new mocked function with no formal arguments.
expect(example.function.name).toBe('square');
expect(example.function.length).toBe(0);
expect(example.function).toHaveLength(0);

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

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

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

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

// creates a new property with the same primitive value as the original property.
expect(example.number).toBe(123);
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/jasmineAsync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('async jasmine', () => {
expect(json.numPendingTests).toBe(0);
expect(json.testResults[0].message).toBe('');

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

it('works with afterEach', () => {
Expand Down
4 changes: 2 additions & 2 deletions e2e/__tests__/setupFilesAfterEnvConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('setupFilesAfterEnv', () => {

expect(result.json.numTotalTests).toBe(2);
expect(result.json.numPassedTests).toBe(2);
expect(result.json.testResults.length).toBe(2);
expect(result.json.testResults).toHaveLength(2);
expect(result.exitCode).toBe(0);
});

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

expect(result.json.numTotalTests).toBe(1);
expect(result.json.numPassedTests).toBe(1);
expect(result.json.testResults.length).toBe(1);
expect(result.json.testResults).toHaveLength(1);
expect(result.exitCode).toBe(0);
});
});
2 changes: 1 addition & 1 deletion e2e/__tests__/testInRoot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ it('runs tests in only test.js and spec.js', () => {
.map(name => path.basename(name))
.sort();

expect(testNames.length).toBe(2);
expect(testNames).toHaveLength(2);
expect(testNames[0]).toBe('spec.js');
expect(testNames[1]).toBe('test.js');
});
8 changes: 4 additions & 4 deletions e2e/auto-clear-mocks/with-auto-clear/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ test('first test', () => {
importedFn();
expect(localFn()).toBe('abcd');

expect(importedFn.mock.calls.length).toBe(1);
expect(localFn.mock.calls.length).toBe(1);
expect(importedFn).toHaveBeenCalledTimes(1);
expect(localFn).toHaveBeenCalledTimes(1);
});

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

expect(importedFn.mock.calls.length).toBe(1);
expect(localFn.mock.calls.length).toBe(1);
expect(importedFn).toHaveBeenCalledTimes(1);
expect(localFn).toHaveBeenCalledTimes(1);
});
16 changes: 8 additions & 8 deletions e2e/auto-clear-mocks/without-auto-clear/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ describe('without an explicit reset', () => {
importedFn();
expect(localFn()).toBe('abcd');

expect(importedFn.mock.calls.length).toBe(1);
expect(localFn.mock.calls.length).toBe(1);
expect(importedFn).toHaveBeenCalledTimes(1);
expect(localFn).toHaveBeenCalledTimes(1);
});

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

expect(importedFn.mock.calls.length).toBe(2);
expect(localFn.mock.calls.length).toBe(2);
expect(importedFn).toHaveBeenCalledTimes(2);
expect(localFn).toHaveBeenCalledTimes(2);
});
});

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

expect(importedFn.mock.calls.length).toBe(1);
expect(localFn.mock.calls.length).toBe(1);
expect(importedFn).toHaveBeenCalledTimes(1);
expect(localFn).toHaveBeenCalledTimes(1);
});

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

expect(importedFn.mock.calls.length).toBe(1);
expect(localFn.mock.calls.length).toBe(1);
expect(importedFn).toHaveBeenCalledTimes(1);
expect(localFn).toHaveBeenCalledTimes(1);
});
});
8 changes: 4 additions & 4 deletions e2e/auto-reset-mocks/with-auto-reset/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ test('first test', () => {
importedFn();
localFn();

expect(importedFn.mock.calls.length).toBe(1);
expect(localFn.mock.calls.length).toBe(1);
expect(importedFn).toHaveBeenCalledTimes(1);
expect(localFn).toHaveBeenCalledTimes(1);
});

test('second test', () => {
importedFn();
localFn();

expect(importedFn.mock.calls.length).toBe(1);
expect(localFn.mock.calls.length).toBe(1);
expect(importedFn).toHaveBeenCalledTimes(1);
expect(localFn).toHaveBeenCalledTimes(1);
});
16 changes: 8 additions & 8 deletions e2e/auto-reset-mocks/without-auto-reset/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ describe('without an explicit reset', () => {
importedFn();
localFn();

expect(importedFn.mock.calls.length).toBe(1);
expect(localFn.mock.calls.length).toBe(1);
expect(importedFn).toHaveBeenCalledTimes(1);
expect(localFn).toHaveBeenCalledTimes(1);
});

test('second test', () => {
importedFn();
localFn();

expect(importedFn.mock.calls.length).toBe(2);
expect(localFn.mock.calls.length).toBe(2);
expect(importedFn).toHaveBeenCalledTimes(2);
expect(localFn).toHaveBeenCalledTimes(2);
});
});

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

expect(importedFn.mock.calls.length).toBe(1);
expect(localFn.mock.calls.length).toBe(1);
expect(importedFn).toHaveBeenCalledTimes(1);
expect(localFn).toHaveBeenCalledTimes(1);
});

test('second test', () => {
importedFn();
localFn();

expect(importedFn.mock.calls.length).toBe(1);
expect(localFn.mock.calls.length).toBe(1);
expect(importedFn).toHaveBeenCalledTimes(1);
expect(localFn).toHaveBeenCalledTimes(1);
});
});
2 changes: 1 addition & 1 deletion examples/manual-mocks/__tests__/file_summarizer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ describe('listFilesInDirectorySync', () => {
const fileSummary =
FileSummarizer.summarizeFilesInDirectorySync('/path/to');

expect(fileSummary.length).toBe(2);
expect(fileSummary).toHaveLength(2);
});
});
2 changes: 1 addition & 1 deletion packages/babel-jest/src/__tests__/getCacheKey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('getCacheKey', () => {
const oldCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);

test('returns cache key hash', () => {
expect(oldCacheKey.length).toBe(32);
expect(oldCacheKey).toHaveLength(32);
});

test('if `THIS_FILE` value is changing', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-config/src/__tests__/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ describe('testMatch', () => {
{} as Config.Argv,
);

expect(options.testMatch.length).toBe(0);
expect(options.testMatch).toHaveLength(0);
});

it('testRegex default not applied if testMatch is set', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ test('creation of a cache key', () => {
instrument: true,
});

expect(hashA.length).toBe(32);
expect(hashA).toHaveLength(32);
expect(hashA).not.toEqual(hashB);
expect(hashA).not.toEqual(hashC);
});
3 changes: 2 additions & 1 deletion packages/jest-each/src/__tests__/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,8 @@ describe('jest-each', () => {
const testFunction = get(eachObject, keyPath);
testFunction('expected string', function (hello, done) {
expect(hello).toBe('hello');
expect(arguments.length).toBe(1);
// eslint-disable-next-line prefer-rest-params
expect(arguments).toHaveLength(1);
expect(done).toBeUndefined();
});
get(globalTestMocks, keyPath).mock.calls[0][1]('DONE');
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-each/src/__tests__/template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,8 @@ describe('jest-each', () => {
expect(b).toBe(1);
expect(expected).toBe(1);
expect(done).toBeUndefined();
expect(arguments.length).toBe(1);
// eslint-disable-next-line prefer-rest-params
expect(arguments).toHaveLength(1);
});
get(globalTestMocks, keyPath).mock.calls[0][1]('DONE');
},
Expand Down
12 changes: 6 additions & 6 deletions packages/jest-haste-map/src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ describe('HasteMap', () => {
expect(data.map.get('fbjs')).toBeUndefined();

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

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

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

fs.readFileSync.mockClear();

Expand All @@ -902,7 +902,7 @@ describe('HasteMap', () => {
const {__hasteMapForTest: data} = await (
await HasteMap.create(defaultConfig)
).build();
expect(fs.readFileSync.mock.calls.length).toBe(1);
expect(fs.readFileSync).toHaveBeenCalledTimes(1);
if (require('v8').deserialize) {
expect(fs.readFileSync).toBeCalledWith(cacheFilePath);
} else {
Expand Down Expand Up @@ -936,7 +936,7 @@ describe('HasteMap', () => {
await HasteMap.create(defaultConfig)
).build();

expect(fs.readFileSync.mock.calls.length).toBe(2);
expect(fs.readFileSync).toHaveBeenCalledTimes(2);

if (require('v8').serialize) {
expect(fs.readFileSync).toBeCalledWith(cacheFilePath);
Expand Down Expand Up @@ -1279,9 +1279,9 @@ describe('HasteMap', () => {
})
).build();

expect(jestWorker.mock.calls.length).toBe(1);
expect(jestWorker).toHaveBeenCalledTimes(1);

expect(mockWorker.mock.calls.length).toBe(5);
expect(mockWorker).toHaveBeenCalledTimes(5);

expect(mockWorker.mock.calls).toEqual([
[
Expand Down
Loading

0 comments on commit 71044c0

Please sign in to comment.