From bdc6834c52dc72ba94df94394aa854a21d52bc18 Mon Sep 17 00:00:00 2001 From: Niklas Holm Date: Sun, 10 Sep 2023 20:04:53 +0000 Subject: [PATCH 1/3] fix(@jest/resolve): replace unmatched capture group with empty string instead of "undefined" (#14500) --- .../src/__mocks__/foo/bar/index.js | 0 .../src/__tests__/resolve.test.ts | 19 +++++++++++++++++++ packages/jest-resolve/src/resolver.ts | 2 +- 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 packages/jest-resolve/src/__mocks__/foo/bar/index.js diff --git a/packages/jest-resolve/src/__mocks__/foo/bar/index.js b/packages/jest-resolve/src/__mocks__/foo/bar/index.js new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/jest-resolve/src/__tests__/resolve.test.ts b/packages/jest-resolve/src/__tests__/resolve.test.ts index 1bc9c447f190..a027d1065aab 100644 --- a/packages/jest-resolve/src/__tests__/resolve.test.ts +++ b/packages/jest-resolve/src/__tests__/resolve.test.ts @@ -561,6 +561,25 @@ describe('resolveModule', () => { expect(mockUserResolver).toHaveBeenCalled(); expect(mockUserResolver.mock.calls[0][0]).toBe('fs'); }); + + it('handles unmatched capture groups correctly', () => { + const resolver = new Resolver(moduleMap, { + extensions: ['.js'], + moduleNameMapper: [ + { + moduleName: './__mocks__/foo$1', + regex: /^@Foo(\/.*)?$/, + }, + ], + } as ResolverConfig); + const src = require.resolve('../'); + expect(resolver.resolveModule(src, '@Foo')).toBe( + require.resolve('../__mocks__/foo.js'), + ); + expect(resolver.resolveModule(src, '@Foo/bar')).toBe( + require.resolve('../__mocks__/foo/bar/index.js'), + ); + }); }); describe('resolveModuleAsync', () => { diff --git a/packages/jest-resolve/src/resolver.ts b/packages/jest-resolve/src/resolver.ts index 7f378e859a19..359acc774faf 100644 --- a/packages/jest-resolve/src/resolver.ts +++ b/packages/jest-resolve/src/resolver.ts @@ -439,7 +439,7 @@ export default class Resolver { ? (moduleName: string) => moduleName.replace( /\$([0-9]+)/g, - (_, index) => matches[parseInt(index, 10)], + (_, index) => matches[parseInt(index, 10)] || '', ) : (moduleName: string) => moduleName; } From b682e83748e33a9024a46577414840b5a2b265d1 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 11 Sep 2023 09:13:32 +0200 Subject: [PATCH 2/3] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ce2bc5c5d9c..f4a8581ce8e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ ### Fixes +- `[jest-resolver]` Replace unmatched capture group with empty string instead of `undefined` ([#14507](https://github.com/jestjs/jest/pull/14507)) - `[jest-snapshot]` Allow for strings as well as template literals in inline snapshots ([#14465](https://github.com/jestjs/jest/pull/14465)) - `[@jest/test-sequencer]` Calculate test runtime if `perStats.duration` is missing ([#14473](https://github.com/jestjs/jest/pull/14473)) From 4d2fe805911071d205a17a8b8ae381c8c49468d5 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 11 Sep 2023 09:21:17 +0200 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4a8581ce8e4..676e0753208a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ ### Fixes -- `[jest-resolver]` Replace unmatched capture group with empty string instead of `undefined` ([#14507](https://github.com/jestjs/jest/pull/14507)) +- `[jest-resolver]` Replace unmatched capture groups in `moduleNameMapper` with empty string instead of `undefined` ([#14507](https://github.com/jestjs/jest/pull/14507)) - `[jest-snapshot]` Allow for strings as well as template literals in inline snapshots ([#14465](https://github.com/jestjs/jest/pull/14465)) - `[@jest/test-sequencer]` Calculate test runtime if `perStats.duration` is missing ([#14473](https://github.com/jestjs/jest/pull/14473))