Skip to content

Commit c626ab8

Browse files
committed
Improve test: divided in multiple describe, runned syncronously.
1 parent 05e7651 commit c626ab8

File tree

1 file changed

+116
-84
lines changed

1 file changed

+116
-84
lines changed

test/test.js

Lines changed: 116 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
const assert = require("assert")
22
const fs = require("fs")
3-
const path = require("path")
43
const http = require("http")
54
const https = require("https")
65

76
const sinon = require("sinon")
87

9-
const app = require("../index.js")()
8+
let app = require("../index.js")()
109
const certs = require("../certs.js")
1110

1211
const HTTPS_PORT = 4443
@@ -39,7 +38,7 @@ function makeRequest(path = "/", secure = true, port = HTTPS_PORT) {
3938
}
4039

4140
// TEST CERTFICATES
42-
describe("Testing certificates generation", function() {
41+
describe("Testing certs", function() {
4342
// timeout 5 min, since requires the mkcert executable download
4443
this.timeout(300000)
4544

@@ -77,19 +76,16 @@ describe("Testing certificates generation", function() {
7776
// inner async function
7877
(async() => {
7978
// set a non-existent custom cert path
80-
process.env.CERT_PATH = path.join("not", "exist")
79+
process.env.CERT_PATH = "does-not-exist"
8180
// stub the exit function
8281
sinon.stub(process, "exit")
8382
// listen
8483
await app.listen(HTTPS_PORT)
8584
// should exit 1
86-
assert(process.exit.isSinonProxy)
87-
assert(process.exit.called)
8885
assert(process.exit.calledWith(1))
86+
process.exit.restore()
8987
// close the server
9088
app.server.close()
91-
// restore the exit
92-
process.exit.restore()
9389
// restore the CERT_PATH to undefined
9490
delete process.env.CERT_PATH
9591
done()
@@ -98,100 +94,136 @@ describe("Testing certificates generation", function() {
9894
})
9995

10096
// TESTS MODULE
101-
describe("Testing https-localhost", () => {
97+
describe("Testing module", () => {
10298
// close the server after each test
10399
afterEach(() => app.server.close())
104100

105-
it("works as a module as custom express app", async function() {
106-
// prepare the server with a mock response
107-
app.get("/test/module", (req, res) => res.send("TEST"))
108-
// start the server
109-
await app.listen(HTTPS_PORT)
110-
// make the request and check the output
111-
await makeRequest("/test/module")
112-
.then(res => assert(res.data === "TEST"))
101+
it("works as express app", function(done) {
102+
(async() => {
103+
// prepare the server with a mock response
104+
app.get("/test/module", (req, res) => res.send("TEST"))
105+
// start the server
106+
await app.listen(HTTPS_PORT)
107+
// make the request and check the output
108+
await makeRequest("/test/module")
109+
.then(res => assert(res.data === "TEST"))
110+
done()
111+
})()
113112
})
114113

115-
it("works with environment port", async function() {
116-
// prepare the server with a mock response
117-
app.get("/test/module", (req, res) => res.send("TEST"))
118-
// set the environment port
119-
process.env.PORT = HTTPS_PORT
120-
// start the server
121-
await app.listen()
122-
// make the request and check the output
123-
await makeRequest("/test/module")
124-
.then(res => assert(res.data === "TEST"))
114+
it("works with environment port", function(done) {
115+
(async() => {
116+
// prepare the server with a mock response
117+
app.get("/test/module", (req, res) => res.send("TEST"))
118+
// set the environment port
119+
process.env.PORT = HTTPS_PORT
120+
// start the server
121+
await app.listen()
122+
// make the request and check the output
123+
await makeRequest("/test/module")
124+
.then(res => assert(res.data === "TEST"))
125+
done()
126+
})()
125127
})
128+
})
126129

127-
it("serves static files from custom path", async function() {
128-
// start the server (serving the test folder)
129-
app.serve("test", HTTPS_PORT)
130-
// make the request and check the output
131-
await makeRequest("/static.html")
132-
.then(res => assert(
133-
res.data.toString() === fs.readFileSync("test/static.html").toString()))
134-
})
130+
describe("Testing serve", () => {
131+
// close the server after each test
132+
afterEach(() => app.server.close())
135133

136-
it("serves static files from default env port", async function() {
137-
// set the environment port
138-
process.env.PORT = HTTPS_PORT
139-
// start the server (serving the default folder)
140-
app.serve("test")
141-
// make the request and check the output
142-
await makeRequest("/static.html")
143-
.then(res => assert(res.data.toString() ===
134+
it("serves static files from custom path", function(done) {
135+
(async() => {
136+
// start the server (serving the test folder)
137+
app.serve("test", HTTPS_PORT)
138+
// make the request and check the output
139+
await makeRequest("/static.html")
140+
.then(res => assert(res.data.toString() ===
144141
fs.readFileSync("test/static.html").toString()))
142+
done()
143+
})()
145144
})
146145

147-
it("doesn't crash on 404", async function() {
148-
// start the server (serving the default folder)
149-
app.serve()
150-
// make the request and check the status code
151-
await makeRequest("/do-not-exist")
152-
.then(res => assert(res.statusCode === 404))
146+
it("serves static files from default env port", function(done) {
147+
(async() => {
148+
// set the environment port
149+
process.env.PORT = HTTPS_PORT
150+
// start the server (serving the default folder)
151+
app.serve("test")
152+
// make the request and check the output
153+
await makeRequest("/static.html")
154+
.then(res => assert(res.data.toString() ===
155+
fs.readFileSync("test/static.html").toString()))
156+
done()
157+
})()
153158
})
154159

155-
it("looks for a 404.html file", async function() {
156-
// start the server (serving the default folder)
157-
await app.serve("test", HTTPS_PORT)
158-
// make the request and check the result
159-
await makeRequest("/do-not-exist.html")
160-
.then(res => {
161-
assert(res.statusCode === 404)
162-
assert(res.data.toString() ===
163-
fs.readFileSync("test/404.html").toString())
164-
})
160+
it("doesn't crash on 404", function(done) {
161+
(async() => {
162+
// start the server (serving the default folder)
163+
app.serve()
164+
// make the request and check the status code
165+
await makeRequest("/do-not-exist")
166+
.then(res => assert(res.statusCode === 404))
167+
done()
168+
})()
169+
})
170+
171+
it("looks for a 404.html file", function(done) {
172+
(async() => {
173+
// start the server (serving the default folder)
174+
await app.serve("test", HTTPS_PORT)
175+
// make the request and check the result
176+
await makeRequest("/do-not-exist.html")
177+
.then(res => {
178+
assert(res.statusCode === 404)
179+
assert(res.data.toString() ===
180+
fs.readFileSync("test/404.html").toString())
181+
})
182+
done()
183+
})()
165184
})
166185

167-
it("doesn't crash if the static path doesn't exists", async function() {
168-
// start the server (serving a non existing folder)
169-
app.serve("do-not-exists")
170-
// make the request and check the status code
171-
await makeRequest("/")
172-
.then(res => assert(res.statusCode === 404))
186+
it("doesn't crash if the static path doesn't exists", function(done) {
187+
(async() => {
188+
// start the server (serving a non existing folder)
189+
app.serve("does-not-exist")
190+
// make the request and check the status code
191+
await makeRequest("/")
192+
.then(res => assert(res.statusCode === 404))
193+
done()
194+
})()
173195
})
196+
})
174197

175-
it("redirect http to https", async function() {
176-
// start the redirection
177-
await app.redirect(HTTP_PORT)
178-
// make the request and check the status
179-
await makeRequest("/", false, HTTP_PORT)
180-
.then(res => assert(res.statusCode === 301))
198+
// OTHER TESTS
199+
describe("Testing additional features", () => {
200+
it("redirect http to https", function(done) {
201+
(async() => {
202+
// start the redirection
203+
await app.redirect(HTTP_PORT)
204+
// make the request and check the status
205+
await makeRequest("/", false, HTTP_PORT)
206+
.then(res => assert(res.statusCode === 301))
207+
done()
208+
})()
181209
})
182210

183-
// IMPORTANT: this test MUST be the latest one, always.
184-
// It delete the cache og the index module,
185-
// so the variable app is broken after this test.
186-
it("is ready for production", async function() {
187-
// set NODE_ENV to production
188-
delete require.cache[require.resolve("../index.js")]
189-
process.env.NODE_ENV = "production"
190-
const production = require("../index.js")()
191-
// start the server (serving the test folder)
192-
production.serve("test", HTTPS_PORT)
193-
// make the request and check the output
194-
await makeRequest("/static.html")
195-
.then(res => assert(res.headers["content-encoding"] === "gzip"))
211+
it("is ready for production", function(done) {
212+
(async() => {
213+
// set NODE_ENV to production
214+
delete require.cache[require.resolve("../index.js")]
215+
process.env.NODE_ENV = "production"
216+
app = require("../index.js")()
217+
// start the server (serving the test folder)
218+
app.serve("test", HTTPS_PORT)
219+
// make the request and check the output
220+
await makeRequest("/static.html")
221+
.then(res => assert(res.headers["content-encoding"] === "gzip"))
222+
// reset NODE_ENV and app
223+
delete require.cache[require.resolve("../index.js")]
224+
delete process.env.NODE_ENV
225+
app = require("../index.js")()
226+
done()
227+
})()
196228
})
197229
})

0 commit comments

Comments
 (0)