在 Nx + Angular monorepo 架构,我应该将特定于一个库的测试助手放在哪里?
就上下文而言,我们目前有一个 Angular + Nx monorepo,只有一个主要 app
,但我们计划很快将其拆分为几个单独的微前端。
我们目前只有一个主模块,util-models
,它包含所有描述 API 交互的所有接口,以及所有 我们在测试中使用的存根来模拟数据。
现在假设我有一个库 my-feature
,其中包含一些我想要构建和部署的功能(作为延迟加载的路由或作为主包的一部分)。该库已经依赖于 util-models
,因为它处理一些标准化数据,但我们还有一个单独的 my-model.model.ts
文件,仅描述接口具体到这一功能。
一些视觉表示:
\my-feature
\lib
- my-models.model.ts
- my-component.component.ts
- my-component.component.spec.ts
\util-models
\lib
- shared-model.model.ts
\test
- shared-model.stub.ts
所以问题是,我应该将包含所有特定于库的存根的 my-models.stub.ts
文件放在哪里?
1. 第一个明显的答案似乎是“将特定于库的代码放入库中”。但这是否意味着我的所有库实际上应该为所有特定于它们的存根或测试实用程序拥有一个单独的 test
目录?这是否也适用于模型(甚至不是可编译的代码,只是接口)?这是否会降低这些应该帮助开发人员的工具的可发现性,以防将来其他一些库实际上也在处理相同的数据结构?
另外,此代码是否会与常规 .spec.ts
文件一起从产品版本中自动删除?不将其导入到模块中就足够了吗?
2. 另一种选择是将其放入已经存在的 util-models
库中,my-feature
可能会继续依赖该库,但显然我担心这只会使从长远来看,它的分离程度较小。另一方面,如果这只是测试永远不会投入生产的代码,那么也许这实际上很好,甚至出于某种原因是可取的?
我最感兴趣的是这两个选项将如何潜在地影响构建/测试构建时间、树摇动、延迟加载和迁移到微前端等事情。我将不胜感激任何提示!
For context, we have an Angular + Nx monorepo with just one main app
currently, but we plan to split it to a few separate microfrontends soon.
We currently have just one main module, util-models
, that contains all the interfaces describing all the API interactions, and also all the stubs we use in tests to mock data.
Now let's say I have a library my-feature
that contains some feature that I'll want to build and deploy (as a lazily-loaded route or as a part of the main bundle). This library already depends on util-models
, because it deals with some standardized data, but we also have there a separate my-model.model.ts
file that only describes interfaces specific to this one feature.
Some visual representation:
\my-feature
\lib
- my-models.model.ts
- my-component.component.ts
- my-component.component.spec.ts
\util-models
\lib
- shared-model.model.ts
\test
- shared-model.stub.ts
So the question is, where should I put my my-models.stub.ts
file that contains all of the library-specific stubs?
1. The first obvious answer would seem to be "put your library-specific code in the library". But does that mean that all of my libraries should actually aim to have a separate test
directory for all stubs or test utils that are specific to them? Does that apply to models too (which aren't even compilable code, just interfaces)? Doesn't that reduce the discoverability of these tools that are supposed to assist devs, in case some other library in the future was also actually dealing with the same data structures?
Also, will this code get automatically dropped from the prod build alongside the regular .spec.ts
files? Is it enough to just not import it in the module
?
2. Another option would be putting it in the already existing util-models
library, which my-feature
will probably keep depending on, but obviously I'm worried that just makes it less separated in the long run. On the other hand, if this is just testing code that will never go to production anyway, then maybe that's actually fine, or even desirable for some reason?
I'm mostly interested in how both options would potentially affect things like build/test build times, tree shaking, lazy loading, and migration to microfrontends. I'd appreciate any tips!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我还没有找到任何官方文档可以解决这个问题,但是在花了更多时间使用 Nx 之后,特别是在了解了更多有关 DDD(领域驱动开发)之后,我有了更多的直觉。
我目前的直觉是,实际上不相关的库之间代码的“可发现性”不是一个功能,而是一个问题。所有代码都应该属于自己的特定领域,而不是集中在某个大型通用库中 - 无论它只是接口还是测试代码都没关系。
如果我们允许每个“共享”库存在于依赖关系树中,则每个“共享”库都必须具有最多特定于一个域的用途。
因此,我现在对以下决定充满信心:
test
目录,以便在我们想要测试组件/服务/等时准备好模型存根。在那个图书馆里。I haven't found any official document that would clear this up, but after spending more time with Nx, and especially after learning more about DDD (Domain Driven Development), I grew more of an intuition.
My current intuition is that "discoverability" of code between libraries that are actually unrelated is not a feature, it's a problem. All code should belong to its own specific domain, not get centralized in some large generic library - it doesn't matter if it's only interfaces or testing code.
Each "shared" library, if we allow it to exist in our dependency tree, has to have a purpose specific to at most one domain.
Therefore I now feel confident with the following decisions:
util-models
library. Models describing API responses all can be assigned to more specific domains.test
directories in any libraries that own models, in order to have model stubs ready when we want to test our components/services/etc. in that library.