CodePark/ questions / Testing Nest apps
START PRACTICINGPRACTICE
Backend question · NestJS › Testing Nest apps

In an e2e test you want to bypass a JwtAuthGuard bound with @UseGuards(). Which testing API is meant for this?

.overrideGuard(JwtAuthGuard).useValue({ canActivate: () => true })

Use overrideGuard(). Guards bound with @UseGuards() are enhancers, not regular providers. So the testing module has special methods for them: overrideGuard(), overrideInterceptor(), overridePipe() and overrideFilter().

"
.overrideProvider(JwtAuthGuard).useValue(...)" only works for a guard registered as an APP_GUARD provider. It doesn't work for a guard attached with the decorator. ".overrideModule(JwtAuthGuard).useModule(...)" is wrong because a guard is not a module.