-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): adds a helper to build moduleNameMapper from paths
Closes #364
- Loading branch information
Showing
6 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { pathsToModuleNameMapper } from './paths-to-module-name-mapper' | ||
import { logTargetMock } from '../__helpers__/mocks' | ||
|
||
const tsconfigMap = { | ||
log: ['src/util/log'], | ||
server: ['src/server'], | ||
'util/*': ['src/util/*'], | ||
'api/*': ['src/api/*'], | ||
'test/*': ['test/*'], | ||
'mocks/*': ['test/mocks/*'], | ||
'test/*/mock': ['test/mocks/*'], | ||
} | ||
|
||
describe('pathsToModuleNameMapper', () => { | ||
it('should convert tsconfig mapping', () => { | ||
expect(pathsToModuleNameMapper(tsconfigMap)).toMatchInlineSnapshot(` | ||
Object { | ||
"^api/(.*)$": "src/api/$1", | ||
"^log$": "src/util/log", | ||
"^mocks/(.*)$": "test/mocks/$1", | ||
"^server$": "src/server", | ||
"^test/(.*)$": "test/$1", | ||
"^test/(.*)/mock$": "test/mocks/$1", | ||
"^util/(.*)$": "src/util/$1", | ||
} | ||
`) | ||
}) | ||
|
||
it('should use the given prefix', () => { | ||
expect(pathsToModuleNameMapper(tsconfigMap, { prefix: '<rootDir>/' })) | ||
.toMatchInlineSnapshot(` | ||
Object { | ||
"^api/(.*)$": "<rootDir>/src/api/$1", | ||
"^log$": "<rootDir>/src/util/log", | ||
"^mocks/(.*)$": "<rootDir>/test/mocks/$1", | ||
"^server$": "<rootDir>/src/server", | ||
"^test/(.*)$": "<rootDir>/test/$1", | ||
"^test/(.*)/mock$": "<rootDir>/test/mocks/$1", | ||
"^util/(.*)$": "<rootDir>/src/util/$1", | ||
} | ||
`) | ||
}) | ||
|
||
it('should warn about mapping it cannot handle', () => { | ||
const log = logTargetMock() | ||
log.clear() | ||
expect( | ||
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\\" becuase it has more than one (2). | ||
", | ||
"[level:40] Not mapping \\"too/*/many/*/stars\\" because it has more than one star (\`*\`). | ||
", | ||
] | ||
`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { CompilerOptions } from 'typescript' | ||
import { rootLogger } from '../util/logger' | ||
import { interpolate, Errors } from '../util/messages' | ||
import { LogContexts } from 'bs-logger' | ||
|
||
type TsPathMapping = Exclude<CompilerOptions['paths'], undefined> | ||
type JestPathMapping = jest.InitialOptions['moduleNameMapper'] | ||
|
||
// we don't need to escape all chars, so commented out is the real one | ||
// const escapeRegex = (str: string) => str.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') | ||
const escapeRegex = (str: string) => str.replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&') | ||
|
||
const logger = rootLogger.child({ [LogContexts.namespace]: 'path-mapper' }) | ||
|
||
export const pathsToModuleNameMapper = ( | ||
mapping: TsPathMapping, | ||
{ prefix = '' }: { prefix?: string } = {}, | ||
): JestPathMapping => { | ||
const jestMap: JestPathMapping = {} | ||
for (const fromPath of Object.keys(mapping)) { | ||
let pattern: string | ||
const toPaths = mapping[fromPath] | ||
// check that we have only one target path | ||
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) { | ||
pattern = `^${escapeRegex(fromPath)}$` | ||
jestMap[pattern] = `${prefix}${target}` | ||
} else if (segments.length === 2) { | ||
pattern = `^${escapeRegex(segments[0])}(.*)${escapeRegex(segments[1])}$` | ||
jestMap[pattern] = `${prefix}${target.replace(/\*/g, '$1')}` | ||
} else { | ||
logger.warn( | ||
interpolate(Errors.NotMappingMultiStarPath, { path: fromPath }), | ||
) | ||
continue | ||
} | ||
} | ||
return jestMap | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters