Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor(api/test): convert module name mapper utility to JavaScript
This allows us to dogfood the Jest configuration without running a build
  • Loading branch information
jrolfs committed Feb 25, 2025
commit 3aedcf701b53dab56ac833a1ab753d3ca6e6df21
1 change: 0 additions & 1 deletion src/api/test.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {pathsToModuleNameMapper} from '../paths-to-module-name-mapper'
const pathsToModuleNameMapper = require('../paths-to-module-name-mapper')

const tsconfigMap = {
log: ['src/utils/log'],
Expand Down
3 changes: 3 additions & 0 deletions src/api/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
pathsToModuleNameMapper: require('./paths-to-module-name-mapper'),
}
1 change: 0 additions & 1 deletion src/api/test/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,38 @@
* @see {@link https://github.com/kulshekhar/ts-jest/blob/dd3523cb7571714f06f1ea2ed1e3cf11970fbfce/src/config/paths-to-module-name-mapper.ts}
*/

import type {Config} from '@jest/types'
import type {CompilerOptions} from 'typescript'

type TsPathMapping = Exclude<CompilerOptions['paths'], undefined>
type JestPathMapping = Config.InitialOptions['moduleNameMapper']
/**
* We don't need to escape all chars, so commented out is the real one
* const escapeRegex = (str: string) => str.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
*
* @param {string} str
* @returns {string}
*/
const escapeRegex = str => str.replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&')

// we don't need to escape all chars, so commented out is the real one
// const escapeRegex = (str: string) => str.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
const escapeRegex = (str: string) => str.replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&')
/**
* @typedef {Exclude<import('typescript').CompilerOptions['paths'], undefined>} TsPathMapping
* @typedef {import('@jest/types').Config.InitialOptions['moduleNameMapper']} JestPathMapping
*/

export const pathsToModuleNameMapper = (
mapping: TsPathMapping,
{prefix = '', useESM = false}: {prefix?: string; useESM?: boolean} = {},
): JestPathMapping => {
const jestMap: JestPathMapping = {}
/**
* Converts TypeScript path mappings to Jest module name mappings
*
* @param {TsPathMapping} mapping - The TypeScript path mapping object
* @param {{prefix?: string, useESM?: boolean}} options - Configuration options
* @returns {JestPathMapping}
*/
const pathsToModuleNameMapper = (
mapping,
{prefix = '', useESM = false} = {},
) => {
/** @type {JestPathMapping} */
const jestMap = {}
for (const fromPath of Object.keys(mapping)) {
const toPaths = mapping[fromPath]
// check that we have only one target path
if (toPaths.length === 0) {
console.warn(`Not mapping "${fromPath}" because it has no target.`)

continue
}

Expand All @@ -35,7 +46,6 @@ export const pathsToModuleNameMapper = (
const paths = toPaths.map(target => {
const enrichedPrefix =
prefix !== '' && !prefix.endsWith('/') ? `${prefix}/` : prefix

return `${enrichedPrefix}${target}`
})
const cjsPattern = `^${escapeRegex(fromPath)}$`
Expand All @@ -48,7 +58,6 @@ export const pathsToModuleNameMapper = (
: target
const enrichedPrefix =
prefix !== '' && !prefix.endsWith('/') ? `${prefix}/` : prefix

return `${enrichedPrefix}${enrichedTarget.replace(/\*/g, '$1')}`
})
if (useESM) {
Expand All @@ -74,3 +83,5 @@ export const pathsToModuleNameMapper = (

return jestMap
}

module.exports = pathsToModuleNameMapper