Skip to content

Commit 56793ab

Browse files
authored
fix(@jest/resolve): replace unmatched capture group with empty string instead of "undefined" (#14507)
1 parent b7828e9 commit 56793ab

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
### Fixes
88

9+
- `[jest-resolver]` Replace unmatched capture groups in `moduleNameMapper` with empty string instead of `undefined` ([#14507](https://github.com/jestjs/jest/pull/14507))
910
- `[jest-snapshot]` Allow for strings as well as template literals in inline snapshots ([#14465](https://github.com/jestjs/jest/pull/14465))
1011
- `[@jest/test-sequencer]` Calculate test runtime if `perStats.duration` is missing ([#14473](https://github.com/jestjs/jest/pull/14473))
1112

packages/jest-resolve/src/__mocks__/foo/bar/index.js

Whitespace-only changes.

packages/jest-resolve/src/__tests__/resolve.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,25 @@ describe('resolveModule', () => {
561561
expect(mockUserResolver).toHaveBeenCalled();
562562
expect(mockUserResolver.mock.calls[0][0]).toBe('fs');
563563
});
564+
565+
it('handles unmatched capture groups correctly', () => {
566+
const resolver = new Resolver(moduleMap, {
567+
extensions: ['.js'],
568+
moduleNameMapper: [
569+
{
570+
moduleName: './__mocks__/foo$1',
571+
regex: /^@Foo(\/.*)?$/,
572+
},
573+
],
574+
} as ResolverConfig);
575+
const src = require.resolve('../');
576+
expect(resolver.resolveModule(src, '@Foo')).toBe(
577+
require.resolve('../__mocks__/foo.js'),
578+
);
579+
expect(resolver.resolveModule(src, '@Foo/bar')).toBe(
580+
require.resolve('../__mocks__/foo/bar/index.js'),
581+
);
582+
});
564583
});
565584

566585
describe('resolveModuleAsync', () => {

packages/jest-resolve/src/resolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ export default class Resolver {
439439
? (moduleName: string) =>
440440
moduleName.replace(
441441
/\$([0-9]+)/g,
442-
(_, index) => matches[parseInt(index, 10)],
442+
(_, index) => matches[parseInt(index, 10)] || '',
443443
)
444444
: (moduleName: string) => moduleName;
445445
}

0 commit comments

Comments
 (0)