Skip to content

Commit fe78b06

Browse files
committed
add integration tests
1 parent 789dbf6 commit fe78b06

3 files changed

Lines changed: 188 additions & 1 deletion

File tree

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"dev": "nodemon examples/index.js",
88
"ci": "jest --watch",
99
"test": "jest --coverage",
10+
"ci:integration": "jest \"**/integration/**spec.js\" --watch",
1011
"lint": "eslint --ext=.js src test",
1112
"lint:fix": "eslint --fix --ext=.js src test",
1213
"deps": "npm-check -u",
@@ -45,6 +46,7 @@
4546
"moleculer": "^0.14.0-beta5",
4647
"moleculer-repl": "^0.6.1",
4748
"moleculer-web": "^0.9.0-beta6",
49+
"node-fetch": "^2.6.0",
4850
"nodemon": "^1.19.4",
4951
"npm-check": "^5.9.0",
5052
"prettier": "^1.18.2"

test/integration/greeter.spec.js

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
const { ServiceBroker } = require("moleculer");
2+
const { MoleculerClientError } = require("moleculer").Errors;
3+
4+
const ApiGateway = require("moleculer-web");
5+
const { ApolloService } = require("../../index");
6+
7+
const fetch = require("node-fetch");
8+
9+
describe("Integration test for greeter service", () => {
10+
const broker = new ServiceBroker({ logger: false });
11+
12+
let port;
13+
const apiSvc = broker.createService({
14+
name: "api",
15+
16+
mixins: [
17+
// Gateway
18+
ApiGateway,
19+
20+
// GraphQL Apollo Server
21+
ApolloService({
22+
// API Gateway route options
23+
routeOptions: {
24+
path: "/graphql",
25+
cors: true,
26+
mappingPolicy: "restrict",
27+
},
28+
29+
// https://www.apollographql.com/docs/apollo-server/v2/api/apollo-server.html
30+
serverOptions: {},
31+
}),
32+
],
33+
34+
settings: {
35+
port: 0, // Random
36+
},
37+
});
38+
39+
broker.createService({
40+
name: "greeter",
41+
42+
actions: {
43+
hello: {
44+
graphql: {
45+
query: "hello: String!",
46+
},
47+
handler() {
48+
return "Hello Moleculer!";
49+
},
50+
},
51+
welcome: {
52+
graphql: {
53+
query: `
54+
welcome(name: String!): String!
55+
`,
56+
},
57+
handler(ctx) {
58+
return `Hello ${ctx.params.name}`;
59+
},
60+
},
61+
/*update: {
62+
graphql: {
63+
subscription: "update: String!",
64+
tags: ["TEST"],
65+
},
66+
handler(ctx) {
67+
return ctx.params.payload;
68+
},
69+
},*/
70+
71+
danger: {
72+
graphql: {
73+
query: "danger: String!",
74+
},
75+
async handler() {
76+
throw new MoleculerClientError(
77+
"I've said it's a danger action!",
78+
422,
79+
"DANGER"
80+
);
81+
},
82+
},
83+
},
84+
});
85+
86+
beforeAll(async () => {
87+
await broker.start();
88+
port = apiSvc.server.address().port;
89+
});
90+
afterAll(() => broker.stop());
91+
92+
it("should call the greeter.hello action", async () => {
93+
const res = await fetch(`http://localhost:${port}/graphql`, {
94+
method: "post",
95+
body: JSON.stringify({
96+
operationName: null,
97+
variables: {},
98+
query: "{ hello }",
99+
}),
100+
headers: { "Content-Type": "application/json" },
101+
});
102+
103+
expect(res.status).toBe(200);
104+
expect(await res.json()).toStrictEqual({
105+
data: {
106+
hello: "Hello Moleculer!",
107+
},
108+
});
109+
});
110+
111+
it("should call the greeter.welcome action with parameter", async () => {
112+
const res = await fetch(`http://localhost:${port}/graphql`, {
113+
method: "post",
114+
body: JSON.stringify({
115+
operationName: null,
116+
variables: {},
117+
query: 'query { welcome(name: "GraphQL") }',
118+
}),
119+
headers: { "Content-Type": "application/json" },
120+
});
121+
122+
expect(res.status).toBe(200);
123+
expect(await res.json()).toStrictEqual({
124+
data: {
125+
welcome: "Hello GraphQL",
126+
},
127+
});
128+
});
129+
130+
it("should call the greeter.welcome action with query variable", async () => {
131+
const res = await fetch(`http://localhost:${port}/graphql`, {
132+
method: "post",
133+
body: JSON.stringify({
134+
operationName: null,
135+
variables: { name: "Moleculer GraphQL" },
136+
query: "query ($name: String!) { welcome(name: $name) }",
137+
}),
138+
headers: { "Content-Type": "application/json" },
139+
});
140+
141+
expect(res.status).toBe(200);
142+
expect(await res.json()).toStrictEqual({
143+
data: {
144+
welcome: "Hello Moleculer GraphQL",
145+
},
146+
});
147+
});
148+
149+
it("should call the greeter.danger and receives an error", async () => {
150+
const res = await fetch(`http://localhost:${port}/graphql`, {
151+
method: "post",
152+
body: JSON.stringify({
153+
operationName: null,
154+
variables: {},
155+
query: "query { danger }",
156+
}),
157+
headers: { "Content-Type": "application/json" },
158+
});
159+
160+
expect(res.status).toBe(200);
161+
expect(await res.json()).toStrictEqual({
162+
data: null,
163+
errors: [
164+
{
165+
extensions: {
166+
code: "INTERNAL_SERVER_ERROR",
167+
exception: {
168+
code: 422,
169+
retryable: false,
170+
type: "DANGER",
171+
},
172+
},
173+
locations: [
174+
{
175+
column: 9,
176+
line: 1,
177+
},
178+
],
179+
message: "I've said it's a danger action!",
180+
path: ["danger"],
181+
},
182+
],
183+
});
184+
});
185+
});

0 commit comments

Comments
 (0)