Skip to content

Commit

Permalink
feat: add removeJsExtension to pathsToModuleNameMapper
Browse files Browse the repository at this point in the history
  • Loading branch information
jjangga0214 committed Sep 4, 2022
1 parent 912a3d1 commit b89c098
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
26 changes: 26 additions & 0 deletions src/config/paths-to-module-name-mapper.spec.ts
Expand Up @@ -39,6 +39,32 @@ describe('pathsToModuleNameMapper', () => {
`)
})

test('removeJsExtension', () => {
expect(pathsToModuleNameMapper(tsconfigMap, { removeJsExtension: true })).toEqual({
/**
* Why not using snapshot here?
* Because the snapshot does not guarantee property order, which is important for jest.
* A pattern ending with `\\.js` should appear before another pattern without the extension does.
*/
'^log$': 'src/utils/log',
'^server$': 'src/server',
'^client$': ['src/client', 'src/client/index'],
'^util/(.*)\\.js$': 'src/utils/$1',
'^util/(.*)$': 'src/utils/$1',
'^api/(.*)\\.js$': 'src/api/$1',
'^api/(.*)$': 'src/api/$1',
'^test/(.*)\\.js$': 'test/$1',
'^test/(.*)$': 'test/$1',
'^mocks/(.*)\\.js$': 'test/mocks/$1',
'^mocks/(.*)$': 'test/mocks/$1',
'^test/(.*)/mock\\.js$': ['test/mocks/$1', 'test/__mocks__/$1'],
'^test/(.*)/mock$': ['test/mocks/$1', 'test/__mocks__/$1'],
'^@foo\\-bar/common$': '../common/dist/library',
'^@pkg/(.*)\\.js$': './packages/$1',
'^@pkg/(.*)$': './packages/$1',
})
})

test.each(['<rootDir>/', 'foo'])('should convert tsconfig mapping with given prefix', (prefix) => {
expect(pathsToModuleNameMapper(tsconfigMap, { prefix })).toMatchSnapshot(prefix)
})
Expand Down
12 changes: 8 additions & 4 deletions src/config/paths-to-module-name-mapper.ts
Expand Up @@ -16,11 +16,10 @@ const logger = rootLogger.child({ [LogContexts.namespace]: 'path-mapper' })

export const pathsToModuleNameMapper = (
mapping: TsPathMapping,
{ prefix = '' }: { prefix: string } = Object.create(null),
{ prefix = '', removeJsExtension = false }: { prefix?: string; removeJsExtension?: boolean } = {},
): 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) {
Expand All @@ -37,7 +36,7 @@ export const pathsToModuleNameMapper = (

return `${enrichedPrefix}${target}`
})
pattern = `^${escapeRegex(fromPath)}$`
const pattern = `^${escapeRegex(fromPath)}$`
jestMap[pattern] = paths.length === 1 ? paths[0] : paths
} else if (segments.length === 2) {
const paths = toPaths.map((target) => {
Expand All @@ -47,7 +46,12 @@ export const pathsToModuleNameMapper = (

return `${enrichedPrefix}${enrichedTarget.replace(/\*/g, '$1')}`
})
pattern = `^${escapeRegex(segments[0])}(.*)${escapeRegex(segments[1])}$`

if (removeJsExtension) {
const patternWithJs = `^${escapeRegex(segments[0])}(.*)${escapeRegex(segments[1])}\\.js$`
jestMap[patternWithJs] = paths.length === 1 ? paths[0] : paths
}
const pattern = `^${escapeRegex(segments[0])}(.*)${escapeRegex(segments[1])}$`
jestMap[pattern] = paths.length === 1 ? paths[0] : paths
} else {
logger.warn(interpolate(Errors.NotMappingMultiStarPath, { path: fromPath }))
Expand Down

0 comments on commit b89c098

Please sign in to comment.