1+ import * as ProtoLoader from '@grpc/proto-loader' ;
12import { INestApplication } from '@nestjs/common' ;
23import { Transport } from '@nestjs/microservices' ;
4+ import { ExpressAdapter } from '@nestjs/platform-express' ;
35import { Test } from '@nestjs/testing' ;
6+ import { fail } from 'assert' ;
7+ import { expect } from 'chai' ;
48import * as express from 'express' ;
9+ import * as GRPC from 'grpc' ;
510import { join } from 'path' ;
611import * as request from 'supertest' ;
7- import * as ProtoLoader from '@grpc/proto-loader' ;
8- import * as GRPC from 'grpc' ;
9- import { expect } from 'chai' ;
10- import { fail } from 'assert' ;
1112import { AdvancedGrpcController } from '../src/grpc-advanced/advanced.grpc.controller' ;
1213
1314describe ( 'Advanced GRPC transport' , ( ) => {
@@ -21,13 +22,14 @@ describe('Advanced GRPC transport', () => {
2122 } ) . compile ( ) ;
2223 // Create gRPC + HTTP server
2324 server = express ( ) ;
24- app = module . createNestApplication ( server ) ;
25+ app = module . createNestApplication ( new ExpressAdapter ( server ) ) ;
2526 /*
2627 * Create microservice configuration
2728 */
2829 app . connectMicroservice ( {
2930 transport : Transport . GRPC ,
3031 options : {
32+ url : 'localhost:5001' ,
3133 package : 'proto_example' ,
3234 protoPath : 'root.proto' ,
3335 loader : {
@@ -41,16 +43,15 @@ describe('Advanced GRPC transport', () => {
4143 await app . init ( ) ;
4244 // Load proto-buffers for test gRPC dispatch
4345 const proto = ProtoLoader . loadSync ( 'root.proto' , {
44- includeDirs : [ join ( __dirname , '../src/grpc-advanced/proto' ) ] ,
46+ includeDirs : [ join ( __dirname , '../src/grpc-advanced/proto' ) ] ,
4547 } ) as any ;
4648 // Create Raw gRPC client object
4749 const protoGRPC = GRPC . loadPackageDefinition ( proto ) as any ;
4850 // Create client connected to started services at standard 5000 port
4951 client = new protoGRPC . proto_example . orders . OrderService (
50- 'localhost:5000 ' ,
52+ 'localhost:5001 ' ,
5153 GRPC . credentials . createInsecure ( ) ,
5254 ) ;
53-
5455 } ) ;
5556
5657 it ( `GRPC Sending and Receiving HTTP POST` , ( ) => {
@@ -69,33 +70,32 @@ describe('Advanced GRPC transport', () => {
6970 } ) ;
7071
7172 it ( 'GRPC Sending and receiving message' , async ( ) => {
72-
7373 // Execute find in Promise
7474 return new Promise ( resolve => {
75- client . find ( {
76- id : 1 ,
77- } , ( err , result ) => {
78- // Compare results
79- expect ( err ) . to . be . null ;
80- expect ( result ) . to . eql ( {
75+ client . find (
76+ {
8177 id : 1 ,
82- itemTypes : [ 1 ] ,
83- shipmentType : {
84- from : 'test' ,
85- to : 'test1' ,
86- carrier : 'test-carrier' ,
87- } ,
88- } ) ;
89- // Resolve after checkups
90- resolve ( ) ;
91- } ) ;
92-
78+ } ,
79+ ( err , result ) => {
80+ // Compare results
81+ expect ( err ) . to . be . null ;
82+ expect ( result ) . to . eql ( {
83+ id : 1 ,
84+ itemTypes : [ 1 ] ,
85+ shipmentType : {
86+ from : 'test' ,
87+ to : 'test1' ,
88+ carrier : 'test-carrier' ,
89+ } ,
90+ } ) ;
91+ // Resolve after checkups
92+ resolve ( ) ;
93+ } ,
94+ ) ;
9395 } ) ;
94-
9596 } ) ;
9697
9798 it ( 'GRPC Sending and receiving Stream from RX handler' , async ( ) => {
98-
9999 const callHandler = client . sync ( ) ;
100100
101101 callHandler . on ( 'data' , ( msg : number ) => {
@@ -114,7 +114,11 @@ describe('Advanced GRPC transport', () => {
114114 callHandler . on ( 'error' , ( err : any ) => {
115115 // We want to fail only on real errors while Cancellation error
116116 // is expected
117- if ( String ( err ) . toLowerCase ( ) . indexOf ( 'cancelled' ) === - 1 ) {
117+ if (
118+ String ( err )
119+ . toLowerCase ( )
120+ . indexOf ( 'cancelled' ) === - 1
121+ ) {
118122 fail ( 'gRPC Stream error happened, error: ' + err ) ;
119123 }
120124 } ) ;
@@ -125,11 +129,9 @@ describe('Advanced GRPC transport', () => {
125129 } ) ;
126130 setTimeout ( ( ) => resolve ( ) , 1000 ) ;
127131 } ) ;
128-
129132 } ) ;
130133
131134 it ( 'GRPC Sending and receiving Stream from Call handler' , async ( ) => {
132-
133135 const callHandler = client . syncCall ( ) ;
134136
135137 callHandler . on ( 'data' , ( msg : number ) => {
@@ -148,7 +150,11 @@ describe('Advanced GRPC transport', () => {
148150 callHandler . on ( 'error' , ( err : any ) => {
149151 // We want to fail only on real errors while Cancellation error
150152 // is expected
151- if ( String ( err ) . toLowerCase ( ) . indexOf ( 'cancelled' ) === - 1 ) {
153+ if (
154+ String ( err )
155+ . toLowerCase ( )
156+ . indexOf ( 'cancelled' ) === - 1
157+ ) {
152158 fail ( 'gRPC Stream error happened, error: ' + err ) ;
153159 }
154160 } ) ;
@@ -159,8 +165,5 @@ describe('Advanced GRPC transport', () => {
159165 } ) ;
160166 setTimeout ( ( ) => resolve ( ) , 1000 ) ;
161167 } ) ;
162-
163168 } ) ;
164-
165-
166- } ) ;
169+ } ) ;
0 commit comments