Skip to content

Commit

Permalink
fix(@jest/resolve): replace unmatched capture group with empty string…
Browse files Browse the repository at this point in the history
… instead of "undefined" (#14507)
  • Loading branch information
niklasholm committed Sep 11, 2023
1 parent b7828e9 commit 56793ab
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 56793ab

Please sign in to comment.