Skip to content

Commit

Permalink
add nestjs project test
Browse files Browse the repository at this point in the history
  • Loading branch information
Toon van Strijp committed May 4, 2020
1 parent edc39ca commit 7213e8b
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 4 deletions.
14 changes: 13 additions & 1 deletion integration/nestjs-metadata-observables/sample-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HeroService, HeroById, Hero, Villain, VillainById } from './hero';
import { Observable, of } from 'rxjs';
import { Observable, of, Subject } from 'rxjs';
import { Metadata } from 'grpc';

export class SampleService implements HeroService {
Expand All @@ -10,4 +10,16 @@ export class SampleService implements HeroService {
findOneVillain(request: VillainById, metadata?: Metadata): Observable<Villain> {
return of({ id: 1, name: 'test' });
}

findManyVillain(request: Observable<VillainById>, metadata?: Metadata): Observable<Villain> {
const hero$ = new Subject<Villain>();

const onNext = (villainById: VillainById) => {
hero$.next({ id: 1, name: 'test' });
};
const onComplete = () => hero$.complete();
request.subscribe(onNext, null, onComplete);

return hero$.asObservable();
}
}
13 changes: 13 additions & 0 deletions integration/nestjs-metadata/sample-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HeroService, HeroById, Hero, Villain, VillainById } from './hero';
import { Metadata } from 'grpc';
import { Observable, Subject } from 'rxjs';

export class SampleService implements HeroService {
findOneHero(request: HeroById, metadata?: Metadata): Promise<Hero> {
Expand All @@ -9,4 +10,16 @@ export class SampleService implements HeroService {
findOneVillain(request: VillainById, metadata?: Metadata): Promise<Villain> {
return Promise.resolve({ id: 1, name: 'test' });
}

findManyVillain(request: Observable<VillainById>): Observable<Villain> {
const hero$ = new Subject<Villain>();

const onNext = (villainById: VillainById) => {
hero$.next({ id: 1, name: 'test' });
};
const onComplete = () => hero$.complete();
request.subscribe(onNext, null, onComplete);

return hero$.asObservable();
}
}
16 changes: 14 additions & 2 deletions integration/nestjs-simple-observables/sample-service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import { HeroService, HeroById, Hero, Villain, VillainById } from './hero';
import { Observable, of } from 'rxjs';
import { Observable, of, Subject } from 'rxjs';

export class SampleService implements HeroService {
findOneHero(request: HeroById): Observable<Hero> {
return of({ id: 1, name: 'test' });
}

findOneVillain(request: VillainById): Observable<Villain> {
return of({ id: 1, name: 'test' });
}

findManyVillain(request: Observable<VillainById>): Observable<Villain> {
const hero$ = new Subject<Villain>();

const onNext = (villainById: VillainById) => {
hero$.next({ id: 1, name: 'test' });
};
const onComplete = () => hero$.complete();
request.subscribe(onNext, null, onComplete);

return hero$.asObservable();
}
}
22 changes: 22 additions & 0 deletions integration/nestjs-simple/nestjs-project/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Module } from '@nestjs/common';
import { HeroController } from './hero.controller';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { join } from 'path';

@Module({
imports: [
ClientsModule.register([
{
name: 'HERO_PACKAGE',
transport: Transport.GRPC,
options: {
url: '0.0.0.0:8080',
package: 'hero',
protoPath: join(__dirname, '../hero.proto'),
},
},
]),
],
controllers: [HeroController]
})
export class AppModule {}
47 changes: 47 additions & 0 deletions integration/nestjs-simple/nestjs-project/hero.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Controller, OnModuleInit } from '@nestjs/common';
import {
GrpcMethod,
GrpcStreamMethod,
} from '@nestjs/microservices';
import { Observable, Subject } from 'rxjs';
import { HeroById, Hero, HeroService, VillainById, Villain } from '../hero';

@Controller('hero')
export class HeroController implements OnModuleInit, HeroService {
private readonly heroes: Hero[] = [
{ id: 1, name: 'Stephenh' },
{ id: 2, name: 'Iangregsondev' },
];

private readonly villains: Villain[] = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Doe' },
];

onModuleInit() {
}

@GrpcMethod('HeroService')
async findOneHero(data: HeroById): Promise<Hero> {
return this.heroes.find(({ id }) => id === data.id)!;
}

@GrpcMethod('HeroService')
async findOneVillain(data: VillainById): Promise<Villain> {
return this.villains.find(({ id }) => id === data.id)!;
}

@GrpcStreamMethod('HeroService')
findManyVillain(request: Observable<VillainById>): Observable<Villain> {
const hero$ = new Subject<Villain>();

const onNext = (villainById: VillainById) => {
const item = this.villains.find(({ id }) => id === villainById.id);
hero$.next(item);
};
const onComplete = () => hero$.complete();
request.subscribe(onNext, null, onComplete);

return hero$.asObservable();
}
}
17 changes: 17 additions & 0 deletions integration/nestjs-simple/nestjs-project/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { NestFactory } from '@nestjs/core';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import { join } from 'path';
import { AppModule } from './app.module';

export async function createApp() {
const app = await NestFactory.createMicroservice<MicroserviceOptions>(AppModule, {
transport: Transport.GRPC,
options: {
url: '0.0.0.0:8080',
package: 'hero',
protoPath: join(__dirname, '../hero.proto'),
},
});

return app;
}
36 changes: 36 additions & 0 deletions integration/nestjs-simple/nestjs-simple-test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,44 @@
import { SampleService } from './sample-service';
import { createApp } from './nestjs-project/main';
import { INestMicroservice } from '@nestjs/common';
import { ClientGrpc } from '@nestjs/microservices';
import { HeroService } from './hero';
import { Observable } from 'rxjs';
import { Hero } from '../nestjs-metadata/hero';

describe('nestjs-simple-test', () => {
it('compiles', () => {
const service = new SampleService();
expect(service).not.toBeUndefined();
});
});

describe('nestjs-simple-test nestjs', () => {
let app: INestMicroservice;
let client: ClientGrpc;
let heroService: HeroService;

beforeAll(async () => {
app = await createApp();
client = app.get('HERO_PACKAGE');
heroService = client.getService<HeroService>('HeroService');
await app.listenAsync();
});

afterAll(async () => {
await app.close();
});

it('should get grpc client', async () => {
expect(client).not.toBeUndefined();
});

it('should get heroService', async () => {
expect(heroService).not.toBeUndefined();
});

it('should findOneHero', async() => {
const hero = await (heroService.findOneHero({ id: 1 }) as unknown as Observable<Hero>).toPromise();
expect(hero).toEqual({ id: 1, name: 'Stephenh' });
});
});
13 changes: 13 additions & 0 deletions integration/nestjs-simple/sample-service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { HeroService, HeroById, Hero, Villain, VillainById } from './hero';
import { Observable, Subject } from 'rxjs';

export class SampleService implements HeroService {
findOneHero(request: HeroById): Promise<Hero> {
Expand All @@ -8,4 +9,16 @@ export class SampleService implements HeroService {
findOneVillain(request: VillainById): Promise<Villain> {
return Promise.resolve({ id: 1, name: 'test' });
}

findManyVillain(request: Observable<VillainById>): Observable<Villain> {
const hero$ = new Subject<Villain>();

const onNext = (villainById: VillainById) => {
hero$.next({ id: 1, name: 'test' });
};
const onComplete = () => hero$.complete();
request.subscribe(onNext, null, onComplete);

return hero$.asObservable();
}
}
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@
"author": "",
"license": "ISC",
"devDependencies": {
"@grpc/proto-loader": "^0.5.4",
"@nestjs/common": "^7.0.9",
"@nestjs/core": "^7.0.9",
"@nestjs/microservices": "^7.0.9",
"@types/jest": "^24.0.11",
"@types/node": "^10.7.0",
"grpc": "^1.24.2",
"jest": "^25.1.0",
"prettier": "^1.16.4",
"reflect-metadata": "^0.1.13",
"rxjs": "^6.5.5",
"ts-jest": "^25.2.1",
"ts-node": "^8.3.0",
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"module": "commonjs",
"strict": true,
"outDir": "build",
"skipLibCheck": true
"skipLibCheck": true,
"experimentalDecorators": true
},
"include": ["src"]
}

0 comments on commit 7213e8b

Please sign in to comment.