最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NestJS Global Modules in tests - Stack Overflow

programmeradmin3浏览0评论

Is there a way to automatically provide all @Globalmodules into a TestModule ? (i.e without having to import them, the same way the main application works)

So far, I had to make sure to insert any global modules into the import list of my call:

await Test.createTestingModule({
      imports: [
        GlobalModule1,
        GlobalModule2

Is there a way to automatically provide all @Globalmodules into a TestModule ? (i.e without having to import them, the same way the main application works)

So far, I had to make sure to insert any global modules into the import list of my call:

await Test.createTestingModule({
      imports: [
        GlobalModule1,
        GlobalModule2
Share Improve this question edited Mar 15, 2019 at 10:54 Kim Kern 60.5k20 gold badges216 silver badges213 bronze badges asked Mar 15, 2019 at 2:41 Luiz Henrique Martins Lins RolLuiz Henrique Martins Lins Rol 2,5996 gold badges31 silver badges44 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 15

Global modules always have to be imported once for their providers to be available globally. This holds true for tests and the main application, see the docs.

Global modules shall be registered only once, in best case by the root or core module. Afterwards, the CatsService provider will be ubiquitous, although CatsModule won't be imported.

So there's no way around importing them. You can make it easier by creating a CommonsModule that imports all your global modules. You can then import the CommonsModule instead of each module in your AppModule and your tests.

Note though, that having lots of global dependencies is a code smell. Also, in unit tests you typically want to test a class in isolation from any other dependencies. If you import the global modules, you will test against the actual providers.

Making everything global is not a good decision. The global modules are available to reduce the amount of necessary boilerplate. The imports array is still the best way to make the module API transparent.

发布评论

评论列表(0)

  1. 暂无评论