NarHakobyan / awesome-nest-boilerplate

Awesome NestJS Boilerplate 😍, Typescript 💪, Postgres 🎉, TypeORM 🥳
https://narhakobyan.github.io/awesome-nest-boilerplate
MIT License
2.35k stars 442 forks source link

e2e test fails with "No CLS namespace defined in your app ... please call initializeTransactionalContext()" #341

Open topchyalexey opened 9 months ago

topchyalexey commented 9 months ago

on node 16 environment

[Nest] 64242  - 12/12/2023, 12:29:00 PM   ERROR [ExceptionsHandler] No CLS namespace defined in your app ... please call initializeTransactionalContext() before application start.
Error: No CLS namespace defined in your app ... please call initializeTransactionalContext() before application start.
    at UserService.createUser (/Users/aleksey.topchy/IdeaProjects/philit/medical/aip-back/node_modules/typeorm-transactional/src/transactions/wrap-in-transaction.ts:36:13)
    at AuthController.userRegister (/Users/aleksey.topchy/IdeaProjects/philit/medical/aip-back/src/modules/auth/auth.controller.ts:32:52)
    at /Users/aleksey.topchy/IdeaProjects/philit/medical/aip-back/node_modules/@nestjs/core/router/router-execution-context.js:38:29
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
  console.log
    query: SELECT "UserEntity"."id" AS "UserEntity_id", "UserEntity"."created_at" AS "UserEntity_created_at", "UserEntity"."updated_at" AS "UserEntity_updated_at", "UserEntity"."first_name" AS "UserEntity_first_name", "UserEntity"."last_name" AS "UserEntity_last_name", "UserEntity"."role" AS "UserEntity_role", "UserEntity"."email" AS "UserEntity_email", "UserEntity"."password" AS "UserEntity_password", "UserEntity"."phone" AS "UserEntity_phone", "UserEntity"."avatar" AS "UserEntity_avatar", (SELECT CONCAT("UserEntity".first_name, ' ', "UserEntity".last_name)) AS "UserEntity_full_name" FROM "users" "UserEntity" WHERE ("UserEntity"."email" = $1) LIMIT 1 -- PARAMETERS: ["john@smith.com"]

      at Function.logInfo (../src/platform/PlatformTools.ts:232:17)

 FAIL  test/app.e2e-spec.ts (10.961 s)
  AuthController (e2e)
    ✕ /auth/register (POST) (48 ms)
    ✕ /auth/login (POST) (26 ms)
    ✕ /auth/me (GET) (4 ms)

  ● AuthController (e2e) › /auth/register (POST)
topchyalexey commented 9 months ago

Fixed by adding

**import { initializeTransactionalContext } from 'typeorm-transactional';**

import { AppModule } from '../src/app.module';

describe('AuthController (e2e)', () => {
  let app: INestApplication;
  let accessToken: string;

  beforeAll(async () => {
    **initializeTransactionalContext();**
    const moduleFixture = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();

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

but then I get next error

[Nest] 65272  - 12/12/2023, 12:37:56 PM   ERROR [ExceptionsHandler] You need to use @UseDto on class (UserEntity) be able to call toDto function
Error: You need to use @UseDto on class (UserEntity) be able to call toDto function
    at UserEntity.toDto (/Users/aleksey.topchy/IdeaProjects/philit/medical/aip-back/src/common/abstract.entity.ts:24:19)
    at AuthController.userRegister (/Users/aleksey.topchy/IdeaProjects/philit/medical/aip-back/src/modules/auth/auth.controller.ts:33:28)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
  console.log
    query: SELECT "UserEntity"."id" AS "UserEntity_id", "UserEntity"."created_at" AS "UserEntity_created_at", "UserEntity"."updated_at" AS "UserEntity_updated_at", "UserEntity"."first_name" AS "UserEntity_first_name", "UserEntity"."last_name" AS "UserEntity_last_name", "UserEntity"."role" AS "UserEntity_role", "UserEntity"."email" AS "UserEntity_email", "UserEntity"."password" AS "UserEntity_password", "UserEntity"."phone" AS "UserEntity_phone", "UserEntity"."avatar" AS "UserEntity_avatar", (SELECT CONCAT("UserEntity".first_name, ' ', "UserEntity".last_name)) AS "UserEntity_full_name" FROM "users" "UserEntity" WHERE ("UserEntity"."email" = $1) LIMIT 1 -- PARAMETERS: ["john@smith.com"]

      at Function.logInfo (../src/platform/PlatformTools.ts:232:17)
topchyalexey commented 9 months ago

Fixed it by this issue workaround https://github.com/NarHakobyan/awesome-nest-boilerplate/issues/332