Skip to content

Commit

Permalink
fix(utils): revert path.join and add check on prefix ends with / (#…
Browse files Browse the repository at this point in the history
…1989)

Closes #1982
  • Loading branch information
ahnpnl authored Sep 29, 2020
1 parent dcd836d commit 3d9035b
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 25 deletions.
1 change: 0 additions & 1 deletion e2e/__cases__/utils/src/foo/bar.ts

This file was deleted.

5 changes: 0 additions & 5 deletions e2e/__cases__/utils/ts-jest-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import * as utils from 'ts-jest/utils'
import { compilerOptions } from './tsconfig.json';

test('utils', () => {
expect(Object.keys(utils)).toEqual(['mocked', 'createJestPreset', 'pathsToModuleNameMapper'])
expect(typeof utils.mocked).toBe('function')
expect(typeof utils.createJestPreset).toBe('function')
expect(typeof utils.pathsToModuleNameMapper).toBe('function')
})

test('pathsToModuleNameMapper', () => {
expect(utils.pathsToModuleNameMapper(compilerOptions.paths, { prefix: compilerOptions.baseUrl } )).toEqual({'^foo/(.*)$': 'src/foo/$1'})
})
14 changes: 0 additions & 14 deletions e2e/__cases__/utils/tsconfig.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

exports[`pathsToModuleNameMapper should convert tsconfig mapping with given prefix: <rootDir>/ 1`] = `
Object {
"^@foo\\\\-bar/common$": "<rootDir>/../common/dist/library",
"^api/(.*)$": "<rootDir>/src/api/$1",
"^client$": Array [
"<rootDir>/src/client",
Expand All @@ -21,6 +22,7 @@ Object {
exports[`pathsToModuleNameMapper should convert tsconfig mapping with given prefix: foo 1`] = `
Object {
"^@foo\\\\-bar/common$": "foo/../common/dist/library",
"^api/(.*)$": "foo/src/api/$1",
"^client$": Array [
"foo/src/client",
Expand Down
2 changes: 2 additions & 0 deletions src/config/paths-to-module-name-mapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ const tsconfigMap = {
'test/*': ['test/*'],
'mocks/*': ['test/mocks/*'],
'test/*/mock': ['test/mocks/*', 'test/__mocks__/*'],
'@foo-bar/common': ['../common/dist/library'],
}

describe('pathsToModuleNameMapper', () => {
it('should convert tsconfig mapping with no given prefix', () => {
expect(pathsToModuleNameMapper(tsconfigMap)).toMatchInlineSnapshot(`
Object {
"^@foo\\\\-bar/common$": "../common/dist/library",
"^api/(.*)$": "src/api/$1",
"^client$": Array [
"src/client",
Expand Down
16 changes: 11 additions & 5 deletions src/config/paths-to-module-name-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import type { CompilerOptions } from 'typescript'

import { rootLogger } from '../utils/logger'
import { Errors, interpolate } from '../utils/messages'
import { join } from 'path'
import { normalizeSlashes } from '../utils/normalize-slashes'

type TsPathMapping = Exclude<CompilerOptions['paths'], undefined>
type JestPathMapping = Config.InitialOptions['moduleNameMapper']
Expand All @@ -18,7 +16,7 @@ const logger = rootLogger.child({ [LogContexts.namespace]: 'path-mapper' })

export const pathsToModuleNameMapper = (
mapping: TsPathMapping,
{ prefix = '' }: { prefix?: string } = {},
{ prefix = '' }: { prefix: string } = Object.create(null),
): JestPathMapping => {
const jestMap: JestPathMapping = {}
for (const fromPath of Object.keys(mapping)) {
Expand All @@ -34,11 +32,19 @@ export const pathsToModuleNameMapper = (
// split with '*'
const segments = fromPath.split(/\*/g)
if (segments.length === 1) {
const paths = toPaths.map((target) => normalizeSlashes(join(prefix, target)))
const paths = toPaths.map((target) => {
const enrichedPrefix = prefix !== '' && !prefix.endsWith('/') ? `${prefix}/` : prefix

return `${enrichedPrefix}${target}`
})
pattern = `^${escapeRegex(fromPath)}$`
jestMap[pattern] = paths.length === 1 ? paths[0] : paths
} else if (segments.length === 2) {
const paths = toPaths.map((target) => normalizeSlashes(join(prefix, target.replace(/\*/g, '$1'))))
const paths = toPaths.map((target) => {
const enrichedPrefix = prefix !== '' && !prefix.endsWith('/') ? `${prefix}/` : prefix

return `${enrichedPrefix}${target.replace(/\*/g, '$1')}`
})
pattern = `^${escapeRegex(segments[0])}(.*)${escapeRegex(segments[1])}$`
jestMap[pattern] = paths.length === 1 ? paths[0] : paths
} else {
Expand Down

0 comments on commit 3d9035b

Please sign in to comment.