@@ -4,26 +4,37 @@ const getPackages = require('get-monorepo-packages')
44const doNotMapPackages = process . env . JEST_SKIP_PACKAGE_MAP === 'true'
55
66/**
7- * Allows ts-jest to dynamically resolve packages so "build"
7+ * Create module mapping that resolve packages for ts-jest so typescript compilation happens in-memory
8+ *
89 */
9- const getJestModuleMap = (
10- packageRoot = '../../' ,
11- skipPackageMap = doNotMapPackages
12- ) => {
10+ const getJestModuleMap = ( { skipPackageMap = doNotMapPackages } = { } ) => {
1311 // get listing of packages in the mono repo
14- const createLocation = ( name ) => {
15- return `<rootDir>/./${ name } /src/$1`
12+
13+ /**
14+ * @param location - e.g. "packages/core"
15+ */
16+
17+ const createPackageMappedPath = ( location ) => {
18+ // if packageRoot is the global jest file (using projects), our mappers suddenly need
19+ // to be relative to each project -- I have no idea why, seems unintuitive.
20+ // If not root config, equiv to running "yarn test" in an individual package repo (rootDir will be the root package.json)
21+ const moduleBasePath = global . JEST_ROOT_CONFIG
22+ ? '<rootDir>/../..'
23+ : '<rootDir>'
24+ return `${ moduleBasePath } /${ location } /src/$1`
1625 }
26+ // for the sake of getPackages working correctly during a project-wide test run, the working directory must be hardcoded to the root
27+ const packageRoot = global . JEST_ROOT_CONFIG ? '.' : '../../'
1728 const moduleNameMapper = getPackages ( packageRoot ) . reduce (
1829 ( acc , el ) => ( {
1930 ...acc ,
20- [ `${ el . package . name } (.*)$` ] : createLocation ( el . location ) ,
31+ [ `${ el . package . name } (.*)$` ] : createPackageMappedPath ( el . location ) ,
2132 } ) ,
2233 { }
2334 )
2435
2536 return {
26- '@/(.+)' : '<rootdir>/../.. /src/$1' ,
37+ '@/(.+)' : '<rootDir> /src/$1' ,
2738 ...( skipPackageMap ? { } : moduleNameMapper ) ,
2839 }
2940}
0 commit comments