Skip to content

Commit c13bca3

Browse files
authored
chore: use String.replaceAll if possible (#14823)
1 parent 5736ae3 commit c13bca3

File tree

68 files changed

+141
-129
lines changed

Some content is hidden

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

68 files changed

+141
-129
lines changed

.eslintrc.cjs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -697,9 +697,6 @@ module.exports = {
697697
'unicorn/filename-case': 'off',
698698
'unicorn/prefer-reflect-apply': 'off',
699699

700-
// TODO: turn on at some point
701-
'unicorn/prefer-string-replace-all': 'off',
702-
703700
// enabling this is blocked by https://github.com/microsoft/rushstack/issues/2780
704701
'unicorn/prefer-export-from': 'off',
705702
// enabling this is blocked by https://github.com/jestjs/jest/pull/14297

e2e/Utils.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export const linkJestPackage = (packageName: string, cwd: string) => {
9393
export const makeTemplate =
9494
(str: string): ((values?: Array<string>) => string) =>
9595
(values = []) =>
96-
str.replace(/\$(\d+)/g, (_match, number) => {
96+
str.replaceAll(/\$(\d+)/g, (_match, number) => {
9797
if (!Array.isArray(values)) {
9898
throw new TypeError('Array of values must be passed to the template.');
9999
}
@@ -191,12 +191,12 @@ export const copyDir = (src: string, dest: string) => {
191191
};
192192

193193
export const replaceSeed = (str: string) =>
194-
str.replace(/Seed: {8}(-?\d+)/g, 'Seed: <<REPLACED>>');
194+
str.replaceAll(/Seed: {8}(-?\d+)/g, 'Seed: <<REPLACED>>');
195195

196196
export const replaceTime = (str: string) =>
197197
str
198-
.replace(/\d*\.?\d+ m?s\b/g, '<<REPLACED>>')
199-
.replace(/, estimated <<REPLACED>>/g, '');
198+
.replaceAll(/\d*\.?\d+ m?s\b/g, '<<REPLACED>>')
199+
.replaceAll(', estimated <<REPLACED>>', '');
200200

201201
// Since Jest does not guarantee the order of tests we'll sort the output.
202202
export const sortLines = (output: string) =>
@@ -235,7 +235,7 @@ export const createEmptyPackage = (
235235

236236
export const extractSummary = (stdout: string) => {
237237
const match = stdout
238-
.replace(/(?:\\[nr])+/g, '\n')
238+
.replaceAll(/(?:\\[nr])+/g, '\n')
239239
.match(
240240
/(Seed:.*\n)?Test Suites:.*\nTests.*\nSnapshots.*\nTime.*(\nRan all test suites)*.*\n*$/gm,
241241
);
@@ -252,7 +252,7 @@ export const extractSummary = (stdout: string) => {
252252
const rest = stdout
253253
.replace(match[0], '')
254254
// remove all timestamps
255-
.replace(/\s*\(\d*\.?\d+ m?s\b\)$/gm, '');
255+
.replaceAll(/\s*\(\d*\.?\d+ m?s\b\)$/gm, '');
256256

257257
return {
258258
rest: rest.trim(),

e2e/__tests__/beforeAllFiltered.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('Correct BeforeAll run', () => {
1212
let {stdout} = runJest('before-all-filtered');
1313

1414
// for some reason Circus does not have the `Object` part
15-
stdout = stdout.replace(/at Object.log \(/g, 'at log (');
15+
stdout = stdout.replaceAll(/at Object.log \(/g, 'at log (');
1616

1717
expect(stdout).toMatchSnapshot();
1818
});

e2e/__tests__/beforeEachQueue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ skipSuiteOnJestCircus(); // Circus does not support funky async definitions
1313
describe('Correct beforeEach order', () => {
1414
it('ensures the correct order for beforeEach', () => {
1515
const result = runJest('before-each-queue');
16-
expect(result.stdout.replace(/\\/g, '/')).toMatchSnapshot();
16+
expect(result.stdout.replaceAll('\\', '/')).toMatchSnapshot();
1717
});
1818
});

e2e/__tests__/customEsmTestSequencers.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test('run prioritySequence', () => {
1717

1818
expect(result.exitCode).toBe(0);
1919
const sequence = extractSummary(result.stderr)
20-
.rest.replace(/PASS /g, '')
20+
.rest.replaceAll('PASS ', '')
2121
.split('\n');
2222
expect(sequence).toEqual([
2323
'./a.test.js',

e2e/__tests__/customTestSequencers.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ test('run prioritySequence first sync', () => {
2424
);
2525
expect(result.exitCode).toBe(0);
2626
const sequence = extractSummary(result.stderr)
27-
.rest.replace(/PASS /g, '')
27+
.rest.replaceAll('PASS ', '')
2828
.split('\n');
2929
expect(sequence).toEqual([
3030
'./a.test.js',
@@ -49,7 +49,7 @@ test('run prioritySequence first async', () => {
4949
);
5050
expect(result.exitCode).toBe(0);
5151
const sequence = extractSummary(result.stderr)
52-
.rest.replace(/PASS /g, '')
52+
.rest.replaceAll('PASS ', '')
5353
.split('\n');
5454
expect(sequence).toEqual([
5555
'./a.test.js',
@@ -75,7 +75,7 @@ test('run failed tests async', () => {
7575
);
7676
expect(result.exitCode).toBe(0);
7777
const sequence = extractSummary(result.stderr)
78-
.rest.replace(/PASS /g, '')
78+
.rest.replaceAll('PASS ', '')
7979
.split('\n');
8080
expect(sequence).toEqual(['./c.test.js', './d.test.js']);
8181
});
@@ -95,7 +95,7 @@ test('run tests based on even seed', () => {
9595
);
9696
expect(result.exitCode).toBe(0);
9797
const sequence = extractSummary(result.stderr)
98-
.rest.replace(/PASS /g, '')
98+
.rest.replaceAll('PASS ', '')
9999
.split('\n');
100100
expect(sequence).toEqual([
101101
'./a.test.js',
@@ -121,7 +121,7 @@ test('run tests based on odd seed', () => {
121121
);
122122
expect(result.exitCode).toBe(0);
123123
const sequence = extractSummary(result.stderr)
124-
.rest.replace(/PASS /g, '')
124+
.rest.replaceAll('PASS ', '')
125125
.split('\n');
126126
expect(sequence).toEqual([
127127
'./e.test.js',

e2e/__tests__/declarationErrors.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import runJest from '../runJest';
1010

1111
const extractMessage = (str: string) =>
1212
extractSummary(str)
13-
.rest.replace(
13+
.rest.replaceAll(
1414
// circus-jasmine normalization
1515
/.+addSpecsToSuite (.+:\d+:\d+).+\n/g,
1616
'',

e2e/__tests__/dependencyClash.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import runJest from '../runJest';
1414
const tempDir = path.resolve(tmpdir(), 'clashing-dependencies-test');
1515
const hasteImplModulePath = path
1616
.resolve('./packages/jest-haste-map/src/__tests__/haste_impl.js')
17-
.replace(/\\/g, '\\\\');
17+
.replaceAll('\\', '\\\\');
1818

1919
beforeEach(() => {
2020
cleanup(tempDir);

e2e/__tests__/failureDetailsProperty.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {isJestJasmineRun} from '@jest/test-utils';
99
import runJest from '../runJest';
1010

1111
const removeStackTraces = (stdout: string) =>
12-
stdout.replace(
12+
stdout.replaceAll(
1313
/at (new Promise \(<anonymous>\)|.+:\d+:\d+\)?)/g,
1414
'at <stacktrace>',
1515
);

e2e/__tests__/failures.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import runJest from '../runJest';
1111

1212
const dir = path.resolve(__dirname, '../failures');
1313

14-
const normalizeDots = (text: string) => text.replace(/\.+$/gm, '.');
14+
const normalizeDots = (text: string) => text.replaceAll(/\.+$/gm, '.');
1515

1616
function cleanStderr(stderr: string) {
1717
const {rest} = extractSummary(stderr);
1818
return rest
19-
.replace(/.*(jest-jasmine2|jest-circus).*\n/g, '')
20-
.replace(new RegExp('Failed: Object {', 'g'), 'thrown: Object {');
19+
.replaceAll(/.*(jest-jasmine2|jest-circus).*\n/g, '')
20+
.replaceAll(new RegExp('Failed: Object {', 'g'), 'thrown: Object {');
2121
}
2222

2323
beforeAll(() => {
@@ -101,7 +101,7 @@ test('works with error with cause thrown outside tests', () => {
101101
const summary = normalizeDots(cleanStderr(stderr));
102102

103103
const sanitizedSummary = summary
104-
.replace(/ Suite\.f /g, ' f ') // added by jasmine runner
104+
.replaceAll(' Suite.f ', ' f ') // added by jasmine runner
105105
.split('\n')
106106
.map(line => line.trim()) // jasmine runner does not come with the same indentation
107107
.join('\n');

0 commit comments

Comments
 (0)