diff --git a/src/config/paths-to-module-name-mapper.spec.ts b/src/config/paths-to-module-name-mapper.spec.ts index 34b8210ad3..a88301355c 100644 --- a/src/config/paths-to-module-name-mapper.spec.ts +++ b/src/config/paths-to-module-name-mapper.spec.ts @@ -5,11 +5,12 @@ import { pathsToModuleNameMapper } from './paths-to-module-name-mapper' const tsconfigMap = { log: ['src/util/log'], server: ['src/server'], + client: ['src/client', 'src/client/index'], 'util/*': ['src/util/*'], 'api/*': ['src/api/*'], 'test/*': ['test/*'], 'mocks/*': ['test/mocks/*'], - 'test/*/mock': ['test/mocks/*'], + 'test/*/mock': ['test/mocks/*', 'test/__mocks__/*'], } describe('pathsToModuleNameMapper', () => { @@ -17,11 +18,18 @@ describe('pathsToModuleNameMapper', () => { expect(pathsToModuleNameMapper(tsconfigMap)).toMatchInlineSnapshot(` Object { "^api/(.*)$": "src/api/$1", + "^client$": Array [ + "src/client", + "src/client/index", + ], "^log$": "src/util/log", "^mocks/(.*)$": "test/mocks/$1", "^server$": "src/server", "^test/(.*)$": "test/$1", - "^test/(.*)/mock$": "test/mocks/$1", + "^test/(.*)/mock$": Array [ + "test/mocks/$1", + "test/__mocks__/$1", + ], "^util/(.*)$": "src/util/$1", } `) @@ -31,11 +39,18 @@ Object { expect(pathsToModuleNameMapper(tsconfigMap, { prefix: '/' })).toMatchInlineSnapshot(` Object { "^api/(.*)$": "/src/api/$1", + "^client$": Array [ + "/src/client", + "/src/client/index", + ], "^log$": "/src/util/log", "^mocks/(.*)$": "/test/mocks/$1", "^server$": "/src/server", "^test/(.*)$": "/test/$1", - "^test/(.*)/mock$": "/test/mocks/$1", + "^test/(.*)/mock$": Array [ + "/test/mocks/$1", + "/test/__mocks__/$1", + ], "^util/(.*)$": "/src/util/$1", } `) @@ -48,20 +63,16 @@ Object { pathsToModuleNameMapper({ kept: ['src/kept'], 'no-target': [], - 'too-many-target': ['one', 'two'], 'too/*/many/*/stars': ['to/*/many/*/stars'], }), ).toMatchInlineSnapshot(` Object { "^kept$": "src/kept", - "^too\\\\-many\\\\-target$": "one", } `) expect(log.lines.warn).toMatchInlineSnapshot(` Array [ "[level:40] Not mapping \\"no-target\\" because it has no target. -", - "[level:40] Mapping only to first target of \\"too-many-target\\" because it has more than one (2). ", "[level:40] Not mapping \\"too/*/many/*/stars\\" because it has more than one star (\`*\`). ", diff --git a/src/config/paths-to-module-name-mapper.ts b/src/config/paths-to-module-name-mapper.ts index d41f3fc322..e06a03831f 100644 --- a/src/config/paths-to-module-name-mapper.ts +++ b/src/config/paths-to-module-name-mapper.ts @@ -26,24 +26,18 @@ export const pathsToModuleNameMapper = ( if (toPaths.length === 0) { logger.warn(interpolate(Errors.NotMappingPathWithEmptyMap, { path: fromPath })) continue - } else if (toPaths.length > 1) { - logger.warn( - interpolate(Errors.MappingOnlyFirstTargetOfPath, { - path: fromPath, - count: toPaths.length, - }), - ) } - const target = toPaths[0] // split with '*' const segments = fromPath.split(/\*/g) if (segments.length === 1) { + const paths = toPaths.map((target) => `${prefix}${target}`) pattern = `^${escapeRegex(fromPath)}$` - jestMap[pattern] = `${prefix}${target}` + jestMap[pattern] = paths.length === 1 ? paths[0] : paths } else if (segments.length === 2) { + const paths = toPaths.map((target) => `${prefix}${target.replace(/\*/g, '$1')}`) pattern = `^${escapeRegex(segments[0])}(.*)${escapeRegex(segments[1])}$` - jestMap[pattern] = `${prefix}${target.replace(/\*/g, '$1')}` + jestMap[pattern] = paths.length === 1 ? paths[0] : paths } else { logger.warn(interpolate(Errors.NotMappingMultiStarPath, { path: fromPath })) continue diff --git a/src/util/messages.ts b/src/util/messages.ts index fc3873179a..b62fe8b8f8 100644 --- a/src/util/messages.ts +++ b/src/util/messages.ts @@ -12,7 +12,6 @@ export const enum Errors { UnableToCompileTypeScript = '{{diagnostics}}', NotMappingMultiStarPath = 'Not mapping "{{path}}" because it has more than one star (`*`).', NotMappingPathWithEmptyMap = 'Not mapping "{{path}}" because it has no target.', - MappingOnlyFirstTargetOfPath = 'Mapping only to first target of "{{path}}" because it has more than one ({{count}}).', GotJsFileButAllowJsFalse = 'Got a `.js` file to compile while `allowJs` option is not set to `true` (file: {{path}}). To fix this:\n - if you want TypeScript to process JS files, set `allowJs` to `true` in your TypeScript config (usually tsconfig.json)\n - if you do not want TypeScript to process your `.js` files, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match `.js` files anymore', GotUnknownFileTypeWithoutBabel = 'Got a unknown file type to compile (file: {{path}}). To fix this, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match this kind of files anymore.', GotUnknownFileTypeWithBabel = 'Got a unknown file type to compile (file: {{path}}). To fix this, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match this kind of files anymore. If you still want Babel to process it, add another entry to the `transform` option with value `babel-jest` which key matches this type of files.',