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(config): fix pathsToModuleNameMapper with ./ in tsconfig paths #2797

Merged
merged 1 commit into from Aug 3, 2021
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
Expand Up @@ -3,6 +3,7 @@
exports[`pathsToModuleNameMapper should convert tsconfig mapping with given prefix: <rootDir>/ 1`] = `
Object {
"^@foo\\\\-bar/common$": "<rootDir>/../common/dist/library",
"^@pkg/(.*)$": "<rootDir>/packages/$1",
"^api/(.*)$": "<rootDir>/src/api/$1",
"^client$": Array [
"<rootDir>/src/client",
Expand All @@ -23,6 +24,7 @@ Object {
exports[`pathsToModuleNameMapper should convert tsconfig mapping with given prefix: foo 1`] = `
Object {
"^@foo\\\\-bar/common$": "foo/../common/dist/library",
"^@pkg/(.*)$": "foo/packages/$1",
"^api/(.*)$": "foo/src/api/$1",
"^client$": Array [
"foo/src/client",
Expand Down
44 changes: 23 additions & 21 deletions src/config/paths-to-module-name-mapper.spec.ts
Expand Up @@ -12,36 +12,38 @@ const tsconfigMap = {
'mocks/*': ['test/mocks/*'],
'test/*/mock': ['test/mocks/*', 'test/__mocks__/*'],
'@foo-bar/common': ['../common/dist/library'],
'@pkg/*': ['./packages/*'],
}

describe('pathsToModuleNameMapper', () => {
it('should convert tsconfig mapping with no given prefix', () => {
test('should convert tsconfig mapping with no given prefix', () => {
expect(pathsToModuleNameMapper(tsconfigMap)).toMatchInlineSnapshot(`
Object {
"^@foo\\\\-bar/common$": "../common/dist/library",
"^api/(.*)$": "src/api/$1",
"^client$": Array [
"src/client",
"src/client/index",
],
"^log$": "src/utils/log",
"^mocks/(.*)$": "test/mocks/$1",
"^server$": "src/server",
"^test/(.*)$": "test/$1",
"^test/(.*)/mock$": Array [
"test/mocks/$1",
"test/__mocks__/$1",
],
"^util/(.*)$": "src/utils/$1",
}
`)
Object {
"^@foo\\\\-bar/common$": "../common/dist/library",
"^@pkg/(.*)$": "./packages/$1",
"^api/(.*)$": "src/api/$1",
"^client$": Array [
"src/client",
"src/client/index",
],
"^log$": "src/utils/log",
"^mocks/(.*)$": "test/mocks/$1",
"^server$": "src/server",
"^test/(.*)$": "test/$1",
"^test/(.*)/mock$": Array [
"test/mocks/$1",
"test/__mocks__/$1",
],
"^util/(.*)$": "src/utils/$1",
}
`)
})

it.each(['<rootDir>/', 'foo'])('should convert tsconfig mapping with given prefix', (prefix) => {
test.each(['<rootDir>/', 'foo'])('should convert tsconfig mapping with given prefix', (prefix) => {
expect(pathsToModuleNameMapper(tsconfigMap, { prefix })).toMatchSnapshot(prefix)
})

it('should warn about mapping it cannot handle', () => {
test('should warn about mapping it cannot handle', () => {
const log = logTargetMock()
log.clear()
expect(
Expand Down
4 changes: 3 additions & 1 deletion src/config/paths-to-module-name-mapper.ts
Expand Up @@ -41,9 +41,11 @@ export const pathsToModuleNameMapper = (
jestMap[pattern] = paths.length === 1 ? paths[0] : paths
} else if (segments.length === 2) {
const paths = toPaths.map((target) => {
const enrichedTarget =
target.startsWith('./') && prefix !== '' ? target.substring(target.indexOf('/') + 1) : target
const enrichedPrefix = prefix !== '' && !prefix.endsWith('/') ? `${prefix}/` : prefix

return `${enrichedPrefix}${target.replace(/\*/g, '$1')}`
return `${enrichedPrefix}${enrichedTarget.replace(/\*/g, '$1')}`
})
pattern = `^${escapeRegex(segments[0])}(.*)${escapeRegex(segments[1])}$`
jestMap[pattern] = paths.length === 1 ? paths[0] : paths
Expand Down