created: 2021-04-04T04:12:31.000Z

Nest can't resolve dependencies of the FooService

NestJS のテストで以下のエラーになった。 これは新しい依存(Prisma)を Denylist モジュールに追加したときに発生した。

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

    Potential solutions:
    - If PrismaService is a provider, is it part of the current RootTestModule?
    - If PrismaService is exported from a separate @Module, is that module imported within RootTestModule?
      @Module({
        imports: [ /* the Module containing PrismaService */ ]
      })

エラーの原因はテストでの providers に追加し忘れ。エラーメッセージの指示通り、サービスが依存するようになったクラスをテストのprovidersにも追加しないといけない。

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
-      providers: [DenylistService],
+      providers: [DenylistService, PrismaService],
    }).compile();
CAREER SKILLS ソフトウェア開発者の完全キャリアガイド
[ad] CAREER SKILLS ソフトウェア開発者の完全キャリアガイド
ジョン・ソンメズ, 長尾 高弘 (Kindle版)