Nest: Upgrade to v6 - overrideGuard not work in tests

Created on 24 Apr 2019  路  19Comments  路  Source: nestjs/nest

Hi!

I'm submitting a bug


[ ] Regression 
[X] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

const module = await Test.createTestingModule({
    imports: [...],
  })
    .overrideGuard(AuthGuard)
    .useValue({ canActivate: () => true })
    .compile();

const app = module.createNestApplication();

Current behavior


Does not override guard.

Expected behavior


On version 5.* it works as expected. So the guard is overrided.

Environment


Nest version: 6.1.1 (also for testing module)

For Tooling issues:

  • Node version: 8.15
  • Platform: Linux
needs clarification

Most helpful comment

Fixed in the latest release :)

All 19 comments

Please, provide a minimal repository which reproduces your issue.

Hi Kamil!
Added a repo here - https://github.com/aistrati/nestjs6-update

Thanks, I'll look at it shortly

@kamilmysliwiec Hi!
Do you have any updates on this ?
Or maybe I can contribute on this somehow ?

Hi @kamilmysliwiec, the issue was introduced by 5922e7ed9c88c29e25dad4d5ae77e0347a426231 in version 6.1.1.

Fixed in 6.2.1 :) Thanks for reporting

I think i'm having this issue again, 6.2.4

Confirming. On latest version it's still not working...

Just pulled the repo again and everything works fine. Please, create a separate issue if you face any issue (+attach repo which reproduces your problem)

A workaround, instead of overrideGuard:

  const guard = moduleFixture.get(AuthGuardWithAllowSentry);
  guard.canActivate = () => Promise.resolve(true);

https://github.com/aistrati/nestjs6-update is not alive anymore.

@Bnaya repo is alive (it's set as private). If you face any issue, please, create a separate issue with the included repo.

@Bnaya I made it public.
@kamilmysliwiec It's not reproducibile on the repo. We will update the repo when guard is not overriden (under some circumstances need to figure out).

@kamilmysliwiec Hi! Updated the repo which reproduce the error. Seems like it's working on controller level, but not on method. Take a look, please.

@kamilmysliwiec I have the same issue, this problem appeared after adding enhancers and if
you have several Guards or another similar enhancers in your controller (for example several methods wrapped by the same Guard). Then new InstanceWrapper will be created for each described Guard. Their metadata store as array: https://github.com/nestjs/nest/blob/4624a241999f237adf5fc4a46f0ccc698cb89e94/packages/core/injector/instance-wrapper.ts#L159
image
But at the same time this enhancer add as an injectable to module (injectables stored as Map) And so new instance of InstanceWrapper will override previous: https://github.com/nestjs/nest/blob/4624a241999f237adf5fc4a46f0ccc698cb89e94/packages/core/injector/module.ts#L147
image

And thats why when you try to override in test this kind of enhancer in fact you override only the last Guard in this controller: https://github.com/nestjs/nest/blob/4624a241999f237adf5fc4a46f0ccc698cb89e94/packages/core/injector/module.ts#L349
image

@kamilmysliwiec @aistrati @Bnaya
UPD: there is already bug fix in master. https://github.com/nestjs/nest/commit/aed28959b88cb7b350d474c86237efd739dd0928

We are waiting for new release :)

Fixed in the latest release :)

Fixed in the latest release :)

if the module has multiple Controllers then you're unable to override AuthGuard()

Fixed in the latest release :)

if the module has multiple Controllers then you're unable to override AuthGuard()

Same issue !

Please, if you face any issue, provide a minimal repository and create a separate issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hackboy picture hackboy  路  3Comments

JulianBiermann picture JulianBiermann  路  3Comments

rlesniak picture rlesniak  路  3Comments

cojack picture cojack  路  3Comments

menme95 picture menme95  路  3Comments