使用NX,Jest和Angular进行测试 - 可以在运行测试时查找库

发布于 2025-02-06 23:47:29 字数 1012 浏览 3 评论 0 原文

我有一个NX MonorePo,它由两个应用程序(客户端,服务器)和5个LIB(客户端核心,平台核心等)组成。我通过在 tsconfig.json 中设置 PATHS 将库将库将其拉入角度(客户端)应用程序。

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

这很好,IDE能够解析库,我可以使用 ng服务为应用程序服务。但是,当我尝试使用 NPX NX Test Client 测试Angular应用程序时,它找不到库。

 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';
                                         ~~~~~~~~~~~~~~~~~~~

我尝试在 tsconfig.spec.json 中添加相同的路径(这不是必需的,因为它“扩展”:“ ./tsconfig.json” ),并且没有影响。

我需要做什么才能从我的规格文件访问这些库?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白芷 2025-02-13 23:47:29

您使用哪些版本?我在Angular 12上遇到了同样的问题,Jest 28。当我将所有依赖项升级到Angular 14 + Jest 28时,问题就消失了。提示:使用纱线工作区还可以使解决方案更容易,而无需使用 paths 属性。

如果版本是最新的 nx 也存在一些特定于应用程序路径的问题。有一个(封闭的)问题:

引用遇到“未找到模块”错误的人。
提供TSCONFIG.APP.JSON的baseurl将其引导到应用程序源,似乎应该将其直接定向到工作区根。对我来说,当我从tsconfig.app.json删除baseurl或将其指向worspace root时,模块之间的链接开始工作。
您的tsconfig.json(或最新的NX中,它的tsconfig.base.json)生活在工作空间根中的baseurl,似乎是正确的。

参见 https://github.com/nrwl/nx/nx/nx/issues/738

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

与之呼应 2025-02-13 23:47:29

解决方案可以是,在 jest.json 中,添加图书馆的路径,例如 tsconfig.json ,例如:

  "moduleNameMapper": {
    "@myorg/client-core": "<rootDir>/../../libs/client-core/src/index.ts"
  }
  • 请注意,您必须添加&lt; rootdir&gt; 像这样,然后将路径添加到索引文件或src dir。

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.
怪我鬧 2025-02-13 23:47:29

就我而言,以下解决方案有效。
与其使用 NPX NX Test Client 运行单元测试,不如指定项目名称如下:

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

或者,如果要测试库,

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

请免责声明:它与NX独立应用程序一起使用,但是由于项目名称和路径显式它也应该与monorepo一起使用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文