Nest.js error in tests with DI between modules. Cannot find module 'src/foo/foo.module' from 'bar/bar.service.spec.ts'

huangapple go评论64阅读模式
英文:

Nest.js error in tests with DI between modules. Cannot find module 'src/foo/foo.module' from 'bar/bar.service.spec.ts'

问题

抱歉,我只提供代码的翻译服务。以下是您提供的代码的翻译:

在一个依赖于另一个模块导出的服务的测试中,出现了错误 `Cannot find module 'src/foo/foo.module' from 'bar/bar.service.spec.ts'`。

我可以通过在Nest.js项目中拥有两个独立的模块来重现此问题,比如说FooModule和BarModule,它们都导出了自己的服务,比如FooService和BarService,其中一个将另一个服务作为构造函数依赖项进行注入。我还没有找到正确的方法来为这种情况设置测试,以避免出现上述错误。以下是具有依赖项注入的服务的规范文件示例:

```typescript
import { Test, TestingModule } from '@nestjs/testing';
import { FooModule } from 'src/foo/foo.module';
import { FooService } from 'src/foo/foo.service';
import { BarService } from './bar.service';

describe('BarService', () => {
  let service: BarService;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [BarService, {
        provide: FooService,
        useValue: {test:() => 'This is a test.'}
      }],
      imports: [FooModule]
    }).compile();

    service = module.get<BarService>(BarService);
  });

  it('should be defined', () => {
    expect(service).toBeDefined();
  });
});

以下是如何在foo.module.ts中导出依赖项FooService:

import { Module } from '@nestjs/common';
import { FooService } from './foo.service';

@Module({
  providers: [FooService],
  exports: [FooService],
})
export class FooModule {}

我确定导入部分是正确的,所以找不到出现此错误的原因。
我想要成功地测试该服务,尽管它依赖于其他模块。项目在其他方面运行正常,所以这只是单元测试的问题。
感谢任何建议!

更新:仅在运行jest时发生这种情况。


<details>
<summary>英文:</summary>

Getting error `Cannot find module &#39;src/foo/foo.module&#39; from &#39;bar/bar.service.spec.ts&#39;` in a test that depends on a service exported by another module.


I can reproduce this issue by having 2 separate modules in a Nest.js project, say FooModule and BarModule, both exporting their own service, say FooService and BarService, and one of them injecting the other service as a constructor dependency. I haven&#39;t found the right approach to set up the test for this scenartio without stepping into above error. Here is a sample of the spec file for the service that has Dependency injection:

import { Test, TestingModule } from '@nestjs/testing';
import { FooModule } from 'src/foo/foo.module';
import { FooService } from 'src/foo/foo.service';
import { BarService } from './bar.service';

describe('BarService', () => {
let service: BarService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [BarService, {
provide: FooService,
useValue: {test:() => 'This is a test'}
}],
imports: [FooModule]
}).compile();

service = module.get&lt;BarService&gt;(BarService);

});

it('should be defined', () => {
expect(service).toBeDefined();
});
});

Here is how I export the dependency, FooService, in foo.module.ts

import { Module } from '@nestjs/common';
import { FooService } from './foo.service';

@Module({
providers: [FooService],
exports: [FooService],
})
export class FooModule {}

Imports I&#39;m sure they are correct, so can&#39;t find any reason for this error.
I want to test the service successfuly despite it has dependencies on another modules. The project runs fine otherwise, so it is just the unit tests.
Appreciate any inputs!

UPDATE: this only happend when running jest.

</details>


# 答案1
**得分**: 1

这在运行应用程序时不会发生,只有在运行jest时才会发生,对吗?那就不是一个nestjs的问题,而是一个jest的问题。

jest默认不理解绝对路径,这就是原因。

请参考另一篇提出相同问题的帖子:https://stackoverflow.com/questions/76280561

<details>
<summary>英文:</summary>

this doesn&#39;t happen when running the app, only when running jest, right? then is not a nestjs issue but a **jest issue**

jest doesn&#39;t understand absolute paths by default, that&#39;s why.

follow this another post with the same question: https://stackoverflow.com/questions/76280561

</details>



huangapple
  • 本文由 发表于 2023年5月22日 08:10:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302400.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定