Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@jest/resolve): replace unmatched capture group with empty string instead of "undefined" (#14500) #14507

Merged
merged 3 commits into from Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

### Fixes

- `[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))

Expand Down
Empty file.
19 changes: 19 additions & 0 deletions packages/jest-resolve/src/__tests__/resolve.test.ts
Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-resolve/src/resolver.ts
Expand Up @@ -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;
}
Expand Down