Skip to content

Commit

Permalink
test: add integration test for gateway module
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxmachine authored and Rick Dutour Geerling committed Jan 20, 2020
1 parent 073a701 commit bef82ab
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"publish:npm": "npm publish --access public",
"prepublish:next": "npm run build",
"publish:next": "npm publish --access public --tag next",
"test:integration": "jest --config ./tests/jest-e2e.json"
"test:integration": "jest --config ./tests/jest-e2e.json --runInBand"
},
"devDependencies": {
"@nestjs/common": "6.10.14",
Expand Down
113 changes: 113 additions & 0 deletions tests/e2e/graphql-gateway.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { AppModule as PostsModule } from '../graphql-federation/posts-service/federation-posts.module';
import { AppModule as UsersModule } from '../graphql-federation/users-service/federation-users.module';
import { AppModule as GatewayModule } from '../graphql-federation/gateway/gateway.module';

describe('GraphQL', () => {
let postsApp: INestApplication;
let usersApp: INestApplication;
let gatewayApp: INestApplication;

beforeEach(async () => {
const usersModule = await Test.createTestingModule({
imports: [UsersModule],
}).compile();

usersApp = usersModule.createNestApplication();
await usersApp.listenAsync(3001);

const postsModule = await Test.createTestingModule({
imports: [PostsModule],
}).compile();

postsApp = postsModule.createNestApplication();
await postsApp.listenAsync(3002);

const gatewayModule = await Test.createTestingModule({
imports: [GatewayModule],
}).compile();

gatewayApp = gatewayModule.createNestApplication();
await gatewayApp.init();
});

it(`should run lookup across boundaries`, () => {
return request(gatewayApp.getHttpServer())
.post('/graphql')
.send({
operationName: null,
variables: {},
query: `
{
getPosts {
id,
title,
body,
user {
id,
name,
}
}
}`,
})
.expect(200, {
data: {
getPosts: [
{
id: '1',
title: 'Hello world',
body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
user: {
id: '5',
name: 'GraphQL',
},
},
],
},
});
});

it(`should run reverse lookup across boundaries`, () => {
return request(gatewayApp.getHttpServer())
.post('/graphql')
.send({
operationName: null,
variables: {},
query: `
{
getUser(id: "5") {
id,
name,
posts {
id,
title,
body,
}
}
}`,
})
.expect(200, {
data: {
getUser: {
id: '5',
name: 'GraphQL',
posts: [
{
id: '1',
title: 'Hello world',
body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
},
],
},
},
});
});

afterEach(async () => {
await postsApp.close();
await usersApp.close();
await gatewayApp.close();
});
});
15 changes: 15 additions & 0 deletions tests/graphql-federation/gateway/gateway.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Module } from '@nestjs/common';
import { GraphQLGatewayModule } from '../../../lib/graphql-gateway.module';
import { join } from 'path';

@Module({
imports: [
GraphQLGatewayModule.forRoot({
serviceList: [
{ name: 'users', url: 'http://localhost:3001/graphql' },
{ name: 'posts', url: 'http://localhost:3002/graphql' },
],
}),
],
})
export class AppModule {}

0 comments on commit bef82ab

Please sign in to comment.