Skip to content

Commit

Permalink
chore: use for..of instead of forEach (jestjs#14517)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Sep 19, 2023
1 parent c985cb4 commit 5b56467
Show file tree
Hide file tree
Showing 110 changed files with 696 additions and 691 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ module.exports = {
yoda: 'off',

'unicorn/explicit-length-check': 'error',
'unicorn/no-array-for-each': 'error',
'unicorn/no-negated-condition': 'error',
'unicorn/prefer-default-parameters': 'error',
'unicorn/prefer-includes': 'error',
Expand Down
2 changes: 1 addition & 1 deletion e2e/MockStdinWatchPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MockStdinWatchPlugin {
apply(jestHooks) {
jestHooks.onTestRunComplete(() => {
const {keys} = this._config.input.shift();
keys.forEach(key => this._stdin.emit('data', key));
for (const key of keys) this._stdin.emit('data', key);
});
}
}
Expand Down
8 changes: 4 additions & 4 deletions e2e/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export const writeFiles = (
files: {[filename: string]: string},
) => {
fs.mkdirSync(directory, {recursive: true});
Object.keys(files).forEach(fileOrPath => {
for (const fileOrPath of Object.keys(files)) {
const dirname = path.dirname(fileOrPath);

if (dirname !== '/') {
Expand All @@ -138,15 +138,15 @@ export const writeFiles = (
path.resolve(directory, ...fileOrPath.split('/')),
dedent(files[fileOrPath]),
);
});
}
};

export const writeSymlinks = (
directory: string,
symlinks: {[existingFile: string]: string},
) => {
fs.mkdirSync(directory, {recursive: true});
Object.keys(symlinks).forEach(fileOrPath => {
for (const fileOrPath of Object.keys(symlinks)) {
const symLinkPath = symlinks[fileOrPath];
const dirname = path.dirname(symLinkPath);

Expand All @@ -158,7 +158,7 @@ export const writeSymlinks = (
path.resolve(directory, ...symLinkPath.split('/')),
'junction',
);
});
}
};

const NUMBER_OF_TESTS_TO_FORCE_USING_WORKERS = 25;
Expand Down
4 changes: 2 additions & 2 deletions e2e/__tests__/coverageRemapping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ it('maps code coverage against original source', () => {
const coverageMap = JSON.parse(readFileSync(coverageMapFile, 'utf-8'));

// reduce absolute paths embedded in the coverage map to just filenames
Object.keys(coverageMap).forEach(filename => {
for (const filename of Object.keys(coverageMap)) {
coverageMap[filename].path = path.basename(coverageMap[filename].path);
delete coverageMap[filename].hash;
coverageMap[path.basename(filename)] = coverageMap[filename];
delete coverageMap[filename];
});
}
expect(coverageMap).toMatchSnapshot();
});
4 changes: 2 additions & 2 deletions e2e/__tests__/coverageTransformInstrumented.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ it('code coverage for transform instrumented code', () => {
const coverageMap = JSON.parse(readFileSync(coverageMapFile, 'utf-8'));

// reduce absolute paths embedded in the coverage map to just filenames
Object.keys(coverageMap).forEach(filename => {
for (const filename of Object.keys(coverageMap)) {
coverageMap[filename].path = path.basename(coverageMap[filename].path);
delete coverageMap[filename].hash;
coverageMap[path.basename(filename)] = coverageMap[filename];
delete coverageMap[filename];
});
}
expect(coverageMap).toMatchSnapshot();
});
8 changes: 4 additions & 4 deletions e2e/__tests__/errorOnDeprecated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const SHOULD_NOT_PASS_IN_JEST = new Set([
'spyOnProperty.test.js',
]);

testFiles.forEach(testFile => {
for (const testFile of testFiles) {
test(`${testFile} errors in errorOnDeprecated mode`, () => {
const result = runJest('error-on-deprecated', [
testFile,
Expand All @@ -42,9 +42,9 @@ testFiles.forEach(testFile => {

expect(rest).toMatchSnapshot();
});
});
}

testFiles.forEach(testFile => {
for (const testFile of testFiles) {
const shouldPass = SHOULD_NOT_PASS_IN_JEST.has(testFile);

const expectation = `${testFile} ${shouldPass ? 'errors' : 'passes'}`;
Expand All @@ -54,4 +54,4 @@ testFiles.forEach(testFile => {
const result = runJest('error-on-deprecated', [testFile]);
expect(result.exitCode).toBe(shouldPass ? 1 : 0);
});
});
}
6 changes: 3 additions & 3 deletions e2e/__tests__/snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@ const getSnapshotOfCopy = () => {

describe('Snapshot', () => {
const cleanup = () => {
[
for (const file of [
snapshotFile,
secondSnapshotFile,
snapshotOfCopy,
copyOfTestPath,
snapshotEscapeFile,
snapshotEscapeRegexFile,
snapshotEscapeSubstitutionFile,
].forEach(file => {
]) {
if (fileExists(file)) {
fs.unlinkSync(file);
}
});
}
if (fileExists(snapshotDir)) {
fs.rmdirSync(snapshotDir);
}
Expand Down
4 changes: 2 additions & 2 deletions e2e/__tests__/summaryThreshold.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import runJest from '../runJest';

['default', 'summary'].forEach(reporter => {
for (const reporter of ['default', 'summary']) {
describe(`${reporter} reporter`, () => {
test('prints failure messages when total number of test suites is over summaryThreshold', () => {
const {exitCode, stderr} = runJest('summary-threshold', [
Expand All @@ -26,4 +26,4 @@ import runJest from '../runJest';
);
});
});
});
}
4 changes: 2 additions & 2 deletions e2e/__tests__/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ describe('transformer caching', () => {
const loggedFiles = stdout.split('\n');

// Verify any lines logged are _just_ the file we care about
loggedFiles.forEach(line => {
for (const line of loggedFiles) {
expect(line).toBe(transformedFile);
});
}

// We run with 2 workers, so the file should be transformed twice
expect(loggedFiles).toHaveLength(2);
Expand Down
4 changes: 2 additions & 2 deletions e2e/__tests__/watchModeOnlyFailed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ test('can press "f" to run only failed tests', () => {
const results = extractSummaries(stderr);

expect(results).toHaveLength(2);
results.forEach(({rest, summary}) => {
for (const {rest, summary} of results) {
expect(rest).toMatchSnapshot('test results');
expect(summary).toMatchSnapshot('test summary');
});
}
expect(exitCode).toBe(0);
});
8 changes: 4 additions & 4 deletions e2e/__tests__/watchModePatterns.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ test('can press "p" to filter by file name', () => {

expect(stdout).toMatchSnapshot();
expect(results).toHaveLength(2);
results.forEach(({rest, summary}) => {
for (const {rest, summary} of results) {
expect(rest).toMatchSnapshot('test results');
expect(summary).toMatchSnapshot('test summary');
});
}
expect(exitCode).toBe(0);
});

Expand All @@ -66,9 +66,9 @@ test('can press "t" to filter by test name', () => {

expect(stdout).toMatchSnapshot();
expect(results).toHaveLength(2);
results.forEach(({rest, summary}) => {
for (const {rest, summary} of results) {
expect(rest).toMatchSnapshot('test results');
expect(summary).toMatchSnapshot('test summary');
});
}
expect(exitCode).toBe(0);
});
4 changes: 2 additions & 2 deletions e2e/__tests__/watchModeUpdateSnapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ test('can press "u" to update snapshots', () => {
const {exitCode, stderr} = runJest(DIR, ['--no-watchman', '--watchAll']);
const results = extractSummaries(stderr);
expect(results).toHaveLength(2);
results.forEach(({rest, summary}) => {
for (const {rest, summary} of results) {
expect(rest).toMatchSnapshot('test results');
expect(summary).toMatchSnapshot('test summary');
});
}
expect(exitCode).toBe(0);
});
4 changes: 2 additions & 2 deletions e2e/custom-reporters/reporters/AssertionCountsReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

class AssertionCountsReporter {
onTestFileResult(test, testResult, aggregatedResult) {
testResult.testResults.forEach((testCaseResult, index) => {
for (const [index, testCaseResult] of testResult.testResults.entries()) {
console.log(
`onTestFileResult testCaseResult ${index}: ${testCaseResult.title}, ` +
`status: ${testCaseResult.status}, ` +
`numExpectations: ${testCaseResult.numPassingAsserts}`,
);
});
}
}
onTestCaseResult(test, testCaseResult) {
console.log(
Expand Down
6 changes: 3 additions & 3 deletions e2e/jasmine-async/__tests__/returningValues.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
'use strict';

describe('returning values', () => {
[
for (const val of [
1,
'string',
0.1,
Expand All @@ -20,7 +20,7 @@ describe('returning values', () => {
[1],
{},
() => {},
].forEach(val => {
]) {
it(`throws if '${val}:${typeof val}' is returned`, () => val);
});
}
});
4 changes: 2 additions & 2 deletions packages/diff-sequences/__benchmarks__/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,13 @@ const testLength = n => {

writeHeading3(n);

[2, 4, 8].forEach(tenth => {
for (const tenth of [2, 4, 8]) {
testDeleteInsert(
tenth,
all,
getItems(n, i => i % 10 >= tenth && `${i}`),
);
});
}
testChange(
1,
all,
Expand Down
6 changes: 3 additions & 3 deletions packages/expect-utils/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe('getPath()', () => {
});

describe('getObjectSubset', () => {
[
for (const [object, subset, expected] of [
[{a: 'b', c: 'd'}, {a: 'd'}, {a: 'b'}],
[{a: [1, 2], b: 'b'}, {a: [3, 4]}, {a: [1, 2]}],
[[{a: 'b', c: 'd'}], [{a: 'z'}], [{a: 'b'}]],
Expand All @@ -129,15 +129,15 @@ describe('getObjectSubset', () => {
],
[{a: [1]}, {a: [1, 2]}, {a: [1]}],
[new Date('2015-11-30'), new Date('2016-12-30'), new Date('2015-11-30')],
].forEach(([object, subset, expected]) => {
]) {
test(
`expect(getObjectSubset(${stringify(object)}, ${stringify(subset)}))` +
`.toEqual(${stringify(expected)})`,
() => {
expect(getObjectSubset(object, subset)).toEqual(expected);
},
);
});
}

describe('returns the object instance if the subset has no extra properties', () => {
test('Date', () => {
Expand Down
26 changes: 13 additions & 13 deletions packages/expect-utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,18 @@ export const getObjectSubset = (
const trimmed: any = {};
seenReferences.set(object, trimmed);

getObjectKeys(object)
.filter(key => hasPropertyInObject(subset, key))
.forEach(key => {
trimmed[key] = seenReferences.has(object[key])
? seenReferences.get(object[key])
: getObjectSubset(
object[key],
subset[key],
customTesters,
seenReferences,
);
});
for (const key of getObjectKeys(object).filter(key =>
hasPropertyInObject(subset, key),
)) {
trimmed[key] = seenReferences.has(object[key])
? seenReferences.get(object[key])
: getObjectSubset(
object[key],
subset[key],
customTesters,
seenReferences,
);
}

if (getObjectKeys(trimmed).length > 0) {
return trimmed;
Expand Down Expand Up @@ -440,7 +440,7 @@ export const partition = <T>(
): [Array<T>, Array<T>] => {
const result: [Array<T>, Array<T>] = [[], []];

items.forEach(item => result[predicate(item) ? 0 : 1].push(item));
for (const item of items) result[predicate(item) ? 0 : 1].push(item);

return result;
};
Expand Down
Loading

0 comments on commit 5b56467

Please sign in to comment.