Testing Microservice client and `Cannot read property 'send' of null`
See original GitHub issueI’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:
- Created 6 years ago
- Comments:14 (10 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey guys, In the nearest release it’d be possible to override components from imported modules. It should help with testing those scenarios.
@ThomRick
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 😃