Skip to content

Commit 6708fbd

Browse files
committed
refactor: use http instead of express
1 parent 43d9b74 commit 6708fbd

File tree

4 files changed

+53
-229
lines changed

4 files changed

+53
-229
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"eslint-plugin-promise": "^4.2.1",
6363
"eslint-plugin-standard": "^4.0.0",
6464
"eslint-plugin-vue": "^5.2.3",
65-
"express": "^4.17.1",
65+
"finalhandler": "^1.1.2",
6666
"geckodriver": "^1.16.2",
6767
"glob": "^7.1.4",
6868
"jest": "^24.8.0",
@@ -84,7 +84,6 @@
8484
"peerDependencies": {
8585
"browserstack-local": "^1.4.2",
8686
"chromedriver": "^76.0.0",
87-
"express": "^4.17.1",
8887
"geckodriver": "^1.16.2",
8988
"jsdom": "^15.1.1",
9089
"puppeteer": "^1.19.0",

src/utils/commands/static-server.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
import http from 'http'
12
import { loadDependency } from '..'
23

34
export default class StaticServer {
45
static async loadDependencies() {
5-
if (StaticServer.express) {
6+
if (StaticServer.serveStatic) {
67
return
78
}
89

9-
StaticServer.express = await loadDependency('express')
1010
StaticServer.serveStatic = await loadDependency('serve-static')
11+
StaticServer.finalhandler = await loadDependency('finalhandler')
1112
}
1213

1314
static load(browser) {
@@ -31,22 +32,31 @@ export default class StaticServer {
3132
static async start(config, quiet) {
3233
await StaticServer.loadDependencies()
3334

34-
const app = StaticServer.express()
35-
36-
app.use(StaticServer.serveStatic(config.folder))
37-
3835
const host = process.env.HOST || config.host || 'localhost'
3936
const port = process.env.PORT || config.port || 3000
4037

41-
StaticServer.server = app.listen(port, host)
38+
const serve = StaticServer.serveStatic(config.folder)
4239

43-
if (!quiet) {
44-
// eslint-disable-next-line no-console
45-
console.info(`tib: Static server started on http://${host}:${port}`)
46-
}
40+
const server = http.createServer((req, res) => {
41+
serve(req, res, StaticServer.finalhandler(req, res))
42+
})
43+
44+
await new Promise((resolve, reject) => {
45+
server.on('error', reject)
46+
server.listen(port, host, () => {
47+
if (!quiet) {
48+
// eslint-disable-next-line no-console
49+
console.info(`tib: Static server started on http://${host}:${port}`)
50+
}
4751

48-
config.host = host
49-
config.port = port
52+
config.host = host
53+
config.port = port
54+
55+
StaticServer.server = server
56+
57+
resolve(server)
58+
})
59+
})
5060
}
5161

5262
static stop() {

test/unit/command.static-server.test.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
import express from 'express'
1+
import http from 'http'
2+
import finalhandler from 'finalhandler'
23
import serveStatic from 'serve-static'
34
import { loadDependency } from '../../src/utils'
45

5-
jest.mock('express')
6+
jest.mock('finalhandler')
67
jest.mock('serve-static')
78
jest.mock('../../src/utils')
89

910
describe('StaticServer', () => {
1011
let StaticServer
1112

1213
beforeAll(() => {
13-
loadDependency.mockImplementation(moduleName => moduleName === 'express' ? express : serveStatic)
14+
loadDependency.mockImplementation(moduleName => moduleName === 'finalhandler' ? finalhandler : serveStatic)
1415
})
1516

1617
beforeEach(async () => {
@@ -25,7 +26,6 @@ describe('StaticServer', () => {
2526
test('should load dependencies once', async () => {
2627
expect(StaticServer.express).toBeUndefined()
2728
await StaticServer.loadDependencies()
28-
expect(StaticServer.express).toBeDefined()
2929
expect(StaticServer.serveStatic).toBeDefined()
3030

3131
expect(loadDependency).toHaveBeenCalledTimes(2)
@@ -68,12 +68,16 @@ describe('StaticServer', () => {
6868
})
6969

7070
test('should start static server', async () => {
71+
const on = jest.fn()
72+
const listen = jest.fn((port, host, cb) => cb())
7173
jest.spyOn(console, 'info').mockImplementation(_ => _)
74+
jest.spyOn(http, 'createServer').mockImplementation((fn) => {
75+
fn()
76+
return { on, listen }
77+
})
7278

73-
const use = jest.fn()
74-
const listen = jest.fn()
75-
StaticServer.express = jest.fn(() => ({ use, listen }))
76-
StaticServer.serveStatic = jest.fn()
79+
StaticServer.serveStatic = jest.fn(() => () => {})
80+
StaticServer.finalhandler = jest.fn()
7781

7882
const staticServerConfig = {
7983
folder: 'test-folder',
@@ -83,22 +87,25 @@ describe('StaticServer', () => {
8387

8488
await expect(StaticServer.start(staticServerConfig)).resolves.toBeUndefined()
8589

86-
expect(StaticServer.express).toHaveBeenCalled()
87-
expect(use).toHaveBeenCalled()
90+
expect(StaticServer.finalhandler).toHaveBeenCalled()
8891
expect(StaticServer.serveStatic).toHaveBeenCalledWith(staticServerConfig.folder)
8992

90-
expect(listen).toHaveBeenCalledWith(staticServerConfig.port, staticServerConfig.host)
93+
expect(listen).toHaveBeenCalledWith(staticServerConfig.port, staticServerConfig.host, expect.any(Function))
9194
// eslint-disable-next-line no-console
9295
expect(console.info).toHaveBeenCalled()
9396
})
9497

9598
test('should start static server but not warn when quiet', async () => {
99+
const on = jest.fn()
100+
const listen = jest.fn((port, host, cb) => cb())
96101
jest.spyOn(console, 'info').mockImplementation(_ => _)
102+
jest.spyOn(http, 'createServer').mockImplementation((fn) => {
103+
fn()
104+
return { on, listen }
105+
})
97106

98-
const use = jest.fn()
99-
const listen = jest.fn()
100-
StaticServer.express = jest.fn(() => ({ use, listen }))
101-
StaticServer.serveStatic = jest.fn()
107+
StaticServer.serveStatic = jest.fn(() => () => {})
108+
StaticServer.finalhandler = jest.fn()
102109

103110
const staticServerConfig = {
104111
folder: 'test-folder',
@@ -108,11 +115,10 @@ describe('StaticServer', () => {
108115

109116
await expect(StaticServer.start(staticServerConfig, true)).resolves.toBeUndefined()
110117

111-
expect(StaticServer.express).toHaveBeenCalled()
112-
expect(use).toHaveBeenCalled()
118+
expect(StaticServer.finalhandler).toHaveBeenCalled()
113119
expect(StaticServer.serveStatic).toHaveBeenCalledWith(staticServerConfig.folder)
114120

115-
expect(listen).toHaveBeenCalledWith(staticServerConfig.port, staticServerConfig.host)
121+
expect(listen).toHaveBeenCalledWith(staticServerConfig.port, staticServerConfig.host, expect.any(Function))
116122
// eslint-disable-next-line no-console
117123
expect(console.info).not.toHaveBeenCalled()
118124
})

0 commit comments

Comments
 (0)