1

I have an nx monorepo, which consists of two apps (client, server) and 5 libs (client-core, platform-core, etc). I pull the libraries into the Angular (client) application by setting the paths in the tsconfig.json.

    "paths": {
      "@myorg/platform-core": [
        "../../libs/platform-core/src/index.ts"
      ],
      "@myorg/client-core": [
        "../../libs/client-core/src/index.ts"
      ],
    },

This works fine, The IDE is able to resolve the libraries and I can serve the application with ng serve. However when I attempt to test the angular application using npx nx test client then it can't find the libraries.

 FAIL  apps/client/src/app/core/guards/patient.guard.spec.ts
  ● Test suite failed to run

    apps/client/src/app/core/guards/patient.guard.spec.ts:4:36 - error TS2307: Cannot find module '@myorg/client-core' or its corresponding type declarations.

    4 import { EnvironmentService } from '@myorg/client-core';
                                         ~~~~~~~~~~~~~~~~~~~

I have tried adding the same paths into tsconfig.spec.json (which shouldn't be necessary as it "extends": "./tsconfig.json") and that had no impact.

What do I need to do to access these libraries from my spec files?

Free Mind
  • 9
  • 2
  • Hi, you're supposed to mock the `EnvironmentService` (in the same specfile) – Pieterjan Jun 13 '22 at 18:51
  • 1
    This doesn't answer the question. I should still be able to access symbols from the libraries inside the specfiles. The `import { EnvironmentService } from '@myorg/client-core';` is inside the `paitent.guard.ts` file. It can find the libraries when I run `ng serve`, it should also be able to find the libraries when running `nx test`. – Free Mind Jun 13 '22 at 19:04
  • Same issue - did you find a solution? – user1514915 Oct 16 '22 at 11:11
  • A colleague did and I can't recall what the solution was. It was a very out of the box "that was too simple" solution. I'll report back if it strikes me. – Free Mind Oct 29 '22 at 00:24

3 Answers3

0

What versions do you use? I had the same problem on Angular 12 with jest 28. When I upgraded all my dependencies to Angular 14 + jest 28 the problems were gone. As a tip: using yarn workspaces also makes the resolves easier without having to use the paths property.

If the versions are up to date nx also has some problems with application-specific paths it seems. There is a (closed) issue about it:

For reference to people having issues with "Module not found" error. Provided tsconfig.app.json's baseUrl directs to application source, where it seems that it should direct to workspace root. For me links between modules started working when I either removed the baseUrl from tsconfig.app.json or pointed it to worspace root. Your tsconfig.json (or in latest nx it's tsconfig.base.json) that lives in workspace root should still have the baseUrl and it seems to be correct.

See https://github.com/nrwl/nx/issues/738

bas
  • 822
  • 10
  • 20
0

A solution can be that in your jest.json add the path to the library as you did in the tsconfig.json, for example:

  "moduleNameMapper": {
    "@myorg/client-core": "<rootDir>/../../libs/client-core/src/index.ts"
  }
  • please note that you must add <rootDir> like that, then add the path to the index file or src dir.
AmrAnwar
  • 59
  • 1
  • 8
-1

In my case the following solution worked.
Instead of running unit tests with npx nx test client, specify the project name as follows:

npx nx test nx-project-name --test-file=src/app/app.component.spec.ts

or, if you want to test libraries,

npx nx test nx-project-name --test-file=libs/lib-name

Disclaimer: It works with Nx Standalone apps but since the project name and the path is explicit it should work with monorepo as well.

Lorenzo
  • 64
  • 8