Skip to content
This repository was archived by the owner on Mar 28, 2022. It is now read-only.

Commit 49befa0

Browse files
committed
Core Server full unit tests coverage
1 parent e12cab5 commit 49befa0

3 files changed

Lines changed: 79 additions & 23 deletions

File tree

jest.config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ module.exports = {
1717
// An object that configures minimum threshold enforcement for coverage results
1818
coverageThreshold: {
1919
global: {
20-
branches: 55,
21-
functions: 65,
22-
lines: 65,
23-
statements: 65
20+
branches: 60,
21+
functions: 70,
22+
lines: 70,
23+
statements: 70
2424
}
2525
},
2626

test/unit/Libs.mocks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class Mock {
8181
onError: httpCreateServerOnError,
8282
listen: this._sandbox.stub().callsFake(httpCreateServerOnListen.runner),
8383
onListen: httpCreateServerOnListen,
84-
close: this._sandbox.stub()
84+
close: this._sandbox.stub().callsFake(cb => cb())
8585
}
8686
}
8787
};

test/unit/core/server/Server.spec.js

Lines changed: 74 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ const CoreMocks = require("../Core.mocks.js");
1818
const Server = require("../../../../lib/core/server/Server");
1919
const tracer = require("../../../../lib/core/tracer");
2020

21+
const wait = (time = 1000) => {
22+
return new Promise(resolve => {
23+
setTimeout(() => {
24+
resolve();
25+
}, time);
26+
});
27+
};
28+
2129
describe("Server", () => {
2230
let sandbox;
2331
let libsMocks;
@@ -54,41 +62,65 @@ describe("Server", () => {
5462
mocksMocks.restore();
5563
});
5664

57-
describe.skip("when instantiated", () => {
65+
describe("when initialized", () => {
5866
it("should be listening to process exit signals and stop the server if occurs", async () => {
5967
processOnStub.callsFake((event, cb) => {
60-
cb();
68+
wait().then(() => {
69+
cb();
70+
});
6171
});
72+
libsMocks.stubs.http.createServer.onListen.returns(null);
6273
await server.init();
74+
await server.start();
75+
await wait();
6376
expect(libsMocks.stubs.http.createServer.close.callCount).toEqual(1);
6477
});
6578
});
6679

80+
describe("custom routers", () => {
81+
it("should be registered when initializating http server", async () => {
82+
const fooRouter = sandbox.spy();
83+
server.addCustomRouter("fooPath", fooRouter);
84+
libsMocks.stubs.http.createServer.onListen.returns(null);
85+
await server.start();
86+
expect(libsMocks.stubs.express.use.calledWith("fooPath", fooRouter)).toEqual(true);
87+
});
88+
});
89+
6790
describe("when started", () => {
68-
it.skip("should be listening to server errors and throw an error if occurs", async () => {
69-
const error = new Error();
70-
libsMocks.stubs.http.createServer.onError.returns(error);
91+
it("should reject the promise if an error occurs when calling to server listen method", async () => {
92+
const error = new Error("Foo error");
93+
libsMocks.stubs.http.createServer.listen.throws(error);
94+
95+
await server.init();
7196

7297
try {
73-
await server.init();
98+
await server.start();
7499
} catch (err) {
75100
expect(err).toEqual(error);
76101
}
77102
});
78103

79-
it("should reject the promise if an error occurs when calling to server listen method", async () => {
80-
const error = new Error("Foo error");
81-
libsMocks.stubs.http.createServer.listen.throws(error);
82-
83-
await server.init();
104+
it("should be listening to server errors and throw an error if occurs", async () => {
105+
const error = new Error();
106+
libsMocks.stubs.http.createServer.onError.returns(error);
84107

85108
try {
109+
await server.init();
86110
await server.start();
87111
} catch (err) {
88112
expect(err).toEqual(error);
89113
}
90114
});
91115

116+
it("should not init httpServer more than once", async () => {
117+
libsMocks.stubs.http.createServer.onListen.returns(null);
118+
await server.start();
119+
await server.start();
120+
await server.start();
121+
expect(libsMocks.stubs.http.createServer.on.callCount).toEqual(1);
122+
});
123+
92124
it("should call to server listen, and resolve the promise when started", async () => {
93125
libsMocks.stubs.http.createServer.onListen.returns(null);
94126

@@ -110,7 +142,26 @@ describe("Server", () => {
110142
});
111143
});
112144

113-
describe.skip("restart method", () => {
145+
describe("stop method", () => {
146+
beforeEach(() => {
147+
libsMocks.stubs.http.createServer.onListen.returns(null);
148+
});
149+
150+
it("should call to stop the server", async () => {
151+
await server.init();
152+
await server.start();
153+
await server.stop();
154+
expect(libsMocks.stubs.http.createServer.close.callCount).toEqual(1);
155+
});
156+
157+
it("should not call to stop server if it has not been initialized", async () => {
158+
await server.init();
159+
await server.stop();
160+
expect(libsMocks.stubs.http.createServer.close.callCount).toEqual(0);
161+
});
162+
});
163+
164+
describe("restart method", () => {
114165
beforeEach(() => {
115166
libsMocks.stubs.http.createServer.onListen.returns(null);
116167
});
@@ -149,7 +200,7 @@ describe("Server", () => {
149200
});
150201
});
151202

152-
describe.skip("behaviors middleware", () => {
203+
describe("behaviors middleware", () => {
153204
const fooRequest = {
154205
method: "get",
155206
url: "foo-route"
@@ -159,20 +210,22 @@ describe("Server", () => {
159210
let resMock;
160211
let nextSpy;
161212

162-
beforeEach(() => {
213+
beforeEach(async () => {
163214
statusSpy = sandbox.spy();
164215
sendSpy = sandbox.spy();
165216
resMock = {
166217
status: statusSpy,
167218
send: sendSpy
168219
};
169220
nextSpy = sandbox.spy();
221+
libsMocks.stubs.http.createServer.onListen.returns(null);
222+
coreInstance.settings.get.withArgs("delay").returns(0);
170223
});
171224

172225
it("should call next if does not found a fixture in current feature matching the request url", async () => {
173226
await server.start();
174227

175-
server.fixturesMiddleware(fooRequest, resMock, nextSpy);
228+
server._fixturesMiddleware(fooRequest, resMock, nextSpy);
176229
expect(nextSpy.callCount).toEqual(1);
177230
});
178231

@@ -190,7 +243,8 @@ describe("Server", () => {
190243
}
191244
}
192245
};
193-
server.fixturesMiddleware(fooRequest, resMock, nextSpy);
246+
server._fixturesMiddleware(fooRequest, resMock, nextSpy);
247+
await wait(200);
194248
expect(resMock.status.getCall(0).args[0]).toEqual(200);
195249
});
196250

@@ -212,7 +266,8 @@ describe("Server", () => {
212266
}
213267
}
214268
};
215-
server.fixturesMiddleware(fooRequest, resMock, nextSpy);
269+
server._fixturesMiddleware(fooRequest, resMock, nextSpy);
270+
await wait(200);
216271
expect(resMock.send.getCall(0).args[0]).toEqual(fooBody);
217272
});
218273

@@ -229,7 +284,8 @@ describe("Server", () => {
229284
}
230285
}
231286
};
232-
server.fixturesMiddleware(fooRequest, resMock, nextSpy);
287+
server._fixturesMiddleware(fooRequest, resMock, nextSpy);
288+
await wait(200);
233289
expect(responseSpy.calledWith(fooRequest, resMock, nextSpy)).toEqual(true);
234290
});
235291
});

0 commit comments

Comments
 (0)