question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Testing Microservice client and `Cannot read property 'send' of null`

See original GitHub issue

I’m trying to test microservice and his client controller

// client
@Controller()
export class EnvironmentRpcClientController {

  @Client({ transport: Transport.TCP, port: ENVIRONMENT_APP_CONFIG.rpcPort })
  private client: ClientProxy;


  public get<T extends Object>(moduleName?: string): Observable<T> {

    const pattern = ENVIRONMENT_RPC_CONFIG.actions.get;
    const data = moduleName;

    return this.client.send<T>(pattern, data);
  }
}

test (EnvironmentAppModule is main microservice module with RPC server)

import { Test } from '@nestjs/testing';
import { NestFactory } from '@nestjs/core';
import 'rxjs/add/operator/finally';

import { ENVIRONMENT_APP_CONFIG } from '../src/config';
import { EnvironmentAppModule } from '../src/module';


import { EnvironmentRpcClientController } from '../src/rpc';


describe('environment', () => {

  let serverApp: any;

  let environmentRpcClientController: EnvironmentRpcClientController;


  beforeEach(done => {

    serverApp = NestFactory.createMicroservice(EnvironmentAppModule, { port: ENVIRONMENT_APP_CONFIG.rpcPort });

    serverApp.listen(() => {

      (<any> serverApp).logger.log(`TEST RPC server is listening on port ${ENVIRONMENT_APP_CONFIG.rpcPort}`);


      Test.createTestingModule({
        controllers: [ EnvironmentRpcClientController ],
      });

      environmentRpcClientController = Test.get(EnvironmentRpcClientController);

      done();
    });
  });

  afterEach(done => {

    serverApp.server.server.close(() => done());
  });


  it('should request envinroment data from service', done => {

    environmentRpcClientController.get()
      .finally(() => done())
      .subscribe(
        config => console.log('config', config)
      );
  });
});

and as result

    TypeError: Cannot read property 'send' of null

this.client in EnvironmentRpcClientController.get() is null.

Also i’m forced to use private fields access hacks like serverApp.server.server.close() to shutdown RPC server after each test and before new one, public method for server close will be cool.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:14 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
kamilmysliwieccommented, Jul 16, 2017

Hey guys, In the nearest release it’d be possible to override components from imported modules. It should help with testing those scenarios.

2reactions
artaommahecommented, Jun 5, 2017

@ThomRick

I don’t how you can call your microservice client controller with an external event / message (like an http request for example)

service without any public endpoint that has some tasks by interval, including RPC calls ) RPC client can be defined in service for this purpose.

Thx for your analysis, will wait for nest update for this tests 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - TypeError: Cannot read property 'sendMessage' of null
I found my error, I had rtm as a const and then a let. So I removed the const from rtm = new...
Read more >
Angular Test - Cannot read property 'textContent' of null
Getting Started with Angular - Angular Test - Karma server TypeError: Cannot read property 'textContent' of nullSpecial offers & Free ...
Read more >
Best Practices for Spies, Stubs and Mocks in Sinon.js
Learn about differences between spies, stubs and mocks, when and how to use them, and get a set of best practices to help...
Read more >
Getting started with continuous integration for Nest.js APIs
Learn how to build RESTful APIs with Nest.js, a Node.js framework built with TypeScript, and automate testing using CircleCI.
Read more >
Karate | Test Automation Made Simple.
To run a script *.feature file from your Java IDE, you just need the following empty test-class in the same package. The name...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found