Could not locate module @app1/shared mapped as... No mapping for self contained modules

1.6k Views Asked by At

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?

1

There are 1 best solutions below

5
On

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',
  },
  ...
};