Nestjs e2e test fails because of Kafka errors

Viewed 41

My main Nest application works fine with Kafka. It sends messages and subscribes to the response (gateway). I wrote an e2e test and when I run it (in the docker container), it fails because of Kafka connection.

Here are my errors:

| [Nest] 29  - 09/09/2022, 10:04:17 AM    WARN [ClientKafka] WARN [undefined] KafkaJS v2.0.0 switched default partitioner. To retain the same partitioning behavior as in previous versions, create the producer with the option "createPartitioner: Partitioners.LegacyPartitioner". See the migration guide at https://kafka.js.org/docs/migration-guide-v2.0.0#producer-new-default-partitioner for details. Silence this warning by setting the environment variable "KAFKAJS_NO_PARTITIONER_WARNING=1" {"timestamp":"2022-09-09T10:04:17.902Z","logger":"kafkajs"}
ms-gateway_1          | [Nest] 29  - 09/09/2022, 10:04:17 AM     LOG [NestApplication] Nest application successfully started +68ms
ms-gateway_1          | [Nest] 29  - 09/09/2022, 10:07:28 AM   ERROR [ClientKafka] ERROR [Connection] Response Heartbeat(key: 12, version: 3) {"timestamp":"2022-09-09T10:07:28.205Z","logger":"kafkajs","broker":"kafka:9092","clientId":"gateway-client-client","error":"The group is rebalancing, so a rejoin is needed","correlationId":48,"size":10}
ms-gateway_1          | [Nest] 29  - 09/09/2022, 10:07:28 AM   ERROR [ClientKafka] ERROR [Connection] Response Heartbeat(key: 12, version: 3) {"timestamp":"2022-09-09T10:07:28.214Z","logger":"kafkajs","broker":"kafka:9092","clientId":"gateway-client-client","error":"The group is rebalancing, so a rejoin is needed","correlationId":49,"size":10}
ms-gateway_1          | [Nest] 29  - 09/09/2022, 10:07:28 AM   ERROR [ClientKafka] ERROR [Connection] Response Heartbeat(key: 12, version: 3) {"timestamp":"2022-09-09T10:07:28.216Z","logger":"kafkajs","broker":"kafka:9092","clientId":"gateway-client-client","error":"The group is rebalancing, so a rejoin is needed","correlationId":50,"size":10}
ms-gateway_1          | [Nest] 29  - 09/09/2022, 10:07:28 AM    WARN [ClientKafka] WARN [Runner] The group is rebalancing, re-joining {"timestamp":"2022-09-09T10:07:28.218Z","logger":"kafkajs","groupId":"gateway-consumer-client","memberId":"gateway-client-client-eaa78df1-177b-484d-9c22-739e7f4a0e49","error":"The group is rebalancing, so a rejoin is needed"}
ms-gateway_1          | [Nest] 29  - 09/09/2022, 10:07:28 AM     LOG [ClientKafka] INFO [ConsumerGroup] Consumer has joined the group {"timestamp":"2022-09-09T10:07:28.243Z","logger":"kafkajs","groupId":"gateway-consumer-client","memberId":"gateway-client-client-eaa78df1-177b-484d-9c22-739e7f4a0e49","leaderId":"gateway-client-client-eaa78df1-177b-484d-9c22-739e7f4a0e49","isLeader":true,"memberAssignment":{"AccountBalanceQuery.reply":[0],"AccountCreateCommand.reply":[0],"AccountBalanceTransferCommand.reply":[0]},"groupProtocol":"NestReplyPartitionAssigner","duration":23}

This is my gateway code (which works fine with graphql playground or postman):

@Query(returns => AccountBalance)
async accountBalance2() {
  // tests passes without this line
  let result = await firstValueFrom(this.client.send(BROKER_MESSAGES.ACCOUNT_BALANCE_QUERY, { account_id: '05fbebcd-710c-48fc-a994-7fd0152bdc89' }));
  console.log('resss:', result)
  return (result >= 0) ? { balance: result } : { error:"Not Found!" };
}

This is my test code:

jest.setTimeout(30000)

describe('App Module (e2e)', () => {
  let app: INestApplication;

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

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

  afterAll(async () => {
    await app.close();
  });

  describe('test graphql', () => {
    it('/graphql (GET)', () => {

      const CREATE_ACCOUNT_MUTATION = 'query { accountBalance2 { balance } }'
  
      return request(app.getHttpServer())
        .post('/graphql')
        .send({
          query:CREATE_ACCOUNT_MUTATION,
        })
        .expect(200)
    });
  })
  
});

0 Answers
Related