0

I've added few changes to sub-project example-app-v12-monorepo to reflect my situation:

  1. Created public_api.ts file as barrel for shared classes in examples/example-app-v12-monorepo/projects/app1/src/app/shared/public_api.ts.

  2. Configured tsconfig.json file in examples/example-app-v12-monorepo/tsconfig.json so it contains my changes:

"paths": {
  "@app1/shared": ["projects/app1/src/app/shared/public_api"]
}
  1. Added modules mapping in examples/example-app-v12-monorepo/projects/app1/jest.config.js:
moduleNameMapper: {
 '@app1/shared': 'projects/app1/src/app/shared'
}
  1. 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?

psmul
  • 141
  • 1
  • 1
  • 13

1 Answers1

0

You must also have public_api in the mapper or you can change public_api in index

module.exports = {
  rootDir: './',
  moduleNameMapper: {
    '@app1/shared': '<rootDir>/projects/app1/src/app/shared/public_api',
  },
  ...
};
Chris
  • 2,117
  • 13
  • 18
  • Hey! Thank you for your answer, however after applying your snippet suggestion I get the same error with different path: `Could not locate module @app1/shared mapped as:/jest-preset-angular/examples/example-app-v12-monorepo/projects/app1/projects/app1/src/app/shared/public_api.`. Maybe rootDir is unnecessary here? – psmul Dec 14 '21 at 13:27
  • @psmul have you tried without root? – Chris Dec 14 '21 at 13:29
  • yes, outcome is almost the same (just path changed): `Could not locate module @app1/shared mapped as: projects/app1/src/app/shared/public_api.` – psmul Dec 14 '21 at 13:41
  • @psmul can you reproduce on stackblitz? – Chris Dec 14 '21 at 14:39
  • Sorry, but this example is a bit too big to be transferred to stackblitz. However, in meantime, I was able to make a progress on my case. Would you mind reading the main thread? (##edit part) – psmul Dec 14 '21 at 14:52