Moleculer: Tests of service is failed in case of service have dependencies.

Created on 9 Nov 2019  ·  2Comments  ·  Source: moleculerjs/moleculer

Prerequisites

Please answer the following questions for yourself before submitting an issue.

  • [x] I am running the latest version
  • [x] I checked the documentation and found no answer
  • [x] I checked to make sure that this issue has not already been filed
  • [x] I'm reporting the issue to the correct repository

Current Behavior

Tests of service is failed in case of service have dependencies.

Expected Behavior

Tests of service is passed in case of service have dependencies.

Failure Information

I have two services: GreeterService and HelperService.
If GreeterService have dependencies: dependencies: ['helper'] then tests is fail.
If GreeterService have not dependencies: dependencies: [] then tests is pass.

Steps to Reproduce

  1. Create two simplest services: GreeterService and HelperService. GreeterService must depend on HelperService: dependencies: ['helper']
  2. Create simple test for GreeterService.
  3. Run tests: npm run test test/unit/greeter.spec.ts

Reproduce code snippet

//File: src/services/greeter.service.ts
import { Context, ServiceSchema } from 'moleculer';
const GreeterService: ServiceSchema = {
    name: "greeter",
    settings: {},

    dependencies: ['helper'],

    actions: {
        hello: {
            handler(ctx: Context): String {
                return "Hello Moleculer";
            }
        },
    },
};
export = GreeterService;

//File: src/services/helper.service.ts
import { Context, ServiceSchema } from 'moleculer';
const HelperService: ServiceSchema = {
  name: 'helper',
  settings: {},

  dependencies: [],

  actions: {
    hello: {
      handler(ctx: Context): String {
        return 'Hello from Helper';
      },
    },
  },
};
export = HelperService;

//File: test/unit/greeter.spec.ts
import { ServiceBroker, Errors } from "moleculer";
import GreeterService from "../../src/services/greeter.service";

describe("Test 'greeter' service", () => {
    let broker = new ServiceBroker();
    broker.createService(GreeterService);

    beforeAll(() => broker.start());
    afterAll(() => broker.stop());

    describe("Test 'greeter.hello' action", () => {
        it("should return with 'Hello Moleculer'", () => {
            expect(broker.call("greeter.hello")).resolves.toBe("Hello Moleculer");
        });
    });
});

Context

  • Moleculer version: 0.13.11
  • NodeJS version: 12.13.0
  • Operating System: 18.04.3 LTS (Bionic Beaver)
  • Moleculer-web: 0.9.0-beta6
  • Jest: 23.6.0
  • Typescript: 3.6.4

Failure Logs

$ npm run test test/unit/greeter.spec.ts

> cross-env NODE_ENV=test jest --detectOpenHandles --runInBand "test/unit/greeter.spec.ts"

  console.info node_modules/moleculer/src/logger.js:126
    [2019-11-09T08:15:05.708Z] INFO  ubuntu-8480/BROKER: Moleculer v0.13.11 is starting...

  console.info node_modules/moleculer/src/logger.js:126
    [2019-11-09T08:15:05.712Z] INFO  ubuntu-8480/BROKER: Node ID: ubuntu-8480

  console.info node_modules/moleculer/src/logger.js:126
    [2019-11-09T08:15:05.712Z] INFO  ubuntu-8480/BROKER: Namespace: <not defined>

  console.info node_modules/moleculer/src/logger.js:126
    [2019-11-09T08:15:05.712Z] INFO  ubuntu-8480/REGISTRY: Strategy: RoundRobinStrategy

  console.info node_modules/moleculer/src/logger.js:126
    [2019-11-09T08:15:05.714Z] INFO  ubuntu-8480/BROKER: Serializer: JSONSerializer

  console.info node_modules/moleculer/src/logger.js:126
    [2019-11-09T08:15:05.715Z] INFO  ubuntu-8480/BROKER: Registered 10 internal middleware(s).

  console.info node_modules/moleculer/src/logger.js:126
    [2019-11-09T08:15:05.725Z] INFO  ubuntu-8480/GREETER: Waiting for service(s) 'helper'...

  console.info node_modules/moleculer/src/logger.js:126
    [2019-11-09T08:15:05.728Z] INFO  ubuntu-8480/REGISTRY: '$node' service is registered.

 FAIL  test/unit/greeter.spec.ts (5.657s)
  Test 'greeter' service
    Test 'greeter.hello' action
      ✕ should return with 'Hello Moleculer' (29ms)

  ● Test 'greeter' service › Test 'greeter.hello' action › should return with 'Hello Moleculer'

    Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.

      at mapper (node_modules/jest-jasmine2/build/queue_runner.js:41:52)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        5.697s, estimated 8s
Ran all test suites matching /test\/unit\/greeter.spec.ts/i.
  console.warn node_modules/moleculer/src/logger.js:126
    [2019-11-09T08:15:10.735Z] WARN  ubuntu-8480/BROKER: Service 'greeter.hello' is not registered.

  console.info node_modules/moleculer/src/logger.js:126
    [2019-11-09T08:15:10.769Z] INFO  ubuntu-8480/BROKER: ServiceBroker is stopped. Good bye.

  console.warn node_modules/bluebird/js/release/debuggability.js:986
    Unhandled rejection Error: expect(received).resolves.toBe()

    Expected received Promise to resolve, instead it rejected to value
      [ServiceNotFoundError: Service 'greeter.hello' is not found.]
        at expect (<ROOT_DIR>/node_modules/expect/build/index.js:100:15)
        at Object.<anonymous> (<ROOT_DIR>/test/unit/greeter.spec.ts:16:4)
        at Object.asyncJestTest (<ROOT_DIR>/node_modules/jest-jasmine2/build/jasmine_async.js:108:37)
        at <ROOT_DIR>/node_modules/jest-jasmine2/build/queue_runner.js:56:12
        at new Promise (<anonymous>)
        at mapper (<ROOT_DIR>/node_modules/jest-jasmine2/build/queue_runner.js:43:19)
        at <ROOT_DIR>/node_modules/jest-jasmine2/build/queue_runner.js:87:41

Most helpful comment

If a service has a dependency you really should register it on the broker before starting it. Try to add
broker.createService(HelloService); in your test.
I would say that in this case you are not doing a unit test anymore but an integration test (because your service is depending and hopefully calling some actions or receiving some events from the other service), so alternatively you could stub any call you are making to the dependency's actions.

All 2 comments

If a service has a dependency you really should register it on the broker before starting it. Try to add
broker.createService(HelloService); in your test.
I would say that in this case you are not doing a unit test anymore but an integration test (because your service is depending and hopefully calling some actions or receiving some events from the other service), so alternatively you could stub any call you are making to the dependency's actions.

Thanks for explanation!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abdavid picture abdavid  ·  4Comments

maitrucquynhq111 picture maitrucquynhq111  ·  3Comments

kesslerdev picture kesslerdev  ·  4Comments

molobala picture molobala  ·  3Comments

HighSoftWare96 picture HighSoftWare96  ·  4Comments