Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can not access to CLS in e2e test after the first sub-test #60

Closed
ianzone opened this issue Mar 9, 2023 · 2 comments
Closed

can not access to CLS in e2e test after the first sub-test #60

ianzone opened this issue Mar 9, 2023 · 2 comments
Labels
not a bug The issue is not caused by a problem in the library

Comments

@ianzone
Copy link

ianzone commented Mar 9, 2023

Log

Error: Cannot set the key "logTrace". No CLS context available, please make sure that a ClsMiddleware/Guard/Interceptor has set up the context, or wrap any calls that depend on CLS with "ClsService#run"
    at ClsService.set (/home/ian/templates/vite-nest/node_modules/.pnpm/nestjs-cls@3.2.1_77foi4w27ghy47yutmnzv7krjy/node_modules/nestjs-cls/dist/src/lib/cls.service.js:41:19)
    at MiddlewareHost.use (/src/middlewares/authentication/authentication.middleware.ts:26:18)
    at MiddlewareHost.use (/home/ian/templates/vite-nest/node_modules/.pnpm/@nestjs+core@9.3.9_jrq2rdgfp2sx67wmylmrqliwxe/node_modules/@nestjs/core/middleware/utils.js:48:30)
    at /home/ian/templates/vite-nest/node_modules/.pnpm/@nestjs+core@9.3.9_jrq2rdgfp2sx67wmylmrqliwxe/node_modules/@nestjs/core/router/router-proxy.js:9:23
    at Holder.done (/home/ian/templates/vite-nest/node_modules/.pnpm/@fastify+middie@8.1.0/node_modules/@fastify/middie/engine.js:107:13)
    at Object.run (/home/ian/templates/vite-nest/node_modules/.pnpm/@fastify+middie@8.1.0/node_modules/@fastify/middie/engine.js:59:12)

To reproduce:

  1. degit https://github.com/ianzone/vite-nest-fastify to-check
  2. cd to-check
  3. pnpm i
  4. pnpm run test
@Papooch
Copy link
Owner

Papooch commented Mar 9, 2023

I'll have a look. But it might be related to this one: #49

@Papooch Papooch added the investigation Why is this happening? label Mar 9, 2023
@Papooch
Copy link
Owner

Papooch commented Mar 9, 2023

Okay, so after looking into it, I found the error. In your tests, you never bound the ClsMiddleware - it is only bound in your main.ts, which is not used in the test.

I see you're binding the AuthenticationMiddleware in your AppModule, so I removed the ClsMiddleware registration out from main and added it to your AppModule before the AuthenticationMiddleware like so:

export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(ClsMiddleware).forRoutes('(.*)');
    consumer.apply(AuthenticationMiddleware).exclude('/').forRoutes('(.*)');
  }
}

Now the test passes as expected.

@Papooch Papooch closed this as completed Mar 9, 2023
@Papooch Papooch added not a bug The issue is not caused by a problem in the library and removed investigation Why is this happening? labels Mar 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
not a bug The issue is not caused by a problem in the library
Projects
None yet
Development

No branches or pull requests

2 participants