Skip to content

Commit

Permalink
fix(utils): pathsToModuleNameMapper resolve path mapping with `path…
Browse files Browse the repository at this point in the history
….join` (#1969)

Closes #1968
  • Loading branch information
ahnpnl authored Sep 19, 2020
1 parent 1452ce4 commit 81fce4c
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 23 deletions.
1 change: 1 addition & 0 deletions e2e/__cases__/utils/src/foo/bar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a = 1;
5 changes: 5 additions & 0 deletions e2e/__cases__/utils/ts-jest-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
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: 14 additions & 0 deletions e2e/__cases__/utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"baseUrl": "src",
"target": "ES5",
"module": "CommonJS",
"resolveJsonModule": true,
"paths": {
"foo/*": ["foo/*"]
}
},
"exclude": [
"node_modules"
]
}
39 changes: 39 additions & 0 deletions src/config/__snapshots__/paths-to-module-name-mapper.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`pathsToModuleNameMapper should convert tsconfig mapping with given prefix: <rootDir>/ 1`] = `
Object {
"^api/(.*)$": "<rootDir>/src/api/$1",
"^client$": Array [
"<rootDir>/src/client",
"<rootDir>/src/client/index",
],
"^log$": "<rootDir>/src/utils/log",
"^mocks/(.*)$": "<rootDir>/test/mocks/$1",
"^server$": "<rootDir>/src/server",
"^test/(.*)$": "<rootDir>/test/$1",
"^test/(.*)/mock$": Array [
"<rootDir>/test/mocks/$1",
"<rootDir>/test/__mocks__/$1",
],
"^util/(.*)$": "<rootDir>/src/utils/$1",
}
`;
exports[`pathsToModuleNameMapper should convert tsconfig mapping with given prefix: foo 1`] = `
Object {
"^api/(.*)$": "foo/src/api/$1",
"^client$": Array [
"foo/src/client",
"foo/src/client/index",
],
"^log$": "foo/src/utils/log",
"^mocks/(.*)$": "foo/test/mocks/$1",
"^server$": "foo/src/server",
"^test/(.*)$": "foo/test/$1",
"^test/(.*)/mock$": Array [
"foo/test/mocks/$1",
"foo/test/__mocks__/$1",
],
"^util/(.*)$": "foo/src/utils/$1",
}
`;
23 changes: 3 additions & 20 deletions src/config/paths-to-module-name-mapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const tsconfigMap = {
}

describe('pathsToModuleNameMapper', () => {
it('should convert tsconfig mapping', () => {
it('should convert tsconfig mapping with no given prefix', () => {
expect(pathsToModuleNameMapper(tsconfigMap)).toMatchInlineSnapshot(`
Object {
"^api/(.*)$": "src/api/$1",
Expand All @@ -35,25 +35,8 @@ describe('pathsToModuleNameMapper', () => {
`)
})

it('should use the given prefix', () => {
expect(pathsToModuleNameMapper(tsconfigMap, { prefix: '<rootDir>/' })).toMatchInlineSnapshot(`
Object {
"^api/(.*)$": "<rootDir>/src/api/$1",
"^client$": Array [
"<rootDir>/src/client",
"<rootDir>/src/client/index",
],
"^log$": "<rootDir>/src/utils/log",
"^mocks/(.*)$": "<rootDir>/test/mocks/$1",
"^server$": "<rootDir>/src/server",
"^test/(.*)$": "<rootDir>/test/$1",
"^test/(.*)/mock$": Array [
"<rootDir>/test/mocks/$1",
"<rootDir>/test/__mocks__/$1",
],
"^util/(.*)$": "<rootDir>/src/utils/$1",
}
`)
it.each(['<rootDir>/', 'foo'])('should convert tsconfig mapping with given prefix', (prefix) => {
expect(pathsToModuleNameMapper(tsconfigMap, { prefix })).toMatchSnapshot(prefix)
})

it('should warn about mapping it cannot handle', () => {
Expand Down
8 changes: 5 additions & 3 deletions src/config/paths-to-module-name-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ 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 @@ -25,22 +27,22 @@ export const pathsToModuleNameMapper = (
// check that we have only one target path
if (toPaths.length === 0) {
logger.warn(interpolate(Errors.NotMappingPathWithEmptyMap, { path: fromPath }))

continue
}

// split with '*'
const segments = fromPath.split(/\*/g)
if (segments.length === 1) {
const paths = toPaths.map((target) => `${prefix}${target}`)
const paths = toPaths.map((target) => normalizeSlashes(join(prefix, target)))
pattern = `^${escapeRegex(fromPath)}$`
jestMap[pattern] = paths.length === 1 ? paths[0] : paths
} else if (segments.length === 2) {
const paths = toPaths.map((target) => `${prefix}${target.replace(/\*/g, '$1')}`)
const paths = toPaths.map((target) => normalizeSlashes(join(prefix, target.replace(/\*/g, '$1'))))
pattern = `^${escapeRegex(segments[0])}(.*)${escapeRegex(segments[1])}$`
jestMap[pattern] = paths.length === 1 ? paths[0] : paths
} else {
logger.warn(interpolate(Errors.NotMappingMultiStarPath, { path: fromPath }))
continue
}
}

Expand Down

0 comments on commit 81fce4c

Please sign in to comment.