I have made some modifications to the sub-project known as example-app-v12-monorepo
that reflect my current situation:
Created a barrel file named
public_api.ts
for shared classes located at
.examples/example-app-v12-monorepo/projects/app1/src/app/shared/public_api.ts
Updated the
tsconfig.json
file in
to include my changes:examples/example-app-v12-monorepo/tsconfig.json
"paths": {
"@app1/shared": ["projects/app1/src/app/shared/public_api"]
}
- Added module mapping in
:examples/example-app-v12-monorepo/projects/app1/jest.config.js
moduleNameMapper: {
'@app1/shared': 'projects/app1/src/app/shared'
}
- Switched from relative paths to absolute paths. For example:
'../shared/highlight.directive'
-> '@app1/shared'
Issue:
When attempting to run tests using yarn test
, an error is encountered:
Configuration error:
Could not find module @app1/shared mapped as:
projects/app1/src/app/shared.
Please review your configuration for these entries:
{
"moduleNameMapper": {
"/@app1\/shared/": "projects/app1/src/app/shared"
},
"resolver": /Users/psmul/Desktop/jest-preset-angular/examples/example-app-v12-monorepo/node_modules/jest-preset-angular/build/resolvers/ng-jest-resolver.js
}
Is there a way to configure/resolve this list of path mappings?
##edit based on further investigation
By adding <rootDir>
to the absolute path, I was able to get closer to the desired outcome:
@app1/shared': '<rootDir>/projects/app1/src/app/shared/public_api
Resulting in:
jest-preset-angular/examples/example-app-v12-monorepo/projects/app1/projects/app1/src/app/shared/public_api
The issue now: In my (large) application, many paths are predefined from the project root, but Jest requires setup at the sub-project level (monorepo). I need to find a way to adjust passed paths to avoid duplicating path segments. Example: tsconfig path:
example/path/project1/app/src/app/public_api.ts
Jest setup level:
project1/app/src/app/public_api.ts
desired outcome:
example/path/project1/app/project1/app/src/app/public_api.ts
Is there a method to accomplish this transformation?