blog.unresolved.xyzblog.unresolved.xyz

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

Fri Jun 16 2023
  • NestJS
  • TIL

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

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

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

1// before 2service = module.get<LineWorksApiService>(LineWorksApiService); 3 4// after 5service = await module.resolve<LineWorksApiService>(LineWorksApiService);

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