I'm currently using moleculer microservices with the unit testing. I've a problem with starting the services.
I have a service named user service with version 1. There is one more service named Notification Service.
User service is tightly coupled with Notification Service.
For e.g.
I have a action named register in User Service and it calls sendEmail method using ctx.call('notification.sendEmail', emailConfig);
When I hit the route after npm run dev it's working properly.
But when I'm testing using npm test it's failing with the error message notification.sendEmail is not a Service
Here's the code snippet of user.spec.js:
const UserService = require("./../../services/user.service");
const NotificationService = require("./../../services/notification.service");
const userServiceBroker = new ServiceBroker({ logger: false });
const notificationServiceBroker = new ServiceBroker({ logger: false });
userServiceBroker.createService(UserService);
notificationServiceBroker.createService(NotificationService);
beforeEach(async () => {
await notificationServiceBroker.start();
await userServiceBroker.start();
});
@SushKenyNeosoft you are creating 2 ServiceBrokers and not setting the transporter so the services can't communicate.
You also might consider using single ServiceBroker during unit testing. This way you can abstract yourself from the transporter module and focus only on the application logic.
Also, for questions consider using Discord chat instead of Github issues.
Sure sir, really appreciate this.
It's working!
Changed the code to this:
const userServiceBroker = new ServiceBroker({ nodeID: "user-service-node", logger: false, transporter: "NATS" });
const notificationServiceBroker = new ServiceBroker({ nodeID: "notification-service-node", logger: false, transporter: "NATS" });
And started NATS server.
Most helpful comment
@SushKenyNeosoft you are creating 2
ServiceBrokers and not setting the transporter so the services can't communicate.You also might consider using single ServiceBroker during unit testing. This way you can abstract yourself from the transporter module and focus only on the application logic.
Also, for questions consider using Discord chat instead of Github issues.