moleculerjs / moleculer

:rocket: Progressive microservices framework for Node.js
https://moleculer.services/
MIT License
6.13k stars 581 forks source link

Getting BAD_PAYLOAD when starting broker with Jest #1066

Closed manju4ever closed 2 years ago

manju4ever commented 2 years ago

Hi Team,

So we're trying to test the microservice using jest. We've a transporter & another moleculer service in docker. When we invoke jest, it shows as BAD_PAYLOAD. The connection fails for some reason.

But if we put the same code as a separate JS file, it seems to be working fine.

import { ServiceBroker } from 'moleculer';

let broker;

beforeAll(async () => {
  broker = new ServiceBroker({
    namespace: 'development',
  });
  broker.createService({
    name: 'admin',
    version: 2,
    actions: {
      hello: () => {
        return 'Hello';
      },
    },
  });
  await broker.start();
}, 20 * 1000);

describe('Test Hello Path', () => {
  test('Test: Hello World', async () => {
    expect(broker.call('admin.hello')).toEqual("Hello");
  });
});

Context

Failure Logs

 [2022-03-07T13:00:19.118Z] ERROR desappa-aa7ab-30935/TRANSIT: NatsError: BAD_PAYLOAD
        at Function.errorForCode (/Users/desappa/Desktop/HolyGrail/isg-asset-inventory-search/src/ais.search.service/node_modules/nats/nats-base-client/error.ts:119:12)
        at NatsConnectionImpl.publish (/Users/desappa/Desktop/HolyGrail/isg-asset-inventory-search/src/ais.search.service/node_modules/nats/nats-base-client/nats.ts:108:23)
        at NatsTransporter.send (/Users/desappa/Desktop/HolyGrail/isg-asset-inventory-search/src/ais.search.service/node_modules/moleculer/src/transporters/nats.js:337:16)
        at NatsTransporter.publish (/Users/desappa/Desktop/HolyGrail/isg-asset-inventory-search/src/ais.search.service/node_modules/moleculer/src/transporters/base.js:192:15)
        at NatsTransporter.prepublish (/Users/desappa/Desktop/HolyGrail/isg-asset-inventory-search/src/ais.search.service/node_modules/moleculer/src/transporters/base.js:333:15)
        at Transit.publish (/Users/desappa/Desktop/HolyGrail/isg-asset-inventory-search/src/ais.search.service/node_modules/moleculer/src/transit.js:1371:18)
        at Transit.discoverNode (/Users/desappa/Desktop/HolyGrail/isg-asset-inventory-search/src/ais.search.service/node_modules/moleculer/src/transit.js:1193:15)
        at LocalDiscoverer.discoverNode (/Users/desappa/Desktop/HolyGrail/isg-asset-inventory-search/src/ais.search.service/node_modules/moleculer/src/registry/discoverers/local.js:44:23)
        at LocalDiscoverer.heartbeatReceived (/Users/desappa/Desktop/HolyGrail/isg-asset-inventory-search/src/ais.search.service/node_modules/moleculer/src/registry/discoverers/base.js:227:9)
        at Transit.messageHandler (/Users/desappa/Desktop/HolyGrail/isg-asset-inventory-search/src/ais.search.service/node_modules/moleculer/src/transit.js:389:21)
        at NatsTransporter.wrappedMessageHandler [as messageHandler] (/Users/desappa/Desktop/HolyGrail/isg-asset-inventory-search/src/ais.search.service/node_modules/moleculer/src/transit.js:66:55)
        at NatsTransporter.incomingMessage (/Users/desappa/Desktop/HolyGrail/isg-asset-inventory-search/src/ais.search.service/node_modules/moleculer/src/transporters/base.js:118:16)
        at NatsTransporter.receive (/Users/desappa/Desktop/HolyGrail/isg-asset-inventory-search/src/ais.search.service/node_modules/moleculer/src/transporters/base.js:131:15)
        at SubscriptionImpl.callback (/Users/desappa/Desktop/HolyGrail/isg-asset-inventory-search/src/ais.search.service/node_modules/moleculer/src/transporters/nats.js:240:11)
        at ProtocolHandler.processMsg (/Users/desappa/Desktop/HolyGrail/isg-asset-inventory-search/src/ais.search.service/node_modules/nats/nats-base-client/protocol.ts:407:11)
        at ProtocolHandler.push (/Users/desappa/Desktop/HolyGrail/isg-asset-inventory-search/src/ais.search.service/node_modules/nats/nats-base-client/protocol.ts:510:14)
        at Parser.parse (/Users/desappa/Desktop/HolyGrail/isg-asset-inventory-search/src/ais.search.service/node_modules/nats/nats-base-client/parser.ts:241:29)
        at ProtocolHandler.<anonymous> (/Users/desappa/Desktop/HolyGrail/isg-asset-inventory-search/src/ais.search.service/node_modules/nats/nats-base-client/protocol.ts:276:25)
        at Generator.next (<anonymous>)
        at fulfilled (/Users/desappa/Desktop/HolyGrail/isg-asset-inventory-search/src/ais.search.service/node_modules/nats/lib/nats-base-client/protocol.js:5:58)
        at processTicksAndRejections (node:internal/process/task_queues:96:5) {
      code: 'BAD_PAYLOAD',
      chainedError: undefined
    } HEARTBEAT Packet { type: 'HEARTBEAT', target: null, payload: { cpu: 1, ver: '4', sender: 'ais-admin-service-39148' } }

      at Array.<anonymous> (node_modules/moleculer/src/loggers/console.js:57:21)
      at Object.logger.<computed> [as error] (node_modules/moleculer/src/logger-factory.js:134:64)
      at Transit.messageHandler (node_modules/moleculer/src/transit.js:404:16)
      at NatsTransporter.wrappedMessageHandler [as messageHandler] (node_modules/moleculer/src/transit.js:66:55)
      at NatsTransporter.incomingMessage (node_modules/moleculer/src/transporters/base.js:118:16)
      at NatsTransporter.receive (node_modules/moleculer/src/transporters/base.js:131:15)
      at SubscriptionImpl.callback (node_modules/moleculer/src/transporters/nats.js:240:11)
icebob commented 2 years ago

Check the version of NATS server and nats library. This is not a Moleculer error, it's a NATS error.

manju4ever commented 2 years ago

I will check this once more. Thanks @icebob.

manju4ever commented 2 years ago

Hi @icebob , we tried with upgrading nats and also the npm package. It only fails from the context of Jest.

Important thing: We're not loading the service into tests, instead we run the required services and then test it.

Nats Server: 2.7 Nats Client Lib: 2.6.1

Not sure what to do tried all ways. This also happens when testing with the default TCP transporter.

icebob commented 2 years ago

Try to create a minimal reproducible example code.