Skip to content

Commit 0025dbb

Browse files
committed
feat: add quiet option to disable logs
1 parent 4324211 commit 0025dbb

File tree

5 files changed

+83
-8
lines changed

5 files changed

+83
-8
lines changed

src/browser.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ export default class Browser extends Hookable {
2020

2121
abstractGuard('Browser', new.target)
2222

23-
this.config = config
23+
this.config = {
24+
quiet: false,
25+
...config
26+
}
27+
2428
this.ready = false
2529

2630
if (config.extendPage && typeof config.extendPage === 'function') {

src/utils/commands/static-server.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ export default class StaticServer {
2424
return
2525
}
2626

27-
browser.hook('start:before', () => StaticServer.start(browser.config.staticServer))
27+
const config = {
28+
quiet: browser.config.quiet,
29+
...browser.config.staticServer
30+
}
31+
32+
browser.hook('start:before', () => StaticServer.start(config))
2833
browser.hook('close:after', StaticServer.stop)
2934
}
3035

@@ -40,8 +45,10 @@ export default class StaticServer {
4045

4146
StaticServer.server = app.listen(port, host)
4247

43-
// eslint-disable-next-line no-console
44-
console.info(`tib: Static server started on http://${host}:${port}`)
48+
if (!config.quiet) {
49+
// eslint-disable-next-line no-console
50+
console.info(`tib: Static server started on http://${host}:${port}`)
51+
}
4552

4653
config.host = host
4754
config.port = port

src/utils/commands/xvfb.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ export default class Xvfb {
3232
browser.config.xvfb = true
3333
}
3434

35-
browser.hook('start:before', () => Xvfb.start(typeof browser.config.xvfb === 'object' ? browser.config.xvfb : {}))
35+
const config = {
36+
quiet: browser.config.quiet,
37+
...browser.config.xvfb
38+
}
39+
40+
browser.hook('start:before', () => Xvfb.start(config))
3641
browser.hook('close:after', Xvfb.stop)
3742
}
3843

@@ -51,7 +56,7 @@ export default class Xvfb {
5156
return !!Xvfb.process && Xvfb.process.connected && !Xvfb.closed
5257
}
5358

54-
static start({ displayNum = 99, args = [] } = {}) {
59+
static start({ displayNum = 99, args = [], quiet } = {}) {
5560
Xvfb.isSupported(true)
5661
Xvfb.closed = false
5762

@@ -94,7 +99,9 @@ export default class Xvfb {
9499
if (code === 1) {
95100
const error = stderr.match(/\(EE\) (?!\(EE\))(.+?)$/m)[1] || stderr
96101
if (stderr.includes('already active for display')) {
97-
console.warn(`Xvfb: ${error}`) // eslint-disable-line no-console
102+
if (!quiet) {
103+
console.warn(`Xvfb: ${error}`) // eslint-disable-line no-console
104+
}
98105
return
99106
}
100107

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,32 @@ describe('StaticServer', () => {
9292
expect(console.info).toHaveBeenCalled()
9393
})
9494

95+
test('should start static server but not warn when quiet', async () => {
96+
jest.spyOn(console, 'info').mockImplementation(_ => _)
97+
98+
const use = jest.fn()
99+
const listen = jest.fn()
100+
StaticServer.express = jest.fn(() => ({ use, listen }))
101+
StaticServer.serveStatic = jest.fn()
102+
103+
const staticServerConfig = {
104+
quiet: true,
105+
folder: 'test-folder',
106+
host: 'test-host',
107+
port: 667
108+
}
109+
110+
await expect(StaticServer.start(staticServerConfig)).resolves.toBeUndefined()
111+
112+
expect(StaticServer.express).toHaveBeenCalled()
113+
expect(use).toHaveBeenCalled()
114+
expect(StaticServer.serveStatic).toHaveBeenCalledWith(staticServerConfig.folder)
115+
116+
expect(listen).toHaveBeenCalledWith(staticServerConfig.port, staticServerConfig.host)
117+
// eslint-disable-next-line no-console
118+
expect(console.info).not.toHaveBeenCalled()
119+
})
120+
95121
test('should stop static server', async () => {
96122
const close = jest.fn(cb => cb())
97123
StaticServer.server = { close }

test/unit/command.xvfb.test.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ describe('xvfb', () => {
111111
expect(Xvfb.isRunning()).toBe(false)
112112
})
113113

114-
test('should warn when Xvfb already running', () => {
114+
test('should warn when Xvfb already running and quiet false', () => {
115115
jest.spyOn(os, 'platform').mockReturnValue('linux')
116116
const spy = jest.spyOn(console, 'warn').mockImplementation(() => {})
117117
jest.spyOn(cp, 'spawn').mockImplementation(() => {
@@ -142,6 +142,37 @@ Fatal server error:
142142
expect(Xvfb.isRunning()).toBe(false)
143143
})
144144

145+
test('should warn when Xvfb already running unless quiet', () => {
146+
jest.spyOn(os, 'platform').mockReturnValue('linux')
147+
const spy = jest.spyOn(console, 'warn').mockImplementation(() => {})
148+
jest.spyOn(cp, 'spawn').mockImplementation(() => {
149+
return {
150+
connected: true,
151+
on(type, fn) {
152+
if (type === 'close') {
153+
fn(1, 0)
154+
}
155+
},
156+
stderr: {
157+
on(type, fn) {
158+
if (type === 'data') {
159+
fn(`(EE)
160+
Fatal server error:
161+
(EE) Server is already active for display 99
162+
If this server is no longer running, remove /tmp/.X99-lock
163+
and start again.
164+
(EE)`)
165+
}
166+
}
167+
}
168+
}
169+
})
170+
171+
Xvfb.start({ quiet: true })
172+
expect(spy).not.toHaveBeenCalled()
173+
expect(Xvfb.isRunning()).toBe(false)
174+
})
175+
145176
test('should warn when Xvfb failed to start', () => {
146177
jest.spyOn(os, 'platform').mockReturnValue('linux')
147178
jest.spyOn(cp, 'spawn').mockImplementation(() => {

0 commit comments

Comments
 (0)