medibloc / nestjs-request-context

NestJS Request Context using AsyncLocalStorage
MIT License
45 stars 4 forks source link

Middleware is not applied in e2e tests #3

Closed psteinroe closed 3 years ago

psteinroe commented 3 years ago

Hi,

I am trying your package but it seems like the middleware is not applied in e2e tests. While running the app normally it works fine though. Any ideas where the problem may be?

Edit: The "onModuleInit" is executed in the e2e tests, although the middleware itself is not..

Edit: The Code I setup the testing module in the normal way:

moduleFixture = await Test.createTestingModule({
  imports: [AppModule],
}).compile();

app = moduleFixture.createNestApplication();
await app.init();

I import the module in my main app module:

@Module({
  imports: [
    RequestContextModule.forRoot({
      contextClass: ExecutionContextStorage,
      isGlobal: true,
    }),
    GraphQLModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: (configService: ConfigService) => ({
        ...configService.get('GRAPHQL'),
      }),
      inject: [ConfigService],
    }),
    AuthModule,
    ...a lot of other modules
  ],
})
export class AppModule {}

In the AuthGuard I set the value:

async canActivate(context: ExecutionContext): Promise<boolean> {
    await super.canActivate(context);
    const request = this.getRequest(context);
    const user: AuthUser = request.user;

    const ctx: ExecutionContextStorage = RequestContext.get();
    ctx.executionContext = context;
    ....
}

and in the resolver-layer, I read it:

const ctx: ExecutionContextStorage = RequestContext.get();

psteinroe commented 3 years ago

I just realised that my problem comes from using createTestClient, where the middlewares are not applied..

youngjoon-lee commented 3 years ago

Thank you for trying to use this package. I'm happy to hear that it works well. Please ping me if you have any issue.