由于Nest无法找到模块,因此无法运行测试

时间:2019-06-21 12:34:03

标签: nestjs

我遵循了单元测试示例,但是我无法运行测试,我不知道为什么它不起作用。我遇到以下错误:Cannot find module 'src/Application/Auth/Command/LoginCommandHandler' from 'LoginCommandHandler.spec.ts',但是正确导入了我的处理程序。 感谢您的帮助。

这是我的单元测试:

import { LoginCommandHandler } from 'src/Application/Auth/Command/LoginCommandHandler';
import { UserRepository } from 'src/Infrastructure/User/Repository/UserRepository';
import { EncryptionAdapter } from 'src/Infrastructure/Adapter/EncryptionAdapter';

// ...

const module: TestingModule = await Test.createTestingModule({
      providers: [LoginCommandHandler, UserRepository, EncryptionAdapter],
    }).compile();

    userRepository = module.get(UserRepository);
    encryptionAdapter = module.get(EncryptionAdapter);
    handler = new LoginCommandHandler(userRepository, encryptionAdapter);

这是我的src / Application / Auth / Command / LoginCommandHandler / LoginCommandHandler:

export class LoginCommandHandler {
  constructor(
    @Inject('IUserRepository')
    private readonly userRepository: IUserRepository,
    @Inject('IEncryptionAdapter')
    private readonly encryptionAdapter: IEncryptionAdapter,
  ) {}
// ...

这是我的AuthModule:

@Module({
  imports: [
    // ...
    TypeOrmModule.forFeature([User]),
  ],
  providers: [
    // ...
    { provide: 'IUserRepository', useClass: UserRepository },
    { provide: 'IEncryptionAdapter', useClass: EncryptionAdapter },
    LoginCommandHandler,
  ],
})
export class AuthModule {}

3 个答案:

答案 0 :(得分:2)

将此文件添加到package.json文件中的Jest配置中:

"moduleNameMapper": {
  "^src/(.*)$": "<rootDir>/$1"
}

答案 1 :(得分:1)

Jest无法找到与您要导入的绝对路径相关的模块。您可以找到更多信息at this stackoverflow question

简而言之,您只需要告诉Jest有关在哪里寻找您的模块(可以在moduleDirectories的{​​{1}}字段中,也可以在{{ 1}})

答案 2 :(得分:0)

有效,谢谢您的回答! 我有一个新问题,巢无法解决我的依赖关系。 我有以下错误: public class HeartbeatPacket implements HeartbeatStop { private final String TAG = getClass().getSimpleName(); private int curInterval = 0; private HeartbeatStop heartbeatStop = null; private final int setInterval; private Timer timer; public HeartbeatPacket(HeartbeatStop heartbeatStop, int setInterval) { this.heartbeatStop = heartbeatStop; this.curInterval = setInterval; this.setInterval = this.curInterval; } public void callStopFun() { if (this.heartbeatStop != null) { this.heartbeatStop.callStopFun(); } } public void recover() { synchronized (this) { this.curInterval = this.setInterval; } } private void run() { if (this.timer == null) { Log.e(this.TAG, "null == timer"); } else { this.timer.schedule(new TimerTask() { public void run() { synchronized (this) { //this is the problem section if (HeartbeatPacket.this.curInterval = HeartbeatPacket.this.curInterval - 1 < 0) { HeartbeatPacket.this.callStopFun(); HeartbeatPacket.this.recover(); HeartbeatPacket.this.stop(); } } } }, 0, 1000); } } public void start() { recover(); this.timer = new Timer(); run(); } public void stop() { this.timer.cancel(); this.timer = null; }`` }

这是我的存储库定义:

Nest can't resolve dependencies of the IUserRepository (?). Please make sure that the argument at index [0] is available in the _RootTestModule context.

我使用typeorm,是否必须导入所有依赖项,或者有办法避免这种情况?