Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(utils): pathsToModuleNameMapper resolve path mapping with path.join #1969

Merged
merged 1 commit into from Sep 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions e2e/__cases__/utils/src/foo/bar.ts
@@ -0,0 +1 @@
export const a = 1;
5 changes: 5 additions & 0 deletions e2e/__cases__/utils/ts-jest-utils.spec.ts
@@ -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
@@ -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
@@ -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
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
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