blog.unresolved.xyzblog.unresolved.xyz

NestJSで@Injectable({ scope: Scope.REQUEST })にするとテストが落ちる問題

Sat Jun 17 2023
  • NestJS
  • TIL

リクエストごとにスコープを切ってほしいサービスクラスを作ろうとしたら、テストが以下のエラーで落ちるようになりました。

XxxService is marked as a scoped provider. Request and transient-scoped providers can't be used in combination with "get()" method. Please, use "resolve()" instead.

とりあえず言うことを聞いて

// before
service = module.get<LineWorksApiService>(LineWorksApiService);

// after
service = await module.resolve<LineWorksApiService>(LineWorksApiService);

のように直したら通りました。