Question
Jest moduleNameMapper Resolution with Environment-Specific Path Aliases
Zo Cedar
0 reputation · 13 Dec 2020, 05:56 UTC
89.5K views0
Context
A project uses moduleNameMapper to alias internal packages with absolute paths that resolve on developer machines. The same configuration fails in the production CI pipeline because the mapped paths do not exist in the container filesystem.
Goal
Define a single moduleNameMapper configuration that resolves correctly in both local development and the production build environment without maintaining duplicate config files.
Constraints
- Local paths use
/Users/<user>/project/packages/*while CI uses/workspace/packages/*. - The alias pattern
^@internal/(.*)$must map to the correct physical location in each environment. - Jest version is 29.x;
rootsandmoduleDirectoriesare already set.
Open Questions
- Can
moduleNameMapperaccept a function or environment variable interpolation to compute the target path at runtime? - Does Jest provide a built-in mechanism to resolve aliases relative to
rootDirinstead of absolute paths? - If neither is supported, what is the recommended pattern for sharing a single jest.config across heterogeneous filesystems?