I've added few changes to sub-project example-app-v12-monorepo
to reflect my situation:
Created
public_api.ts
file as barrel for shared classes inexamples/example-app-v12-monorepo/projects/app1/src/app/shared/public_api.ts
.Configured
tsconfig.json
file inexamples/example-app-v12-monorepo/tsconfig.json
so it contains my changes:
"paths": {
"@app1/shared": ["projects/app1/src/app/shared/public_api"]
}
- Added modules mapping in
examples/example-app-v12-monorepo/projects/app1/jest.config.js
:
moduleNameMapper: {
'@app1/shared': 'projects/app1/src/app/shared'
}
- Changed relative path from folder shared to absolute. For example:
'../shared/highlight.directive'
-> '@app1/shared'
Result:
When I try to run tests with yarn test
I get error:
Configuration error:
Could not locate module @app1/shared mapped as:
projects/app1/src/app/shared.
Please check 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/fix this list of path mappings?
##edit due to investigation progress
When I added <rootDir>
to absolute path I got closest of wanted results:
@app1/shared': '<rootDir>/projects/app1/src/app/shared/public_api
Resulted in:
jest-preset-angular/examples/example-app-v12-monorepo/projects/app1/projects/app1/src/app/shared/public_api
Basically, if I remove /projects/app1
part it all works as intended.
My problem now:
In my (big) app I have a lot of pre-defined paths from the project root, while Jest needs to be set up on sub-project level (monorepo) I need to figure out a way to transform passed paths so it doesn't double path segments.
Example:
tsconfig path:
example/path/project1/app/src/app/public_api.ts
Jest setup level:
project1/app/src/app/public_api.ts
outcome:
example/path/project1/app/project1/app/src/app/public_api.ts
Is there a way to transform it?
You must also have
public_api
in the mapper or you can changepublic_api
inindex