Please answer the following questions for yourself before submitting an issue.
Tests of service is failed in case of service have dependencies.
Tests of service is passed in case of service have dependencies.
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.
dependencies: ['helper']npm run test test/unit/greeter.spec.ts//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");
});
});
});
$ 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
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!
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.