moleculerjs / moleculer

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

Error while running unit test of service async actions #832

Closed goldytech closed 3 years ago

goldytech commented 3 years ago

Prerequisites

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

Current Behavior

I want to write unit tests for login action in auth service.

Here is the code for auth service

https://gist.github.com/goldytech/ccbd0a149ae40d29ec2638a353402013

The auth service has a dependency on greeter service. In greeter service I'm initialising my db in created life cycle event of the service.

Here is the code for greeter service

https://gist.github.com/goldytech/1ca35374f497d738621ca17e7e5e7fc5

This is the code for my unit test for login action in auth service

https://gist.github.com/goldytech/68dd303977b47da841c830d8171362d3

This above code gives me error

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

I want to know how can bypass dependencies when running unit tests of service actions. Is there any parameter options when creating new instance of ServiceBroker

Also another thing how can I initialise ctx.meta.someProperty in my tests as my action depends on it. This property is set from the api service in onBeforeCall

Expected Behavior

I should be able to run unit tests

Failure Information

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  1. step 1
  2. step 2
  3. you get it...

Reproduce code snippet

const broker = new ServiceBroker({
    logger: console,
    transporter: "NATS"
});

broker.createService({
    name: "test",
    actions: {
        empty(ctx) {
            return "Hello"
        }
    }
});

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

Failure Logs

1fabiopereira commented 3 years ago

@goldytech You found how to fix?