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 811734a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
25 changes: 18 additions & 7 deletions src/config/paths-to-module-name-mapper.spec.ts
Expand Up @@ -5,23 +5,31 @@ 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', () => {
it('should convert tsconfig mapping', () => {
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",
}
`)
Expand All @@ -31,11 +39,18 @@ Object {
expect(pathsToModuleNameMapper(tsconfigMap, { prefix: '<rootDir>/' })).toMatchInlineSnapshot(`
Object {
"^api/(.*)$": "<rootDir>/src/api/$1",
"^client$": Array [
"<rootDir>/src/client",
"<rootDir>/src/client/index",
],
"^log$": "<rootDir>/src/util/log",
"^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 +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 (\`*\`).
",
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 811734a

Please sign in to comment.