diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 62117ad1727a..75ce0a7f3bd3 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -681,7 +681,6 @@ module.exports = { 'unicorn/prefer-reflect-apply': 'off', // TODO: turn on at some point - 'unicorn/better-regex': 'off', 'unicorn/catch-error-name': 'off', 'unicorn/consistent-function-scoping': 'off', 'unicorn/error-message': 'off', @@ -713,7 +712,6 @@ module.exports = { 'unicorn/prefer-number-properties': 'off', 'unicorn/prefer-object-from-entries': 'off', 'unicorn/prefer-prototype-methods': 'off', - 'unicorn/prefer-regexp-test': 'off', 'unicorn/prefer-spread': 'off', 'unicorn/prefer-string-replace-all': 'off', 'unicorn/prevent-abbreviations': 'off', diff --git a/e2e/Utils.ts b/e2e/Utils.ts index 9374b9c0129e..083cd8dc261a 100644 --- a/e2e/Utils.ts +++ b/e2e/Utils.ts @@ -235,7 +235,7 @@ export const createEmptyPackage = ( export const extractSummary = (stdout: string) => { const match = stdout - .replace(/(?:\\[rn])+/g, '\n') + .replace(/(?:\\[nr])+/g, '\n') .match( /(Seed:.*\n)?Test Suites:.*\nTests.*\nSnapshots.*\nTime.*(\nRan all test suites)*.*\n*$/gm, ); diff --git a/e2e/__tests__/failures.test.ts b/e2e/__tests__/failures.test.ts index 056aa10e9626..b22a6bd11ad0 100644 --- a/e2e/__tests__/failures.test.ts +++ b/e2e/__tests__/failures.test.ts @@ -11,7 +11,7 @@ import runJest from '../runJest'; const dir = path.resolve(__dirname, '../failures'); -const normalizeDots = (text: string) => text.replace(/\.{1,}$/gm, '.'); +const normalizeDots = (text: string) => text.replace(/\.+$/gm, '.'); function cleanStderr(stderr: string) { const {rest} = extractSummary(stderr); @@ -65,7 +65,7 @@ test('works with async failures', () => { const result = normalizeDots(rest) .replace(/.*thrown:.*\n/, '') .replace( - /.*Add a timeout value to this test to increase the timeout, if this is a long-running test. See https:\/\/jestjs.io\/docs\/api#testname-fn-timeout..*/, + /.*Add a timeout value to this test to increase the timeout, if this is a long-running test. See https:\/\/jestjs.io\/docs\/api#testname-fn-timeout.+/, '', ) .replace(/.*Timeout - Async callback was not.*/, ''); diff --git a/e2e/__tests__/jasmineAsync.test.ts b/e2e/__tests__/jasmineAsync.test.ts index 37418d53955d..9e98e716bf8d 100644 --- a/e2e/__tests__/jasmineAsync.test.ts +++ b/e2e/__tests__/jasmineAsync.test.ts @@ -115,7 +115,7 @@ describe('async jasmine', () => { expect(json.numPendingTests).toBe(1); expect(json.testResults[0].message).toMatch(/concurrent test fails/); if (!isJestJasmineRun()) { - expect(stderr.match(/\[\[\w+\]\]/g)).toEqual([ + expect(stderr.match(/\[\[\w+]]/g)).toEqual([ '[[beforeAll]]', '[[test]]', '[[test]]', diff --git a/e2e/__tests__/runProgrammatically.test.ts b/e2e/__tests__/runProgrammatically.test.ts index 217cdf1fab19..828964881114 100644 --- a/e2e/__tests__/runProgrammatically.test.ts +++ b/e2e/__tests__/runProgrammatically.test.ts @@ -12,10 +12,10 @@ const dir = resolve(__dirname, '..', 'run-programmatically'); test('run Jest programmatically cjs', () => { const {stdout} = run('node cjs.js --version', dir); - expect(stdout).toMatch(/\d{2}\.\d{1,2}\.\d{1,2}[-\S]*-dev$/); + expect(stdout).toMatch(/\d{2}(?:\.\d{1,2}){2}\S*-dev$/); }); test('run Jest programmatically esm', () => { const {stdout} = run('node index.js --version', dir); - expect(stdout).toMatch(/\d{2}\.\d{1,2}\.\d{1,2}[-\S]*-dev$/); + expect(stdout).toMatch(/\d{2}(?:\.\d{1,2}){2}\S*-dev$/); }); diff --git a/e2e/__tests__/showConfig.test.ts b/e2e/__tests__/showConfig.test.ts index cc7d598f2788..a921def3f0ac 100644 --- a/e2e/__tests__/showConfig.test.ts +++ b/e2e/__tests__/showConfig.test.ts @@ -30,7 +30,7 @@ test('--showConfig outputs config info and exits', () => { stdout = stdout .replace(/\\\\node_modules\\\\/g, 'node_modules') - .replace(/\\\\\.pnp\\\\\.\[\^[/\\]+\]\+\$/g, '<>') + .replace(/\\\\\.pnp\\\\\.\[\^[/\\]+]\+\$/g, '<>') .replace(/\\\\(?:([^.]+?)|$)/g, '/$1') .replace(/"cacheDirectory": "(.+)"/g, '"cacheDirectory": "/tmp/jest"') .replace(/"id": "(.+)"/g, '"id": "[md5 hash]"') diff --git a/e2e/__tests__/stackTrace.test.ts b/e2e/__tests__/stackTrace.test.ts index 9aa61522caa2..1f2dfd0dfa69 100644 --- a/e2e/__tests__/stackTrace.test.ts +++ b/e2e/__tests__/stackTrace.test.ts @@ -19,9 +19,7 @@ describe('Stack Trace', () => { /ReferenceError: thisIsARuntimeError is not defined/, ); expect(stderr).toMatch(/> 10 \| thisIsARuntimeError\(\);/); - expect(stderr).toMatch( - /\s+at\s(?:.+?)\s\(__tests__\/runtimeError.test\.js/, - ); + expect(stderr).toMatch(/\s+at\s.+?\s\(__tests__\/runtimeError.test\.js/); }); it('does not print a stack trace for runtime errors when --noStackTrace is given', () => { @@ -37,7 +35,7 @@ describe('Stack Trace', () => { /ReferenceError: thisIsARuntimeError is not defined/, ); expect(stderr).not.toMatch( - /\s+at\s(?:.+?)\s\(__tests__\/runtimeError.test\.js/, + /\s+at\s.+?\s\(__tests__\/runtimeError.test\.js/, ); }); @@ -47,7 +45,7 @@ describe('Stack Trace', () => { expect(extractSummary(stderr).summary).toMatchSnapshot(); expect(exitCode).toBe(1); - expect(stderr).toMatch(/\s+at\s(?:.+?)\s\(__tests__\/stackTrace.test\.js/); + expect(stderr).toMatch(/\s+at\s.+?\s\(__tests__\/stackTrace.test\.js/); }); it('does not print a stack trace for matching errors when --noStackTrace is given', () => { @@ -59,9 +57,7 @@ describe('Stack Trace', () => { expect(extractSummary(stderr).summary).toMatchSnapshot(); expect(exitCode).toBe(1); - expect(stderr).not.toMatch( - /\s+at\s(?:.+?)\s\(__tests__\/stackTrace.test\.js/, - ); + expect(stderr).not.toMatch(/\s+at\s.+?\s\(__tests__\/stackTrace.test\.js/); }); it('prints a stack trace for errors', () => { @@ -73,16 +69,14 @@ describe('Stack Trace', () => { expect(stderr).toMatch(/this is unexpected\./); expect(stderr).toMatch(/this is a string\./); - expect(stderr).toMatch(/\s+at\s(?:.+?)\s\(__tests__\/testError.test\.js/); + expect(stderr).toMatch(/\s+at\s.+?\s\(__tests__\/testError.test\.js/); // Make sure we show Jest's jest-resolve as part of the stack trace expect(stderr).toMatch( /Cannot find module 'this-module-does-not-exist' from '__tests__\/testError\.test\.js'/, ); - expect(stderr).toMatch( - /\s+at\s(?:.+?)\s\((?:.+?)jest-resolve\/build\/index\.js/, - ); + expect(stderr).toMatch(/\s+at\s.+?\s\(.+?jest-resolve\/build\/index\.js/); }); it('prints a stack trace for errors without message in stack trace', () => { @@ -95,7 +89,7 @@ describe('Stack Trace', () => { expect(stderr).toMatch(/important message/); expect(stderr).toMatch( - /\s+at\s(?:.+?)\s\(__tests__\/stackTraceWithoutMessage.test\.js/, + /\s+at\s.+?\s\(__tests__\/stackTraceWithoutMessage.test\.js/, ); }); @@ -108,8 +102,6 @@ describe('Stack Trace', () => { expect(extractSummary(stderr).summary).toMatchSnapshot(); expect(exitCode).toBe(1); - expect(stderr).not.toMatch( - /\s+at\s(?:.+?)\s\(__tests__\/testError.test\.js/, - ); + expect(stderr).not.toMatch(/\s+at\s.+?\s\(__tests__\/testError.test\.js/); }); }); diff --git a/e2e/__tests__/version.test.ts b/e2e/__tests__/version.test.ts index 5e90d023fae1..1c7b24598220 100644 --- a/e2e/__tests__/version.test.ts +++ b/e2e/__tests__/version.test.ts @@ -22,7 +22,7 @@ test('works with jest.config.js', () => { }); const {exitCode, stdout, stderr} = runJest(DIR, ['--version']); - expect(stdout).toMatch(/\d{2}\.\d{1,2}\.\d{1,2}[-\S]*-dev$/); + expect(stdout).toMatch(/\d{2}(?:\.\d{1,2}){2}\S*-dev$/); // Only version gets printed and nothing else expect(stdout.split(/\n/)).toHaveLength(1); expect(stderr).toBe(''); diff --git a/e2e/snapshot-escape/__tests__/snapshotEscapeRegex.js b/e2e/snapshot-escape/__tests__/snapshotEscapeRegex.js index 4fd245e24b3a..74ea3cb2d38c 100644 --- a/e2e/snapshot-escape/__tests__/snapshotEscapeRegex.js +++ b/e2e/snapshot-escape/__tests__/snapshotEscapeRegex.js @@ -6,7 +6,7 @@ */ 'use strict'; -const regex = /\dd \s+ \w \\\[ \. blahzz.* [xyz]+/; +const regex = /\dd \s+ \w \\\[ \. blahzz.* [x-z]+/; test('escape regex', () => expect(regex).toMatchSnapshot()); diff --git a/packages/expect-utils/src/utils.ts b/packages/expect-utils/src/utils.ts index 24a3a7fdcfa2..cf3ff2aec4d5 100644 --- a/packages/expect-utils/src/utils.ts +++ b/packages/expect-utils/src/utils.ts @@ -503,7 +503,7 @@ export function emptyObject(obj: unknown): boolean { return obj && typeof obj === 'object' ? Object.keys(obj).length === 0 : false; } -const MULTILINE_REGEXP = /[\r\n]/; +const MULTILINE_REGEXP = /[\n\r]/; export const isOneline = (expected: unknown, received: unknown): boolean => typeof expected === 'string' && diff --git a/packages/expect/src/__tests__/__snapshots__/toThrowMatchers.test.ts.snap b/packages/expect/src/__tests__/__snapshots__/toThrowMatchers.test.ts.snap index 1c54deebd0e5..de7b0318643b 100644 --- a/packages/expect/src/__tests__/__snapshots__/toThrowMatchers.test.ts.snap +++ b/packages/expect/src/__tests__/__snapshots__/toThrowMatchers.test.ts.snap @@ -244,7 +244,7 @@ Received message: "apple" exports[`toThrow regexp threw, but message did not match (non-error falsey) 1`] = ` expect(received).toThrow(expected) -Expected pattern: /^[123456789]\\d*/ +Expected pattern: /^[1-9]\\d*/ Received value: 0 `; @@ -261,7 +261,7 @@ Received message: "Invalid array length" exports[`toThrow regexp threw, but message should not match (non-error truthy) 1`] = ` expect(received).not.toThrow(expected) -Expected pattern: not /^[123456789]\\d*/ +Expected pattern: not /^[1-9]\\d*/ Received value: 404 `; diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index 3e559ebeb12e..d800345a6a35 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -1817,7 +1817,7 @@ describe('.toMatch()', () => { }); it('does not maintain RegExp state between calls', () => { - const regex = /[f]\d+/gi; + const regex = /f\d+/gi; jestExpect('f123').toMatch(regex); jestExpect('F456').toMatch(regex); jestExpect(regex.lastIndex).toBe(0); diff --git a/packages/expect/src/__tests__/toThrowMatchers.test.ts b/packages/expect/src/__tests__/toThrowMatchers.test.ts index 04264f293d49..5a87f9b3ac90 100644 --- a/packages/expect/src/__tests__/toThrowMatchers.test.ts +++ b/packages/expect/src/__tests__/toThrowMatchers.test.ts @@ -121,7 +121,7 @@ describe('toThrow', () => { jestExpect(() => { // eslint-disable-next-line no-throw-literal throw 0; - }).toThrow(/^[123456789]\d*/); + }).toThrow(/^[1-9]\d*/); }).toThrowErrorMatchingSnapshot(); }); @@ -138,7 +138,7 @@ describe('toThrow', () => { jestExpect(() => { // eslint-disable-next-line no-throw-literal throw 404; - }).not.toThrow(/^[123456789]\d*/); + }).not.toThrow(/^[1-9]\d*/); }).toThrowErrorMatchingSnapshot(); }); }); diff --git a/packages/jest-cli/src/args.ts b/packages/jest-cli/src/args.ts index 7a66bafd1233..5e84991fd47d 100644 --- a/packages/jest-cli/src/args.ts +++ b/packages/jest-cli/src/args.ts @@ -77,14 +77,10 @@ export function check(argv: Config.Argv): true { if ( argv.config && !isJSONString(argv.config) && - !argv.config.match( - new RegExp( - `\\.(${constants.JEST_CONFIG_EXT_ORDER.map(e => e.slice(1)).join( - '|', - )})$`, - 'i', - ), - ) + !new RegExp( + `\\.(${constants.JEST_CONFIG_EXT_ORDER.map(e => e.slice(1)).join('|')})$`, + 'i', + ).test(argv.config) ) { throw new Error( `The --config option requires a JSON string literal, or a file path with one of these extensions: ${constants.JEST_CONFIG_EXT_ORDER.join( diff --git a/packages/jest-config/src/__tests__/normalize.test.ts b/packages/jest-config/src/__tests__/normalize.test.ts index 7b07c744f215..99199a4ab906 100644 --- a/packages/jest-config/src/__tests__/normalize.test.ts +++ b/packages/jest-config/src/__tests__/normalize.test.ts @@ -1100,7 +1100,7 @@ describe('preset', () => { ); const errorMessage = semver.satisfies(process.versions.node, '<19.0.0') - ? /Unexpected token } in JSON at position (104|110)[\s\S]* at / + ? /Unexpected token } in JSON at position (104|110)[\S\s]* at / : 'SyntaxError: Expected double-quoted property name in JSON at position 104'; await expect( @@ -1125,7 +1125,7 @@ describe('preset', () => { ); const errorMessage = semver.satisfies(process.versions.node, '<16.9.1') - ? /TypeError: Cannot read property 'call' of undefined[\s\S]* at / + ? /TypeError: Cannot read property 'call' of undefined[\S\s]* at / : "TypeError: Cannot read properties of undefined (reading 'call')"; await expect( diff --git a/packages/jest-config/src/stringToBytes.ts b/packages/jest-config/src/stringToBytes.ts index 93891f2366d3..eb07249c6350 100644 --- a/packages/jest-config/src/stringToBytes.ts +++ b/packages/jest-config/src/stringToBytes.ts @@ -33,7 +33,7 @@ function stringToBytes( if (isNaN(Number.parseFloat(input.slice(-1)))) { // eslint-disable-next-line prefer-const let [, numericString, trailingChars] = - input.match(/(.*?)([^0-9.-]+)$/i) || []; + input.match(/(.*?)([^\d.-]+)$/i) || []; if (trailingChars && numericString) { const numericValue = Number.parseFloat(numericString); diff --git a/packages/jest-config/src/utils.ts b/packages/jest-config/src/utils.ts index 3dac9816328f..220f57f1495d 100644 --- a/packages/jest-config/src/utils.ts +++ b/packages/jest-config/src/utils.ts @@ -52,7 +52,7 @@ export const resolve = ( }; export const escapeGlobCharacters = (path: string): string => - path.replace(/([()*{}[\]!?\\])/g, '\\$1'); + path.replace(/([!()*?[\\\]{}])/g, '\\$1'); export const replaceRootDirInPath = ( rootDir: string, diff --git a/packages/jest-docblock/src/index.ts b/packages/jest-docblock/src/index.ts index ae4525f4824b..ff9f94d08033 100644 --- a/packages/jest-docblock/src/index.ts +++ b/packages/jest-docblock/src/index.ts @@ -13,11 +13,11 @@ type Pragmas = Record>; const commentEndRe = /\*\/$/; const commentStartRe = /^\/\*\*?/; const docblockRe = /^\s*(\/\*\*?(.|\r?\n)*?\*\/)/; -const lineCommentRe = /(^|\s+)\/\/([^\r\n]*)/g; +const lineCommentRe = /(^|\s+)\/\/([^\n\r]*)/g; const ltrimNewlineRe = /^(\r?\n)+/; const multilineRe = - /(?:^|\r?\n) *(@[^\r\n]*?) *\r?\n *(?![^@\r\n]*\/\/[^]*)([^@\r\n\s][^@\r\n]+?) *\r?\n/g; -const propertyRe = /(?:^|\r?\n) *@(\S+) *([^\r\n]*)/g; + /(?:^|\r?\n) *(@[^\n\r]*?) *\r?\n *(?![^\n\r@]*\/\/[^]*)([^\s@][^\n\r@]+?) *\r?\n/g; +const propertyRe = /(?:^|\r?\n) *@(\S+) *([^\n\r]*)/g; const stringStartRe = /(\r?\n|^) *\* ?/g; const STRING_ARRAY: ReadonlyArray = []; diff --git a/packages/jest-each/src/table/array.ts b/packages/jest-each/src/table/array.ts index 1b1101c26a37..ed30c9eda58f 100644 --- a/packages/jest-each/src/table/array.ts +++ b/packages/jest-each/src/table/array.ts @@ -12,7 +12,7 @@ import {format as pretty} from 'pretty-format'; import type {EachTests} from '../bind'; import {type Templates, interpolateVariables} from './interpolation'; -const SUPPORTED_PLACEHOLDERS = /%[sdifjoOp#]/g; +const SUPPORTED_PLACEHOLDERS = /%[#Odfijops]/g; const PRETTY_PLACEHOLDER = '%p'; const INDEX_PLACEHOLDER = '%#'; const PLACEHOLDER_PREFIX = '%'; diff --git a/packages/jest-haste-map/src/__tests__/dependencyExtractor.js b/packages/jest-haste-map/src/__tests__/dependencyExtractor.js index 166b3d121798..27b071b0aa90 100644 --- a/packages/jest-haste-map/src/__tests__/dependencyExtractor.js +++ b/packages/jest-haste-map/src/__tests__/dependencyExtractor.js @@ -10,7 +10,7 @@ const blockCommentRe = /\/\*[^]*?\*\//g; const lineCommentRe = /\/\/.*/g; const LOAD_MODULE_RE = - /(?:^|[^.]\s*)(\bloadModule\s*?\(\s*?)([`'"])([^`'"]+)(\2\s*?\))/g; + /(?:^|[^.]\s*)(\bloadModule\s*?\(\s*?)(["'`])([^"'`]+)(\2\s*?\))/g; export function extract(code, filePath, defaultDependencyExtractor) { const dependencies = defaultDependencyExtractor(code); diff --git a/packages/jest-message-util/src/__tests__/__snapshots__/messages.test.ts.snap b/packages/jest-message-util/src/__tests__/__snapshots__/messages.test.ts.snap index 1daf195036db..49dc55e198bc 100644 --- a/packages/jest-message-util/src/__tests__/__snapshots__/messages.test.ts.snap +++ b/packages/jest-message-util/src/__tests__/__snapshots__/messages.test.ts.snap @@ -116,16 +116,16 @@ exports[`on node >=15.0.0 should return the inner errors of an AggregateError 1` AggregateError: - at Object. (packages/jest-message-util/src/__tests__/messages.test.ts:588:22) + at Object. (packages/jest-message-util/src/__tests__/messages.test.ts:586:22) Errors contained in AggregateError: Err 1 - at Object. (packages/jest-message-util/src/__tests__/messages.test.ts:589:7) + at Object. (packages/jest-message-util/src/__tests__/messages.test.ts:587:7) Err 2 - at Object. (packages/jest-message-util/src/__tests__/messages.test.ts:590:7) + at Object. (packages/jest-message-util/src/__tests__/messages.test.ts:588:7) " `; @@ -177,12 +177,12 @@ exports[`should return the error cause if there is one 1`] = ` Test exception - at Object. (packages/jest-message-util/src/__tests__/messages.test.ts:567:17) + at Object. (packages/jest-message-util/src/__tests__/messages.test.ts:565:17) Cause: Cause Error - at Object. (packages/jest-message-util/src/__tests__/messages.test.ts:570:17) + at Object. (packages/jest-message-util/src/__tests__/messages.test.ts:568:17) " `; diff --git a/packages/jest-message-util/src/__tests__/messages.test.ts b/packages/jest-message-util/src/__tests__/messages.test.ts index 9916b9c58096..3f3528c9c45a 100644 --- a/packages/jest-message-util/src/__tests__/messages.test.ts +++ b/packages/jest-message-util/src/__tests__/messages.test.ts @@ -36,7 +36,7 @@ const unixStackTrace = at Object.it (build/__tests__/messages-test.js:45:41) at Object. (../jest-jasmine2/build/jasmine-pit.js:35:32) at attemptAsync (../jest-jasmine2/build/jasmine-2.4.1.js:1919:24)`; -const unixError = new Error(unixStackTrace.replace(/\n\s*at [\s\s]*/m, '')); +const unixError = new Error(unixStackTrace.replace(/\n\s*at \s*/m, '')); unixError.stack = unixStackTrace; const assertionStack = @@ -59,9 +59,7 @@ const assertionStack = at process._tickCallback (internal/process/next_tick.js:188:7) at internal/process/next_tick.js:188:7 `; -const assertionError = new Error( - assertionStack.replace(/\n\s*at [\s\s]*/m, ''), -); +const assertionError = new Error(assertionStack.replace(/\n\s*at \s*/m, '')); assertionError.stack = assertionStack; const vendorStack = @@ -90,7 +88,7 @@ const babelStack = \u001b[90m 22 | \u001b[39m )\u001b[33m;\u001b[39m \u001b[90m 23 | \u001b[39m } \u001b[36melse\u001b[39m \u001b[36mif\u001b[39m (\u001b[36mtypeof\u001b[39m render \u001b[33m!==\u001b[39m \u001b[32m'function'\u001b[39m) {\u001b[0m `; -const babelError = new Error(babelStack.replace(/\n\s*at [\s\s]*/m, '')); +const babelError = new Error(babelStack.replace(/\n\s*at \s*/m, '')); babelError.stack = babelStack; function buildErrorWithCause(message: string, opts: {cause: unknown}): Error { diff --git a/packages/jest-message-util/src/index.ts b/packages/jest-message-util/src/index.ts index 01b0b80c44b7..055f611c4ba3 100644 --- a/packages/jest-message-util/src/index.ts +++ b/packages/jest-message-util/src/index.ts @@ -74,7 +74,7 @@ const trim = (string: string) => (string || '').trim(); // want to trim those, because they may have pointers to the column/character // which will get misaligned. const trimPaths = (string: string) => - string.match(STACK_PATH_REGEXP) ? trim(string) : string; + STACK_PATH_REGEXP.test(string) ? trim(string) : string; const getRenderedCallsite = ( fileContent: string, @@ -292,7 +292,7 @@ export const formatPath = ( relativeTestPath: string | null = null, ): string => { // Extract the file path from the trace line. - const match = line.match(/(^\s*at .*?\(?)([^()]+)(:[0-9]+:[0-9]+\)?.*$)/); + const match = line.match(/(^\s*at .*?\(?)([^()]+)(:\d+:\d+\)?.*$)/); if (!match) { return line; } @@ -528,7 +528,7 @@ export const separateMessageFromStack = ( // If the error is a plain "Error:" instead of a SyntaxError or TypeError we // remove the prefix from the message because it is generally not useful. const messageMatch = content.match( - /^(?:Error: )?([\s\S]*?(?=\n\s*at\s.*:\d*:\d*)|\s*.*)([\s\S]*)$/, + /^(?:Error: )?([\S\s]*?(?=\n\s*at\s.*:\d*:\d*)|\s*.*)([\S\s]*)$/, ); if (!messageMatch) { // For typescript diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts index 1ab36498fb19..7b786d3af4ed 100644 --- a/packages/jest-mock/src/index.ts +++ b/packages/jest-mock/src/index.ts @@ -1047,7 +1047,7 @@ export class ModuleMocker { if ( type === 'function' && this.isMockFunction(component) && - slot.match(/^mock/) + slot.startsWith('mock') ) { continue; } diff --git a/packages/jest-regex-util/src/index.ts b/packages/jest-regex-util/src/index.ts index 71d77377469d..9aaaf77a58d0 100644 --- a/packages/jest-regex-util/src/index.ts +++ b/packages/jest-regex-util/src/index.ts @@ -18,12 +18,12 @@ export const escapePathForRegex = (dir: string): string => { }; export const escapeStrForRegex = (string: string): string => - string.replace(/[[\]{}()*+?.\\^$|]/g, '\\$&'); + string.replace(/[$()*+.?[\\\]^{|}]/g, '\\$&'); export const replacePathSepForRegex = (string: string): string => { if (sep === '\\') { return string.replace( - /(\/|(.)?\\(?![[\]{}()*+?.^$|\\]))/g, + /(\/|(.)?\\(?![$()*+.?[\\\]^{|}]))/g, (_match, _, p2) => (p2 && p2 !== '\\' ? `${p2}\\\\` : '\\\\'), ); } diff --git a/packages/jest-repl/src/cli/repl.ts b/packages/jest-repl/src/cli/repl.ts index 0039fa56a7c3..7b8356c98f75 100644 --- a/packages/jest-repl/src/cli/repl.ts +++ b/packages/jest-repl/src/cli/repl.ts @@ -108,7 +108,7 @@ const replInstance: repl.REPLServer = repl.start({ }); replInstance.context.require = (moduleName: string) => { - if (/(\/|\\|\.)/.test(moduleName)) { + if (/([./\\])/.test(moduleName)) { moduleName = path.resolve(process.cwd(), moduleName); } return require(moduleName) as unknown; diff --git a/packages/jest-resolve/src/resolver.ts b/packages/jest-resolve/src/resolver.ts index f8187135a4b2..e4be68f5fdb0 100644 --- a/packages/jest-resolve/src/resolver.ts +++ b/packages/jest-resolve/src/resolver.ts @@ -438,7 +438,7 @@ export default class Resolver { return matches ? (moduleName: string) => moduleName.replace( - /\$([0-9]+)/g, + /\$(\d+)/g, (_, index) => matches[parseInt(index, 10)] || '', ) : (moduleName: string) => moduleName; diff --git a/packages/jest-transform/src/shouldInstrument.ts b/packages/jest-transform/src/shouldInstrument.ts index 56c44b9aac22..527261992262 100644 --- a/packages/jest-transform/src/shouldInstrument.ts +++ b/packages/jest-transform/src/shouldInstrument.ts @@ -80,7 +80,9 @@ export default function shouldInstrument( } if ( - config.coveragePathIgnorePatterns.some(pattern => !!filename.match(pattern)) + config.coveragePathIgnorePatterns.some(pattern => + new RegExp(pattern).test(filename), + ) ) { return false; } diff --git a/packages/jest-util/src/TestPathPatterns.ts b/packages/jest-util/src/TestPathPatterns.ts index 2c5856e4932a..558f4a68e99b 100644 --- a/packages/jest-util/src/TestPathPatterns.ts +++ b/packages/jest-util/src/TestPathPatterns.ts @@ -35,12 +35,12 @@ export default class TestPathPatterns { const regexString = this.patterns .map(p => { // absolute paths passed on command line should stay same - if (p.match(/^\//)) { + if (/^\//.test(p)) { return p; } // explicit relative paths should resolve against rootDir - if (p.match(/^\.\//)) { + if (/^\.\//.test(p)) { return p.replace(/^\.\//, rootDirRegex); } diff --git a/packages/jest-util/src/replacePathSepForGlob.ts b/packages/jest-util/src/replacePathSepForGlob.ts index 3a0ca627e62d..aaa13199209d 100644 --- a/packages/jest-util/src/replacePathSepForGlob.ts +++ b/packages/jest-util/src/replacePathSepForGlob.ts @@ -6,5 +6,5 @@ */ export default function replacePathSepForGlob(path: string): string { - return path.replace(/\\(?![{}()+?.^$])/g, '/'); + return path.replace(/\\(?![$()+.?^{}])/g, '/'); } diff --git a/packages/pretty-format/src/index.ts b/packages/pretty-format/src/index.ts index d730c36818d1..a2c19cac3ac8 100644 --- a/packages/pretty-format/src/index.ts +++ b/packages/pretty-format/src/index.ts @@ -183,7 +183,7 @@ function printBasicValue( if (toStringed === '[object RegExp]') { if (escapeRegex) { // https://github.com/benjamingr/RegExp.escape/blob/main/polyfill.js - return regExpToString.call(val).replace(/[\\^$*+?.()|[\]{}]/g, '\\$&'); + return regExpToString.call(val).replace(/[$()*+.?[\\\]^{|}]/g, '\\$&'); } return regExpToString.call(val); } diff --git a/scripts/checkChangelog.mjs b/scripts/checkChangelog.mjs index 7070ff6eea21..9eb9b738acc6 100644 --- a/scripts/checkChangelog.mjs +++ b/scripts/checkChangelog.mjs @@ -8,7 +8,7 @@ import fs from 'graceful-fs'; const linkRegex = - /\[#(\d+)\]\(https:\/\/github.com\/facebook\/jest\/(issues|pull)\/(\d+)\)/g; + /\[#(\d+)]\(https:\/\/github.com\/facebook\/jest\/(issues|pull)\/(\d+)\)/g; const changelogPath = 'CHANGELOG.md'; const data = fs.readFileSync(changelogPath, 'utf-8');