Skip to content

Commit

Permalink
feat(config): add support for multiple paths in module name mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
OrkhanAlikhanov committed May 27, 2020
1 parent 0f93378 commit 5017eba
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
16 changes: 9 additions & 7 deletions src/config/paths-to-module-name-mapper.spec.ts
Expand Up @@ -9,7 +9,7 @@ const tsconfigMap = {
'api/*': ['src/api/*'],
'test/*': ['test/*'],
'mocks/*': ['test/mocks/*'],
'test/*/mock': ['test/mocks/*'],
'test/*/mock': ['test/mocks/*', 'test/__mocks__/*'],
}

describe('pathsToModuleNameMapper', () => {
Expand All @@ -21,7 +21,10 @@ Object {
"^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",
}
`)
Expand All @@ -35,7 +38,10 @@ Object {
"^mocks/(.*)$": "<rootDir>/test/mocks/$1",
"^server$": "<rootDir>/src/server",
"^test/(.*)$": "<rootDir>/test/$1",
"^test/(.*)/mock$": "<rootDir>/test/mocks/$1",
"^test/(.*)/mock$": Array [
"<rootDir>/test/mocks/$1",
"<rootDir>/test/__mocks__/$1",
],
"^util/(.*)$": "<rootDir>/src/util/$1",
}
`)
Expand All @@ -48,20 +54,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 (\`*\`).
",
Expand Down
14 changes: 4 additions & 10 deletions src/config/paths-to-module-name-mapper.ts
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/util/messages.ts
Expand Up @@ -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.',
Expand Down

0 comments on commit 5017eba

Please sign in to comment.