使用NX,Jest和Angular进行测试 - 可以在运行测试时查找库
我有一个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”
),并且没有影响。
我需要做什么才能从我的规格文件访问这些库?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您使用哪些版本?我在Angular 12上遇到了同样的问题,Jest 28。当我将所有依赖项升级到Angular 14 + Jest 28时,问题就消失了。提示:使用纱线工作区还可以使解决方案更容易,而无需使用
paths
属性。如果版本是最新的
nx
也存在一些特定于应用程序路径的问题。有一个(封闭的)问题:参见 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:See https://github.com/nrwl/nx/issues/738
解决方案可以是,在
jest.json
中,添加图书馆的路径,例如tsconfig.json
,例如:< rootdir>
像这样,然后将路径添加到索引文件或src dir。A solution can be that in your
jest.json
add the path to the library as you did in thetsconfig.json
, for example:<rootDir>
like that, then add the path to the index file or src dir.就我而言,以下解决方案有效。
与其使用
NPX NX Test Client
运行单元测试,不如指定项目名称如下:或者,如果要测试库,
请免责声明:它与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:or, if you want to test libraries,
Disclaimer: It works with Nx Standalone apps but since the project name and the path is explicit it should work with monorepo as well.