diff --git a/e2e/__cases__/utils/src/foo/bar.ts b/e2e/__cases__/utils/src/foo/bar.ts deleted file mode 100644 index cc798ff50d..0000000000 --- a/e2e/__cases__/utils/src/foo/bar.ts +++ /dev/null @@ -1 +0,0 @@ -export const a = 1; diff --git a/e2e/__cases__/utils/ts-jest-utils.spec.ts b/e2e/__cases__/utils/ts-jest-utils.spec.ts index 04a1987b8d..a70d33afb2 100644 --- a/e2e/__cases__/utils/ts-jest-utils.spec.ts +++ b/e2e/__cases__/utils/ts-jest-utils.spec.ts @@ -1,5 +1,4 @@ import * as utils from 'ts-jest/utils' -import { compilerOptions } from './tsconfig.json'; test('utils', () => { expect(Object.keys(utils)).toEqual(['mocked', 'createJestPreset', 'pathsToModuleNameMapper']) @@ -7,7 +6,3 @@ test('utils', () => { 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'}) -}) diff --git a/e2e/__cases__/utils/tsconfig.json b/e2e/__cases__/utils/tsconfig.json deleted file mode 100644 index 56df0e537d..0000000000 --- a/e2e/__cases__/utils/tsconfig.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "compilerOptions": { - "baseUrl": "src", - "target": "ES5", - "module": "CommonJS", - "resolveJsonModule": true, - "paths": { - "foo/*": ["foo/*"] - } - }, - "exclude": [ - "node_modules" - ] -} diff --git a/src/config/__snapshots__/paths-to-module-name-mapper.spec.ts.snap b/src/config/__snapshots__/paths-to-module-name-mapper.spec.ts.snap index 265cfd0e3f..9338f77cb9 100644 --- a/src/config/__snapshots__/paths-to-module-name-mapper.spec.ts.snap +++ b/src/config/__snapshots__/paths-to-module-name-mapper.spec.ts.snap @@ -2,6 +2,7 @@ exports[`pathsToModuleNameMapper should convert tsconfig mapping with given prefix: / 1`] = ` Object { + "^@foo\\\\-bar/common$": "/../common/dist/library", "^api/(.*)$": "/src/api/$1", "^client$": Array [ "/src/client", @@ -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", diff --git a/src/config/paths-to-module-name-mapper.spec.ts b/src/config/paths-to-module-name-mapper.spec.ts index 80875a8b91..22caa6fafc 100644 --- a/src/config/paths-to-module-name-mapper.spec.ts +++ b/src/config/paths-to-module-name-mapper.spec.ts @@ -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", diff --git a/src/config/paths-to-module-name-mapper.ts b/src/config/paths-to-module-name-mapper.ts index b35a7bd765..fc03983fe3 100644 --- a/src/config/paths-to-module-name-mapper.ts +++ b/src/config/paths-to-module-name-mapper.ts @@ -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 type JestPathMapping = Config.InitialOptions['moduleNameMapper'] @@ -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)) { @@ -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 {